开发者

Any way to merge two queries in solr?

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 cat开发者_运维技巧egory "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.


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


You can apply multiple filter queries at the same time

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


Perhaps I am not understanding your issue, but the only difference between a query and a filter is that the filter is cached. If you don't care about the caching, just modify their query:

real query +((type:book category:fiction) (type:person group:pangolin))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜