6 Lucene笔记(六):检索结果排序问题

packagech.lucene.app;

importjava.util.ArrayList;
importjava.util.List;

importorg.apache.lucene.document.Document;
importorg.apache.lucene.index.IndexWriter;
importorg.apache.lucene.queryParser.QueryParser;
importorg.apache.lucene.search.IndexSearcher;
importorg.apache.lucene.search.Query;
importorg.apache.lucene.search.ScoreDoc;
importorg.apache.lucene.search.Sort;
importorg.apache.lucene.search.SortField;
importorg.apache.lucene.search.TopDocs;
importorg.junit.Test;

importch.lucene.entity.Article;
importch.lucene.utils.LuceneUtils;

/**
 * 排序问题
 *@authorJayin
 */
publicclassSixApp {
/**
 * 创建索引库
 *@throwsException
 */
     @Test
     publicvoidcreateIndexDB() throws Exception {
           //创建javabean 对象
          Articleart = newArticle(3, "搜索排序问题" , "搜索什么好呢,反正就随便咯,金木研说的。" );
           /*Article art1 = new Article(2, "搜索排序问题", "搜索什么好呢,反正就随便咯,金木研说的。。");
          Article art2 = new Article(4, "搜索问题", "搜索什么好呢金木研说的。。");
          Article art3 = new Article(1, "搜索问题", "反正就随便咯,金木研说的。");*/          
           //创建document对象,并将 javabean对象转换为document对象
          Documentdoc = LuceneUtils.javaBeanToDocument(art);
           //创建IndexWrite对象
          IndexWriteriw = newIndexWriter(LuceneUtils.getDirectory(), LuceneUtils.getAnalyzer(), LuceneUtils.getMaxFieldLength());
           //将doc写入到索引库中
           iw.addDocument( doc);
           //关闭
           iw.close();
     }
     /**
      * 根据关键字搜索
      *@throwsException
      */
     @Test
     publicvoidfindByKeyword() throws Exception {
           //关键字
          Stringkeyword = "金木";
           //创建数组
          List<Article>artList = newArrayList<Article>();
           //创建IndexSearcher字符流对象
          IndexSearcherindexSearcher = newIndexSearcher(LuceneUtils.getDirectory());
           //创建解析器
          QueryParserqueryParser = newQueryParser(LuceneUtils.getVersion(),"content", LuceneUtils.getAnalyzer());
           //封装关键字
          Queryquery = queryParser.parse(keyword);
           //根据关键字,去索引库中的词汇表搜索
           //TopDocs topDocs = indexSearcher.search(query, 100);          
           //按得分度高低排序
                   //TopDocs topDocs = indexSearcher.search(query,100);      
                   //创建排序对象
                   //参数一:id表示依据document对象中的哪个字段排序,例如:id
                   //参数二:SortField.INT表示document对象中该字段的类型,以常量方式书写
                   //参数三:true表示降序,类似于order by iddesc
                   //参数三:false表示升序,类似于order by idasc
                   //Sort sort = new Sort(new SortField("id",SortField.INT,false));
                   
                   //按count字段的降序排列,如果count字段相同的话,再按id的升序排序
                   Sortsort = newSort(
                             new SortField("count",SortField.INT,true),
                             new SortField("id",SortField.INT,false));
                   
                   //sort表示排序的条件
                   TopDocstopDocs = indexSearcher.search(query,null,100,sort);
          
           for( int i = 0 ; i< topDocs. scoreDocs. length; i++){
              //获取对象
              ScoreDocscoreDoc = topDocs.scoreDocs[i];
              //获取编号
              int no = scoreDoc. doc;
              //获取属性的值
              Documentdoc = indexSearcher.doc(no);
              //document转换成 javabean
              Articleart = (Article) LuceneUtils.documentToJavaBean(doc, Article.class);
              //将art添加到数组集合中
              artList.add(art);
              for(Articlearticle : artList){
                   System.out.println(article);
              }              
          }
     }
}



单字段排序:

Sort sort = new Sort(newSortField("id",SortField.INT,true));
TopDocs topDocs = indexSearcher.search( query,null,1000000,sort);

多字段排序:

Sort sort = new Sort(new SortField("count",SortField.INT,true),new SortField("id",SortField.INT,false));


单字段搜索:

QueryParser queryParser = new QueryParser(LuceneUtils.getVersion(),"content", LuceneUtils.getAnalyzer());


多字段搜索:(推荐)

QueryParser queryParser = new MultiFieldQueryParser(LuceneUtils.getVersion(),new String[]{"centent","title"}, LuceneUtils.getAnalyzer ());





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值