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

Lucene使用Sort方法对文档进行排序

时间:2015-04-03 11:26:20      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

/*
 * 这段代码使用的是Lucene对于文档排序使用的sort方法
 * 这里可以使用多个sort的构造方法,来自己定义,所想要实现的功能。
 * */
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.Sort;
import org.apache.lucene.search.TermQuery;




public class SortScore {



public SortScore(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);
TermQuery q = new TermQuery(new Term("bookname", "女"));

//使用sort。RELEVANCE来制定排序的准则
Hits hits = searcher.search(q, Sort.RELEVANCE);

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

SortScore ss = new SortScore("E:\\Lucene项目\\索引文件");


}


}



结果显示排序后的截图:


技术分享

Lucene使用Sort方法对文档进行排序

标签:

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

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