【Lucene4.3开发之小试牛刀】

本篇适合对于刚学lucene的朋友们,在这之前笔者还是喜欢啰嗦几句,想要学好一门技术,首先就得从思想层次上全面了解这种技术的作用,适用范围,以及优缺点,对于这些理论,大家可以先在Google,或百度上深入了解一下,也可以到其官方网站上看文档或者WIKI,只有在思想层面上,大概了解这个技术的总体架构,那么学起来,就可以很快上手,举个例子,先"会当凌绝顶"一下,然后在爬山,肯定会容易一些,笔者想说的就是这个道理。

    下面就开始进入正题,本篇的入门代码,相对简单,主要是先把lucene添加的Demo给搭建起来,后续的修改,删除,查询会在后面的文章中一一补上,笔者觉得学习这东西还是得脚踏实地一步一步来比较好,只要真真正正理解每一行代码的意思,我们就算有收获了,有时候学习步伐太快,反而会根基不牢,效果不好。

    需要准备的Jar包

    Java代码
    lucene-core-4.3.1.jar
    lucene-core-4.3.1.jar

    Java代码
    lucene-analyzers-common-4.3.1.jar
    lucene-analyzers-common-4.3.1.jar

    Java代码
    package com.qin;
    import java.io.File;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.document.Field.Store;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    /**
    * Lucene的演示Demo类
    *
    * **/
    public class CommonLuceneBasic {
    /**
    * 抽象的父类文件夹
    * */
    public static Directory directory;
    /**
    * 返回IndexWriter
    * */
    public static IndexWriter getWriter() throws Exception{
    Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43);//设置标准分词器 ,默认是一元分词
    IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer);//设置IndexWriterConfig
    // iwc.setRAMBufferSizeMB(3);//设置缓冲区大小
    return new IndexWriter(directory,iwc);
    }
    /**
    * @indexPath 索引存放路径
    * **/
    public static void add(String indexWriterPath){
    IndexWriter writer=null;
    try{
    directory=FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
    writer=getWriter();
    Document doc=new Document();
    doc.add(new StringField("id", "1", Store.YES));//存储
    doc.add(new StringField("name", "张飞", Store.YES));//存储
    doc.add(new StringField("content", "也许放弃,才能靠近你!", Store.YES));//存储
    writer.addDocument(doc);//添加进写入流里
    writer.forceMerge(1);//优化压缩段,大规模添加数据的时候建议,少使用本方法,会影响性能
    writer.commit();//提交数据
    System.out.println("添加成功");
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    if(writer!=null){
    try{
    writer.close();//关闭流
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    }
    public static void main(String[] args) {
    String path="E:\\临时索引";
    add(path);//调用添加方法
    }
    }
    package com.qin;
    import java.io.File;
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.document.Field.Store;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    /**
    * Lucene的演示Demo类
    *
    * **/
    public class CommonLuceneBasic {
    /**
    * 抽象的父类文件夹
    * */
    public static Directory directory;
    /**
    * 返回IndexWriter
    * */
    public static IndexWriter getWriter() throws Exception{
    Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_43);//设置标准分词器 ,默认是一元分词
    IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_43, analyzer);//设置IndexWriterConfig
    // iwc.setRAMBufferSizeMB(3);//设置缓冲区大小
    return new IndexWriter(directory,iwc);
    }
    /**
    * @indexPath 索引存放路径
    * **/
    public static void add(String indexWriterPath){
    IndexWriter writer=null;
    try{
    directory=FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
    writer=getWriter();
    Document doc=new Document();
    doc.add(new StringField("id", "1", Store.YES));//存储
    doc.add(new StringField("name", "张飞", Store.YES));//存储
    doc.add(new StringField("content", "也许放弃,才能靠近你!", Store.YES));//存储
    writer.addDocument(doc);//添加进写入流里
    writer.forceMerge(1);//优化压缩段,大规模添加数据的时候建议,少使用本方法,会影响性能
    writer.commit();//提交数据
    System.out.println("添加成功");
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    if(writer!=null){
    try{
    writer.close();//关闭流
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    }
    public static void main(String[] args) {
    String path="E:\\临时索引";
    add(path);//调用添加方法
    }
    }

    添加成功之后,我们就可以通过Luke工具,进行索引查看

posted on 2013-09-16 09:11  潇洒kman  阅读(186)  评论(0编辑  收藏  举报