Question

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.

Was it helpful?

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?

OTHER TIPS

I would use adjust_solr_params to use edismax with a negating clause.

Article.search do
  fulltext params[:q]
  adjust_solr_params do |solr|
    solr['defType'] = 'edismax'
    solr['q'] += %( -"some phrase to exclude")
  end
end

Not the most obvious approach, certainly not documented, and I haven't tested it, but it should work. Hopefully future versions of Sunspot will support edismax more natively.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top