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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21965778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:42:48  来源:igfitidea点击:

Sorting search result in Lucene based on a numeric field

javalucene

提问by Taher Khorshidi

I have some docs with two fields: text, count.

我有一些包含两个字段的文档:文本、计数。

I've used Luceneto index docs and now I want to search in text and get the result sorted by count in descending order. How can I do that?

我曾经Lucene为文档编制索引,现在我想在文本中搜索并按降序按计数排序结果。我怎样才能做到这一点?

采纳答案by Salah

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. The default implementation is defined this way :

Sort 参数指定用于排序的字段或属性。默认实现是这样定义的:

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.
  • 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.

  • 您必须指定 SortField(String field, int type) 的类型参数才能让 Lucene 找到您的字段,即使这通常是可选的。
  • 排序字段必须被索引但不能被标记:

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

  • 排序字段内容只能是纯文本。如果在用于排序的字段之一中只有一个元素具有特殊字符或重音符号,则整个搜索将返回未排序的结果。

Check this tutorial.

检查本教程

回答by ProHyman

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了!

回答by Madhav

Below line will do the trick. Last parameter is boolean reverseif you set it to true it will sort in reverse order i.e. 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 NumericDocValuesFieldwhen you create your index.

此外,NumericDocValuesField在创建索引时,您还需要将启用排序的字段添加为 a 。

 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