Lucene 对所查的结果进行排序

前面介绍完查询以后,现在要对查询结果进行排序的显示。

代码:

package com.querytype;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

public class QuerySortDemo2 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  try {
   /*
    * Document doc1 = new Document(); Field f1 = new
    * Field("bookNumber","003",Field.Store.YES, Field.Index.TOKENIZED);
    * Field f2 = new Field("bookname","钢铁是怎样炼成的",Field.Store.YES,
    * Field.Index.TOKENIZED); Field f3 = new
    * Field("publishdate","1970-01-01",Field.Store.YES,
    * Field.Index.TOKENIZED); doc1.add(f1); doc1.add(f2); doc1.add(f3);
    * Document doc2 = new Document(); f1 = new
    * Field("bookNumber","001",Field.Store.YES, Field.Index.TOKENIZED);
    * f2 = new Field("bookname","钢铁是怎样炼成的2",Field.Store.YES,
    * Field.Index.TOKENIZED); f3 = new
    * Field("publishdate","1990-01-01",Field.Store.YES,
    * Field.Index.TOKENIZED); doc2.add(f1); doc2.add(f2); doc2.add(f3);
    * Document doc3 = new Document(); f1 = new
    * Field("bookNumber","004",Field.Store.YES, Field.Index.TOKENIZED);
    * f2 = new Field("bookname","钢铁是怎样炼成的4",Field.Store.YES,
    * Field.Index.TOKENIZED); f3 = new
    * Field("publishdate","1980-01-01",Field.Store.YES,
    * Field.Index.TOKENIZED); doc3.add(f1); doc3.add(f2); doc3.add(f3);
    *
    * Document doc4 = new Document(); f1 = new
    * Field("bookNumber","004",Field.Store.YES, Field.Index.TOKENIZED);
    * f2 = new Field("bookname","钢铁是怎样炼成的4",Field.Store.YES,
    * Field.Index.TOKENIZED); f3 = new
    * Field("publishdate","1970-01-01",Field.Store.YES,
    * Field.Index.TOKENIZED); doc4.add(f1); doc4.add(f2); doc4.add(f3);
    *
    *
    * File file = new File("d://demo"); Analyzer luceneAnalyzer = new
    * StandardAnalyzer(); IndexWriter writer = new IndexWriter(file,
    * luceneAnalyzer, true); writer.setUseCompoundFile(true);
    * writer.optimize(); writer.addDocument(doc1);
    * writer.addDocument(doc2); writer.addDocument(doc3);
    * writer.addDocument(doc4); writer.close();
    */

   IndexSearcher search = new IndexSearcher("d://demo");

   String field = "bookname";
   String queryStr = "钢铁";
   QueryParser parser = new QueryParser(field, new StandardAnalyzer());
   Query q = parser.parse(queryStr);

//上面的查询是通过QueryParser来查询,下面通过SORT来进行排序,排序规则是首先根据bookNumber来排序,然后再根据publishdate来排序,第二个参数表示该字段是什么类型,第三个字段表示排列顺序(FALSH:升序,TRUE:降序)
   Sort sort = new Sort();
   SortField f1 = new SortField("bookNumber", SortField.INT, false);
   SortField f2 = new SortField("publishdate", SortField.STRING, false);
   sort.setSort(new SortField[] { f1, f2 });

//如果指定多个FIELD来进行排序,就把多个FIELD组合成数据塞到SORT中
   Hits hits = search.search(q, sort);
   System.out.println(q.toString());
   for (int i = 0; i < hits.length(); i++) {
    Document doc = hits.doc(i);
    System.out.println("书名:" + doc.get("bookname") + "\t\t");
    System.out.println("书号:" + doc.get("bookNumber"));
    System.out.println("出版日期:" + doc.get("publishdate"));
    System.out.println("#####################");
   }

  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

 

SortField提供的FIELD的类型:string,int,float.指定FIELD的类型对排序过程的效率有巨大的影响。

 

你可能感兴趣的:(apache,Lucene,出版)