Any way to merge two queries in solr?

10,157

As Geert-Jan mentioned it in his answer, the possibility to do OR between fq is a solr asking feature, but with very little support by now: https://issues.apache.org/jira/browse/SOLR-1223

So I managed to simulate what I want to in a simple way:

  • for each field a document type can have, we have to define everytime a value (so if in my own example Books can have no category, at index time we still have to define something like category=noCategoryCode
  • when using a filter on one of this fields in a query on multiple types, we add a non-present condition in the filter, so fq=category:fiction becomes fq=category:fiction (*:* AND -category:*)

By this way, all other types (like Person) will pass through this filter, and the filter stands quite atomic and often used - so caching is still useful.

So, my full example becomes:

q = name:jean content:jean
&
fq= type:(book person)
&
fq= category:(fiction fantasy) (*:* AND -category:*)
&
fq= group:(pangolin) (*:* AND -group:*)

Still, can't wait SOLR-1223 to be patched :)

Share:
10,157
Xavier Portebois
Author by

Xavier Portebois

Updated on June 04, 2022

Comments

  • Xavier Portebois
    Xavier Portebois almost 2 years

    In my project, we use solr to index a lot of different kind of documents, by example Books and Persons, with some common fields (like the name) and some type-specific fields (like the category, or the group people belong to).

    We would like to do queries that can find both books and persons, with for each document type some filters applied. Something like:

    • find all Books and Persons with "Jean" in the name and/or content
    • but only Books from category "fiction" and "fantasy"
    • and only Persons from the group "pangolin"
    • everything sorted by score

    A very simple way to do that would be:

    q = name:jean content:jean
    &
    fq= 
        (type:book AND category:(fiction fantasy)) 
        OR 
        (type:person AND group:pangolin)
    

    But alas, as fq are cached, I'd prefer something allowing me simpler and so more reusable fq like :

    • fq=type:book,
    • fq=type:person,
    • fq=category(fiction fantasy),
    • fq=group:pangolin.

    Is there a way to tell solr to merge or combine many queries? Something like 'grouping' fq together.

    I read a bit about nested queries with _query_, but the very few documentation about it makes me think it's not the solution I'm looking for.

  • Geert-Jan
    Geert-Jan over 12 years
    since 'book' and 'person' are disjunct -> fq=type:book&fq=type:person would return 0 results.
  • Xavier Portebois
    Xavier Portebois over 12 years
    I have to agree with @Geert-Jan: my whole problem is that fq=category:(fantasy fiction) will remove all possible persons, and fq=group:pangolin will throw away all books. A possible approach would be to add a non-present-field condition, something like fq=group:pangolin (-group:[* TO *]): it would take all persons in group "pangolin" and take also all documents without the field group (so books). I was just hoping for a better way to do it.
  • Xavier Portebois
    Xavier Portebois over 12 years
    Thanks, but the problem essentially was to try something that cares about caching and performance.
  • Xodarap
    Xodarap over 12 years
    @XavierPortebois: Have you tried it? Often times the queries are not much slower than filters.
  • Xavier Portebois
    Xavier Portebois over 12 years
    In fact, I currently do not have data to do such a stress test, but I will as soon as I can :)