lucene使用sort进行结果查询排序

本文介绍了一个使用Lucene进行文档排序的实际案例,展示了如何通过设置不同的字段类型和SortField来实现查询结果的有效排序。

利用sort对查询结果进行排序示例

  • 对于要排序的字段,在索引的时候可以Field.Index.NOT_ANALYZE
package com.cn;
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.IndexWriterConfig;
import org.apache.lucene.index.Term;
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.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class TTT {

	public static void main(String []args)throws Exception {
		String [] ids = {"1","2","3","4","5"};
		String [] bookName = {"java start","java begin","java in action","java web","java ssh"};
		String [] page = {"300","400","256","302","279"};
		String [] price = {"89","99","70","60","120"};
		Directory directory = new RAMDirectory();
		
		IndexWriter indexWriter = new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_34, new StandardAnalyzer(Version.LUCENE_34)));
		for(int i = 0;i < ids.length;i++){
			Document doc = new Document();
			doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
			doc.add(new Field("bookName",bookName[i],Field.Store.YES,Field.Index.ANALYZED));
			doc.add(new Field("page",page[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
			doc.add(new Field("price",price[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
			indexWriter.addDocument(doc);
		}
		System.out.println("total:"+indexWriter.numDocs());
		indexWriter.close();
		Term term = new Term("bookName","java");
		Query query = new TermQuery(term);
		IndexSearcher indexSearcher = new IndexSearcher(directory);
		Sort sort = new Sort();
		SortField sortField = new SortField("price", SortField.INT);
		sort.setSort(sortField);
        //参数10的意思为返回10条记录
		TopDocs topDocs = indexSearcher.search(query, 10, sort);
		ScoreDoc [] scoreDoc = topDocs.scoreDocs;
		for(int i=0;i<scoreDoc.length;i++){
			Document d = indexSearcher.doc(scoreDoc[i].doc);
			System.out.println(d.get("bookName")+"\t"+d.get("page")+"\t"+d.get("price"));
		}
	}
}

结果:
total:5java web    302    60java in action    256    70java start    300    89java begin    400    99java ssh    279    120
如果在sort加上第三个参数:true是否反序,结果如下 sort默认的排序是升序排列的
total:5
java ssh	279	120
java begin	400	99
java start	300	89
java in action	256	70

相关API如下

BYTE
          Sort using term values as encoded Bytes.

CUSTOM
          Sort using a custom Comparator.

DOC
          Sort by document number (index order).

DOUBLE
          Sort using term values as encoded Doubles.

FIELD_DOC
          Represents sorting by document number (index order).

FIELD_SCORE
          Represents sorting by document score (relevance).

FLOAT
          Sort using term values as encoded Floats.

INT
          Sort using term values as encoded Integers.

LONG
        Sort using term values as encoded Longs.

SCORE
          Sort by document score (relevance).

SHORT
          Sort using term values as encoded Shorts.

STRING
          Sort using term values as Strings.

STRING_VAL
          Sort using term values as Strings, but comparing by value (using String.compareTo) for all comparisons.


Constructor Summary

SortField(String field, FieldCache.Parser parser)
          Creates a sort by terms in the given field, parsed to numeric values using a custom FieldCache.Parser.

SortField(String field, FieldCache.Parser parser, boolean reverse)
          Creates a sort, possibly in reverse, by terms in the given field, parsed to numeric values using a custom FieldCache.Parser.

SortField(String field, FieldComparatorSource comparator)
          Creates a sort with a custom comparison function.

SortField(String field, FieldComparatorSource comparator, boolean reverse)
          Creates a sort, possibly in reverse, with a custom comparison function.

SortField(String field, int type)
          Creates a sort by terms in the given field with the type of term values explicitly given.

SortField(String field, int type, boolean reverse)
          Creates a sort, possibly in reverse, by terms in the given field with the type of term values explicitly given.

SortField(String field, Locale locale)
          Creates a sort by terms in the given field sorted according to the given locale.

SortField(String field, Locale locale, boolean reverse)
          Creates a sort, possibly in reverse, by terms in the given field sorted according to the given locale.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值