Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex SOLR query including NOT and OR

I have some fairly complex requirements for a SOLR search that I need to execute against my database of tagged content. I need to first filter the database to those results matching my filter tags. Any of these results which have a tag from the blacklist should be removed unless they also contain tags from the whitelist.

So let's say I want to retrieve all documents tagged "forest" or "conservation". But I want to exclude documents tagged "uk" or "europe" - unless they are also tagged with "africa" or "asia". I tried to write this in my SOLR query:

tag:((forest OR conservation) AND (africa OR asia OR !uk OR !europe))

The logic seems sound to me but the query doesn't work. I have 46 documents tagged with "forest" or "conservation" - one of which is also tagged with "asia" and none of which are tagged with "uk" or "europe" but the query returns no results.

I also saw information about the fq (filtered query) parameter to SOLR which sounded like it could be useful. But this didn't seem to help either. It seems it is the combination of NOT and OR which causes SOLR to break down.

Is what I'm trying to do possible? Is my logic flawed somewhere? This is on SOLR 1.4 btw - incase this is an issue which has been resolved somewhere....

Thanks for any advice!

like image 787
vitch Avatar asked May 21 '12 16:05

vitch


1 Answers

try

tag:((forest OR conservation) AND (africa OR asia OR (*:* AND !uk AND !europe)))
like image 65
Persimmonium Avatar answered Sep 19 '22 20:09

Persimmonium