Search code examples
solrfull-text-searchsunspot-railskeyword-search

Keywords search with multiple terms


I would like to write something like this:

 keywords ['apple', 'peach'], :fields => [:fruits]

If I have two different instances where one has 'apple' and the other has 'peach', I want both of them to be returned in results.

I have tried syntax, that I have mentioned above, but I only get a match for first term('apple').

Does sunspot even supports this? I was not able to find that it does which does not mean that I haven't missed something.


Solution

  • Let's say you have two instances of your model, one with name apple, second one with name equal peach and third one with name 'apple peach'. If you want to get either one of them you should do something similar to the following:

    Product.search do
      minimum_match 1
      keywords 'apple peach' # btw, fulltext is an alias to keywords
    end
    

    Now you will get all three results (at least you should get), crucial part is minimum_match, without that Sunspot will use its defaults setting which is to return only 'full' matches (all phrases found).