Negative queries with solr in multiple fields

We recently did some negative queries and had a lot of ‘fun’ with solr.
After reading/testing a bit we found a simple rule: negative queries for single words do not work (dont ask me why…), but it can be fixed with an additional *:*

Does not work: title: -xxx / (-title:xxx)

When you are only interested in certain fields, query building gets rather conplex:

  • contains foo and bar -> title:(foo bar) OR description:(foo bar)
  • contains foo or bar -> title:(foo OR bar) OR description:(foo OR bar)
  • does not contain foo or bar-> -title:(foo bar *:*) AND -description(foo bar *:*)

The *:* is killed by acts_as_solr, so the parser needs a little fix too:

# lib/parser_methods.rb:80
# *:xxx -> *:xxx a : b -> a_t:b
query = "(#{query.gsub(/([^\*]) *: */, "\\1_t:")}) #{models}"

(see our branch on github)

Hope this helps someone!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s