码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-03 11:19:15      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 这里的类实现的功能是对多个字段进行排序
 * 比单个难度要大,但是有相应的方法,可以满足不同项目的不同需求
 * */
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项目\\索引文件");
}


}

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

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/44852169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!