0

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):

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

  • `group=true&group.query=vehicle_type:car&group.query=vehicle_type:truck&group.limit=10` might do what you want, `group.format=simple` might also be useful, but I'm not sure if `group.limit` is still respected per group - test it. :-) – MatsLindh Mar 22 '23 at 22:37
  • Thank you for the response @MatsLindh, I'm experimenting with the grouping options now! – 5DimentionalObject Mar 23 '23 at 01:09
  • @MatsLindh Using the example you provided, I have been able to get this to work properly. Can you add it as a answer to the question and I will accept it as the best answer? – 5DimentionalObject Mar 23 '23 at 14:57

0 Answers0