Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Connecting multiple Filter Queries (fq) in Apache Solr

Let’s assume, I have the following documents in my index:

title: Entry #1
myfield: 5
---
title: Entry #2
myfield: 2
---
title: Entry #3

As you can see, myfield is optional and not present in all documents.
Now I want to select all documents where myfield is greater than 3 or the field does not exist. Of course there is also a search word let’s say entry, so it finds all documents.
So the query should return Entry #1 and Entry #3.

Currently I am querying like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

q=entry
defType=dismax
qf=title
fl=*
fq=-myfield:* myfield:[3 TO *]

which does not return any documents. Each filter query alone, so -myfield:* and myfield:[3 TO *] are working like expected.
How can I connect these two filter queries?

>Solution :

It’s usually helpful to think of each term of the query as a set of documents, and the boolean operators as operations performed between these sets (i.e. AND finds the intersection between two sets, while OR finds the union). A negative set would then be the difference between sets.

When you use a negative match, you have to subtract it from something. When you don’t have any other clauses in your query, Solr helpfully appends the complete set of documents (*:*) in front of your query. But as soon as you add a second boolean term, Solr can’t do that any longer – since it doesn’t know what you actually mean with your query.

So your negative clause needs to start with a set that the other set (i.e. the documents that doesn’t have field) can be subtracted from:

fq=(*:* -myfield:*) OR myfield:[3 TO *]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading