How to do grouping in Lucene search results?

2019-04-09 09:32发布

How do I group search results returned by Lucene by fields (similar to SQL Server's)?

标签: lucene
4条回答
够拽才男人
2楼-- · 2019-04-09 09:48

It's not clear whether you want a true SQL-like "GROUP BY" behavior, or merely an "ORDER BY" behavior. There's nothing like aggregation functions in Lucene, so "GROUP BY" would have to implemented in your application, on top of Lucene.

However, sorting by fields is fairly easy. Make sure the desired field is indexed, and create a org.apache.lucene.search.Sort object to be passed as part of the search criteria; most search methods have an overload that accepts a Sort instance.

If you were to implement your own "GROUP BY" logic, having the results "ORDERED BY" the correct fields is a helpful first step.

查看更多
相关推荐>>
3楼-- · 2019-04-09 09:49

Lucene has some suplimentary libraries, one being what you need: Grouping

You can't group by function query values nor by arbitrary queries(like Solr does), but you can by single-valued field.

Something similar(faceted search) will be implemented in Lucene 4.0

查看更多
我只想做你的唯一
4楼-- · 2019-04-09 10:05

https://issues.apache.org/jira/browse/LUCENE-1421

it appears that you cant. there is possibly a workaround though: theres a thread here which outlines how someone else has done it : here

查看更多
乱世女痞
5楼-- · 2019-04-09 10:14

Lucene 3.4 now supports faceted search. At indexing you specify something supplementary and at search time you search by query and by groups.

for next 3 docs, that you index with these groups

doc1: monday, 1pm,  3min    
doc2: monday, 1pm,  4min    
doc3: monday, 2pm,  3min

you can search only for the first param: monday, and get value:3, or you can drill down and search for monday/1pm and get value:2 or set depth of search 3 and get

monday :3
monday/1pm :2
monday/1pm/3min :1
monday/1pm/4min :1
monday/2pm :1
monday/2pm/3min :1

here's the source sample :

But most of all readfaceted search

查看更多
登录 后发表回答