问题
How do I group search results returned by Lucene by fields (similar to SQL Server's)?
回答1:
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
回答2:
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
回答3:
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:
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.
来源:https://stackoverflow.com/questions/342966/how-to-do-grouping-in-lucene-search-results