I need to return a total of 20 documents from a SOLR collection. I need 10 documents that match one field value and 10 that match a different value for the same field. Can this be done in a single SOLR query?
Example (multiple lines for readability):
http://myhost/mycollection?
fq=is_deleted:false AND vehicle_type:(car OR truck)
&q=vehicle_name:(*blue* *driver*)
&fl=vehicle_name,vehicle_type
&count=20
&start=0
&sort=score desc
The problem is I might get 20 cars and 0 trucks, depending on which docs rank higher and how many docs there are of each type. What I need is a max of 10 cars and 10 trucks.
In SQL, you could accomplish this in a single query by writing two separate queries (one for 10 trucks and one for 10 cars), then combine the results with a UNION statement.
Is there any way to accomplish this in SOLR?
What I have tried is the above query, but I often get 20 docs of one type and none of the other type.
I am also looking into SOLR facets and SOLR grouping to see if I might be able to use one of them to accomplish this.
Related questions (with no satisfactory answer):
- Usage of UNION and MINUS in SOLR
- How to combine results from multiple solr queries (much like UNION on SQL's)?
- Any way to merge two queries in solr?
- SOLR Union of two queries
Related question (with similar answer)
Solution
(based on MatsLindh's suggestions below) I used group.field instead of group.query and I added group.main which instructs solr to return a flat list of results.
http://myhost/mycollection?
fq=is_deleted:false AND vehicle_type:(car OR truck)
&q=vehicle_name:(*blue* *driver*)
&fl=vehicle_name,vehicle_type
&count=20
&start=0
&sort=score desc
&group=true
&group.field=vehicle_type
&group.limit=10
&group.main=true
&group.sort=score desc
References