Search code examples
solrlucenesolrj

(Lucene/SOLR) Can I annotate the results of a query based on grouping by subqueries?


I would like to group the results of any query in terms of "categories".

"Categories" are keyword queries, they cannot be pre-defined at index time, since they evolve and change over time.

More specifically:

I have a set of categories defined by queries q1,q2,...qN.

Given a user query (q), I need to return the top resulting docs (d1,...d10) as usual,

but I need to know if they belong or not to each of the groups q1,...qN.

As I understand it I could use grouping with queries, but this has two drawbacks:

  1. I will change the results, since instead of d1,...d10 I will get top docs for each query
  2. I will loose the original ordering of the results

The only solution I can think of right now is to issue first q to get the results and ordering, then each of q AND q1, q AND q2, etc. to get the grouping, then parse all the results and group outside the query... expensive!

Any ideas how can I get what I need?


Solution

  • You can use the normal way to do the query, and then add pseudo-fields in fl param that matches the clipping against your categories using function queries.

    http://solr.pl/en/2011/11/22/solr-4-0-new-fl-parameter-functionalities-first-look/ https://cwiki.apache.org/confluence/display/solr/Function+Queries

    Example:

    fl=category1:sum(0.0, query($q1))

    q1={!dismax}your query 1

    fl=category2:sum(0.0, query($q2))

    q2={!dismax}your query 2