lucene多个域排序

lucene的sort类实现多个域排序,常见一个   SortField数据,将需要排序的域名字加入到这个数组中。
需要注意到是增加顺序,排序结果先按照第一个域排序,然后第二个域作为次要关键字排序。

// 常见一个排序域的数组
SortField[] sortArray;
sortArray = new SortField[] {new SortField("order", SortField.STRING), new SortField("num", SortField.STRING)};

// 根据域实现sort类
results = searcher.search(query, null, 100, new Sort(sortArray));

以下是测试代码:
注:lucene版本为3.0
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.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class SortByMultiFields {

	static String[] contentList = { "搜索 引擎", "Lucene 使用  方便 长度1", "使用 Lucene", "Lucene 功能强大 长度长度长度3", "Lucene 开放 源码 长度长度2", "Lucene 源码" };
	static String[] numberList = { "No.0", "No.1", "No.2", "No.3", "No.4", "No.5" };
	static String[] orderList = { "0", "3", "2", "3", "1", "1" };
	
 	public static void main(String[] args) throws Exception {
 		searchIndex();
	}

	private static void searchIndex() throws Exception {
		RAMDirectory ramDirectory = new RAMDirectory();
		IndexWriter writer = new IndexWriter(ramDirectory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
		
		for(int i = 0; i < contentList.length; i++) {
			Document document = new Document();
			Field contentField = new Field("content", contentList[i], Field.Store.YES, Field.Index.ANALYZED);
			Field numberField = new Field("num", numberList[i], Field.Store.YES, Field.Index.ANALYZED);
			Field orderField = new Field("order", orderList[i], Field.Store.YES, Field.Index.ANALYZED);
			
			document.add(contentField);
			document.add(numberField);
			document.add(orderField);
			
			writer.addDocument(document);
		}
		
		writer.close();
		
		// search 
		IndexSearcher searcher = new IndexSearcher(ramDirectory);
		QueryParser parser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30));
		Query query = parser.parse("Lucene");
		
		TopDocs results;
		
		System.out.println("+++++++++lucene 默认相关性排序++++++++++++++++++++++");
		results = searcher.search(query, 100);
		for(ScoreDoc sd : results.scoreDocs) {
			int docID = sd.doc;
			float score = sd.score;
			Document doc = searcher.doc(docID);
			System.out.println(doc.get("order") + " " + doc.get("num") + " " + doc.get("content") + " " + query.getBoost() + " " + score);
		}
		
		System.out.println("+++++++++指定域order排序++++++++++++++++++++++");
		results = searcher.search(query, null, 100, new Sort(new SortField("order", SortField.STRING)));
		for(ScoreDoc sd : results.scoreDocs) {
			int docID = sd.doc;
			float score = sd.score;
			Document doc = searcher.doc(docID);
			System.out.println(doc.get("order") + " " + doc.get("num") + " " + doc.get("content") + " " + query.getBoost() + " " + score);
		}
		
		System.out.println("+++++++++指定域order,number联合排序++++++++++++++++++++++");
		SortField[] sortArray;
		sortArray = new SortField[] {new SortField("order", SortField.STRING), new SortField("num", SortField.STRING)};
		results = searcher.search(query, null, 100, new Sort(sortArray));
		for(ScoreDoc sd : results.scoreDocs) {
			int docID = sd.doc;
			float score = sd.score;
			Document doc = searcher.doc(docID);
			System.out.println(doc.get("order") + " " + doc.get("num") + " " + doc.get("content") + " " + query.getBoost() + " " + score);
		}
		
	}

}


程序运行结果:

+++++++++lucene 默认相关性排序++++++++++++++++++++++
2 No.2 使用 Lucene 1.0 0.5
1 No.5 Lucene 源码 1.0 0.5
3 No.1 Lucene 使用  方便 长度1 1.0 0.3125
1 No.4 Lucene 开放 源码 长度长度2 1.0 0.3125
3 No.3 Lucene 功能强大 长度长度长度3 1.0 0.25
+++++++++指定域order排序++++++++++++++++++++++
1 No.4 Lucene 开放 源码 长度长度2 1.0 NaN
1 No.5 Lucene 源码 1.0 NaN
2 No.2 使用 Lucene 1.0 NaN
3 No.1 Lucene 使用  方便 长度1 1.0 NaN
3 No.3 Lucene 功能强大 长度长度长度3 1.0 NaN
+++++++++指定域order,number联合排序++++++++++++++++++++++
1 No.4 Lucene 开放 源码 长度长度2 1.0 NaN
1 No.5 Lucene 源码 1.0 NaN
2 No.2 使用 Lucene 1.0 NaN
3 No.1 Lucene 使用  方便 长度1 1.0 NaN
3 No.3 Lucene 功能强大 长度长度长度3 1.0 NaN





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值