Lucene使用SortField对多个字段进行排序

/*
 * 这里的类实现的功能是对多个字段进行排序
 * 比单个难度要大,但是有相应的方法,可以满足不同项目的不同需求
 * */
package score;


import java.io.IOException;


import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;


public class SortFieldTest {



public SortFieldTest(String INDEX_STORE_PATH){

try{
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);

//首先定义文本
Document doc1 = new Document();
Field f1 = new Field("bookNumber", "0000001", Field.Store.YES, Field.Index.UN_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.UN_TOKENIZED);
doc1.add(f1);
doc1.add(f2);
doc1.add(f3);

Document doc2 = new Document();
f1 = new Field("bookNumber", "0000002", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "钢铁战士", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "1980-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc2.add(f1);
doc2.add(f2);
doc2.add(f3);


Document doc3 = new Document();
f1 = new Field("bookNumber", "0000003", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "篱笆女人和狗", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "1990-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc3.add(f1);
doc3.add(f2);
doc3.add(f3);

Document doc4 = new Document();
f1 = new Field("bookNumber", "0000004", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "女人是水做的", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "1999-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc4.add(f1);
doc4.add(f2);
doc4.add(f3);

Document doc5 = new Document();
f1 = new Field("bookNumber", "0000005", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "英雄儿女", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "2002-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc5.add(f1);
doc5.add(f2);
doc5.add(f3);

Document doc6 = new Document();
f1 = new Field("bookNumber", "0000006", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "白毛女", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "1985-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc6.add(f1);
doc6.add(f2);
doc6.add(f3);


Document doc7 = new Document();
f1 = new Field("bookNumber", "0000007", Field.Store.YES, Field.Index.UN_TOKENIZED);
f2 = new Field("bookname", "我的兄弟和女儿", Field.Store.YES, Field.Index.TOKENIZED);
f3 = new Field("publishdate", "1978-01-01", Field.Store.YES, Field.Index.UN_TOKENIZED);
doc7.add(f1);
doc7.add(f2);
doc7.add(f3);


//将书信息加入索引
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.addDocument(doc4);
writer.addDocument(doc5);
writer.addDocument(doc6);
writer.addDocument(doc7);
writer.close();


//构建一个查询
IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);
Term begin = new Term("publishdate", "1950-01-01");
Term end = new Term("publishdate", "2002-01-01");
RangeQuery rq = new RangeQuery(begin, end, false);

//构建两个SortField
Sort sort = new Sort();

//按照publishdate排序,降序
SortField sf1 = new SortField("publishdate", SortField.STRING, true);

//按bookNumber排序,升序
SortField sf2 = new SortField("bookname", SortField.INT, false);

//先按照publishdate排,在按照bookNumber排
sort.setSort(new SortField[]{sf1, sf2});

Hits hits = searcher.search(rq, sort);

for(int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
System.out.print("书名 :");
System.out.println(doc.get("bookname"));
System.out.print("得分 :");
System.out.println(hits.score(i));
System.out.print("内部ID :");
System.out.println(hits.id(i));
System.out.print("书号 : ");
System.out.println(doc.get("bookNumber"));
System.out.print("发行日期 : ");
System.out.println(doc.get("publishdate"));
System.out.println("===========================");

}

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

}
public static void main(String[] args) {
// TODO Auto-generated method stub
SortFieldTest sft = new SortFieldTest("E:\\Lucene项目\\索引文件");
}


}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。