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

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
      public void createIndexDB() throws Exception {
           // 创建Article对象(id,title,content)
          Article art = new Article(1,"标题","搜索标题的内容是什么呢?Lucene学习学习哈哈哈");
          
           //创建Document对象
          Document doc = 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
           * 参数二:采用什么策略将文本拆分,一个策略就是一个具体实现类
           * 参数三:最大值
           */
          Directory directory = FSDirectory. open(newFile("F:/LuceneDB")) ;
          Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_30);
          MaxFieldLength maxFiledLength = MaxFieldLength.LIMITED;
          IndexWriter indexWriter = 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
      public void searchByKeyword () throws Exception{
          String keyword = "标题";
          
          Directory directory = FSDirectory. open(newFile("F:/LuceneDB")) ;
          List<Article> list = newArrayList<Article>();
          Analyzer analyzer = newStandardAnalyzer(Version.LUCENE_30);
          MaxFieldLength maxFiledLength = MaxFieldLength. LIMITED ;
           //创建IndexSearcher字符流对象
          IndexSearcher indexSearcher = newIndexSearcher(directory);
           //创建查询解析器对象:参数一,版本号;参数二,针对document对象中 的哪个属性值进行搜索  
          QueryParser queryParser = newQueryParser(Version.LUCENE_30, "content" , analyzer );
           //封装查询关键字
          Query query = queryParser.parse( keyword);
           //根据关键字,去索引库中的词汇表搜索:参数一,封装关键字;参数二,取出搜索内容钱100条,不足则取实         
          TopDocs topDocs = indexSearcher.search( query, 100);
           //取值
           for( int i=0; i< topDocs. scoreDocs. length; i++){
               //取出封装封装编号和分数的对象
              ScoreDoc scoreDoc topDocs.scoreDocs[i];
               //取编号
               int no = scoreDoc. doc;
               //根据编号去索引库里面搜索document对象
              Document doc = indexSearcher.doc(no);
               //取出document对象中的三个属性值
              String id = doc.get("id");
              String title = doc.get("title");
              String content = doc.get("content");
               //封装到Article对象中
              Article article = newArticle(Integer.parseInt(id),title,content);
               //将article对象加入到list集合中
               list.add( article);
          }
           //迭代结果
           for(Article a : list){
              System. out.println( a );
          }    
     }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值