How do I sort Lucene results by field value using

2019-02-02 08:49发布

I'm using the following code to execute a query in Lucene.Net

var collector = new GroupingHitCollector(searcher.GetIndexReader());
searcher.Search(myQuery, collector);
resultsCount = collector.Hits.Count;

How do I sort these search results based on a field?


Update

Thanks for your answer. I had tried using TopFieldDocCollector but I got an error saying, "value is too small or too large" when i passed 5000 as numHits argument value. Please suggest a valid value to pass.

标签: search lucene
4条回答
淡お忘
2楼-- · 2019-02-02 09:15

In the original (Java) version of Lucene, there is no hard restriction on the size of the the TopFieldDocCollector results. Any number greater than zero is accepted. Although memory constraints and performance degradation create a practical limit that depends on your environment, 5000 hits is trivial and shouldn't pose a problem outside of a mobile device.

Perhaps in porting Lucene, TopFieldDocCollector was modified to use something other than Lucene's "heap" implementation (called PriorityQueue, extended by FieldSortedHitQueue)—something that imposes an unreasonably small limit on the results size. If so, you might want to look at the source code for TopFieldDocCollector, and implement your own similar hit collector using a better heap implementation.

I have to ask, however, why are you trying to collect 5000 results? No user in an interactive application is going to want to see that many. I figure that users willing to look at 200 results are rare, but double it to 400 just as factor of safety. Depending on the application, limiting the result size can hamper malicious screen scrapers and mitigate denial-of-service attacks too.

查看更多
smile是对你的礼貌
3楼-- · 2019-02-02 09:15

The constructor for Sort accepting only the string field name has been depreciated. Now you have to create a sort object and pass it in as the last paramater of searcher.Search()

/* sorting by a field of type long called "size" from greatest -> smallest 
(signified by passing in true for the last isReversed paramater)*/

Sort sorter = new Sorter(new SortField("size", SortField.Type.LONG, true))
searcher.Search(myQuery, collector, sorter);
查看更多
再贱就再见
4楼-- · 2019-02-02 09:32

The search.Searcher.search method will accept a search.Sort parameter, which can be constructed as simply as:

new Sort("my_sort_field")

However, there are some limitations on which fields can be sorted on - they need to be indexed but not tokenized, and the values convertible to Strings, Floats or Integers.

Lucene in Action covers all of the details, as well as sorting by multiple fields and so on.

查看更多
放我归山
5楼-- · 2019-02-02 09:40

What you're looking for is probably TopFieldDocCollector. Use it instead of the GroupingHitCollector (what is that?), or inside it.

Comment on this if you need more info. I'll be happy to help.

查看更多
登录 后发表回答