Search code examples
ruby-on-railsrubysolrsunspot-solrwebsolr

sunspot-solr: How to exclude search results that include any given word?


I have a Ruby on Rails application which uses solr. I use sunspot gem for that. I also use websolr for the production environment.

I know we can do fulltext searching with sunspot solr. But I also want to be able to exclude any hit (and eventually result) that has a given word.

Example class:

class Article
    searchable do
        text :title
        text :content
    end
end

Now, I want to search for the articles that include "Obama" in its content but also want to exclude ones that also have the word "Lady Gaga". I know I can apply reject block on returned array of results but I want to handle this situation on Solr level.

Hope I stated my problem clearly.

Thanks in advance for your help.


Solution

  • From https://github.com/sunspot/sunspot/wiki/Fulltext-search

    A small subset of the normal boolean query syntax is parsed: in particular, well-matched quotation marks can be used to demarcate phrases, and the + and - operators can be used to require or exclude words respectively

    Is that what you were looking for?