如何在Lucene.Net中查询最大字段值?

时间:2021-06-21 03:00:51

We want to be able to return a single document with a maximum value for a particular field. A typical example would be,

我们希望能够返回具有特定字段的最大值的单个文档。一个典型的例子是,

max(date_time_field:*)

which returns the latest document in the index.

它返回索引中的最新文档。

Is there any support for something like this in Lucene.Net?

在Lucene.Net中是否有类似的支持?

1 个解决方案

#1


3  

I'm not sure how you are querying your index, but you can certainly order by the field in question, and then simply take the top document:

我不确定你是如何查询索引的,但你当然可以通过相关字段进行排序,然后只需获取最顶层的文档:

var sortBy = new Sort(new SortField("date_time_field", SortField.DOUBLE, true));
var hits = ... IndexSearcher.Search(query, null, 1, sortBy));

...

var doc = searcher.IndexSearcher.Doc(hits.ScoreDocs[0]);

#1


3  

I'm not sure how you are querying your index, but you can certainly order by the field in question, and then simply take the top document:

我不确定你是如何查询索引的,但你当然可以通过相关字段进行排序,然后只需获取最顶层的文档:

var sortBy = new Sort(new SortField("date_time_field", SortField.DOUBLE, true));
var hits = ... IndexSearcher.Search(query, null, 1, sortBy));

...

var doc = searcher.IndexSearcher.Doc(hits.ScoreDocs[0]);