1 Lucene笔记(一):创建索引库、根据关键字搜索

来源:互联网 发布:邢台网络推广招聘 编辑:程序博客网 时间:2024/06/09 14:27
package ch.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.junit.Test;

import ch.lucene.entity.Article;

public class FirstLucenne {
    /**
     * 创建索引库
     * 1,创建javabean对象
     * 2,创建document对象
     * 3,将javabean对象所有属性值,放到document对象中
     * 4,创建IndexWrite对象
     * 5,将document对象通过Indexwrite写入到索引库中
     * 6,关闭IndexWrite对象
     **/   
      @Test
     publicvoidcreateIndexDB()throwsException {
          // 创建Article对象(id,title,content)
          Articleart = newArticle(1,"标题","搜索标题的内容是什么呢?Lucene学习学习哈哈哈");
          
          //创建Document对象
          Documentdoc= newDocument();
          //将Article对象中三个属性值绑定到document对象中
          /* 参数一:表示document中的 xid属性,与Article中的属性名id相对应,项目中提倡相同
           * 参数二:document中xid属性的值value,与Article相同
           * 参数三:表示是否讲id属性的值存入词汇表中:
                    Store.YES表示是,
                    Store.NO表示否,
                        项目中提倡非id值都存入词汇表中
           * 参数四:是否将xid的属性值进行分词算法
                    Index.ANALYZED:表示会对该属性值进行词汇拆分
                    Index.NOT_ANALYZED:表示不会对该属性值进行词汇拆分
                        项目中提倡非id属性值都进行词汇拆分
                    目前,一个汉字为一个拆分词
           */
          doc.add(new Field( "id",art.getId().toString(), Store.YES, Index.ANALYZED));
          doc.add(new Field( "title",art.getTitle(), Store.YES, Index.ANALYZED));
          doc.add(new Field( "content",art.getContent(), Store.YES, Index.ANALYZED));
          
          //将document对象写入索引库
          /* 参数一: Lucene索引库最终对应硬盘的目录。如:E:/LuceneDB
           * 参数二:采用什么策略将文本拆分,一个策略就是一个具体实现类
           * 参数三:最大值
           */
          Directorydirectory= FSDirectory. open(newFile("F:/LuceneDB")) ;
          Analyzeranalyzer= newStandardAnalyzer(Version.LUCENE_30);
          MaxFieldLengthmaxFiledLength= MaxFieldLength.LIMITED;
          IndexWriterindexWriter= newIndexWriter(directory,analyzer, maxFiledLength);
          indexWriter.addDocument(doc);
          indexWriter.close();
     }
    /**
     * 根据关键字搜索
     * 1,创建IndexSearcher对象
       2,创建QueryParser对象
       3,创建Query对象来封装关键字
       4,用IndexSearcher对象去索引库中查询符合条件的前100条记录,不足100条记录的以实际为准
       5,获取符合条件的编号
       6,用indexSearcher对象去索引库中查询编号对应的Document对象
       7,将Document对象中的所有属性取出,再封装回JavaBean对象中去,并加入到集合中保存,以备将之用
     **/
      @Test
     publicvoidsearchByKeyword ()throwsException{
          Stringkeyword = "标题";
          
          Directorydirectory= FSDirectory. open(newFile("F:/LuceneDB")) ;
          List<Article>list= newArrayList<Article>();
          Analyzeranalyzer= newStandardAnalyzer(Version.LUCENE_30);
          MaxFieldLengthmaxFiledLength= MaxFieldLength.LIMITED;
          //创建IndexSearcher字符流对象
          IndexSearcherindexSearcher= newIndexSearcher(directory);
          //创建查询解析器对象:参数一,版本号;参数二,针对document对象中 的哪个属性值进行搜索  
          QueryParserqueryParser= newQueryParser(Version.LUCENE_30,"content" , analyzer );
          //封装查询关键字
          Queryquery = queryParser.parse(keyword);
          //根据关键字,去索引库中的词汇表搜索:参数一,封装关键字;参数二,取出搜索内容钱100条,不足则取实         
          TopDocstopDocs = indexSearcher.search(query, 100);
          //取值
          for(int i=0; i< topDocs. scoreDocs. length; i++){
              //取出封装封装编号和分数的对象
              ScoreDocscoreDoc topDocs.scoreDocs[i];
              //取编号
              int no = scoreDoc. doc;
              //根据编号去索引库里面搜索document对象
              Documentdoc= indexSearcher.doc(no);
              //取出document对象中的三个属性值
              Stringid= doc.get("id");
              Stringtitle= doc.get("title");
              Stringcontent= doc.get("content");
              //封装到Article对象中
              Articlearticle= newArticle(Integer.parseInt(id),title,content);
              //将article对象加入到list集合中
              list.add(article);
          }
          //迭代结果
          for(Articlea : list){
              System.out.println(a);
          }   
     }
}
0 0