简体   繁体   English

基于数字字段对Lucene中的搜索结果进行排序

[英]Sorting search result in Lucene based on a numeric field

I have some docs with two fields: text, count.我有一些包含两个字段的文档:文本、计数。

I've used Lucene to index docs and now I want to search in text and get the result sorted by count in descending order.我已经使用Lucene来索引文档,现在我想在文本中搜索并按降序按计数排序结果。 How can I do that?我怎样才能做到这一点?

The default search implementation of Apache Lucene returns results sorted by score (the most relevant result first), then by id (the oldest result first). Apache Lucene 的默认搜索实现返回按分数排序的结果(最相关的结果在前),然后是 id(最旧的结果在前)。

This behavior can be customized at query time with an additionnal Sort parameter .可以在查询时使用附加的 Sort 参数自定义此行为。

TopFieldDocs Searcher#search(Query query, Filter filter, int n, Sort sort) TopFieldDocs Searcher#search(Query query, Filter filter, int n, Sort sort)

The Sort parameter specifies the fields or properties used for sorting. Sort 参数指定用于排序的字段或属性。 The default implementation is defined this way :默认实现是这样定义的:

new Sort(new SortField[] { SortField.FIELD_SCORE, SortField.FIELD_DOC });

To change sorting, you just have to replace fields with the ones you want :要更改排序,您只需将字段替换为您想要的字段:

new Sort(new SortField[] {
SortField.FIELD_SCORE,
new SortField("field_1", SortField.STRING),
new SortField("field_2", SortField.STRING) });

This sounds simple, but will not work until the following conditions are met :这听起来很简单,但在满足以下条件之前不会起作用:

  • You have to specify the type parameter of SortField(String field, int type) to make Lucene find your field, even if this is normaly optional.您必须指定 SortField(String field, int type) 的类型参数才能让 Lucene 找到您的字段,即使这通常是可选的。
  • The sort fields must be indexed but not tokenized :排序字段必须被索引但不能被标记:

    document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));

  • The sort fields content must be plain text only.排序字段内容只能是纯文本。 If only one single element has a special character or accent in one of the fields used for sorting, the whole search will return unsorted results.如果在用于排序的字段之一中只有一个元素具有特殊字符或重音符号,则整个搜索将返回未排序的结果。

Check this tutorial .检查本教程

Below line will do the trick.下面的线会做的伎俩。 Last parameter is boolean reverse if you set it to true it will sort in reverse order ie descending in your case.最后一个参数是boolean reverse如果您将其设置为 true 它将以相反的顺序排序,即在您的情况下降序。

  SortField longSort = new SortedNumericSortField(FIELD_NAME_LONG, SortField.Type.LONG, true);

Sample code:示例代码:

  IndexSearcher searcher = new IndexSearcher(reader);
  Query q = new MultiFieldQueryParser(new String[] { FIELD_NAME_NAME}, analyzer).parse("YOUR_QUERY") );

  SortField longSort = new SortedNumericSortField(FIELD_NAME_LONG, SortField.Type.LONG, true);

  Sort sort = new Sort(longSort);
  ScoreDoc[] hits = searcher.search(q, 10 , sort).scoreDocs;

Also it's necessary that you add you sort enabled field as a NumericDocValuesField when you create your index.此外,在创建索引时,您还需要将启用排序的字段添加为NumericDocValuesField

 doc.add(new NumericDocValuesField(FIELD_NAME_LONG, longValue));//sort enabled field

Code is as per lucene-core-5.0.0代码按照 lucene-core-5.0.0

first:第一的:

Fieldable count = new NumericField("count", Store.YES, true);

second:第二:

SortField field = new SortField("count", SortField.INT);
Sort sort = new Sort(field);

third:第三:

TopFieldDocs docs = searcher.search(query, 20, sort);
ScoreDoc[] sds = docs.scoreDocs;

Like this is OK !这样就OK了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM