Search code examples
ruby-on-railssphinxthinking-sphinx

Good search term in Sphinx


Using Rails 3.2, Sphinx Search and Thinking Sphinx gem. I have tried wrapping % and *, and wildcard, in the keywords in my controller, but can't achieve what I want:

Keywords to search:

  • world trade
  • worldtrade
  • worl trade
  • trade world
  • trad worl

Expected matched search results:

  • World Trade Center

How should I format the keywords in my controller so that I get the expected search result with the different keywords as shown above?


Solution

    • For Sphinx, the wildcard character is * - so there's no point using %.
    • If you want every query to have wildcards added to it, use :star => true in your Thinking Sphinx search call.
    • If you want to match on parts of words, you need to enable either min_prefix_len or min_infix_len. The default is 0 (disabled), but given your examples, perhaps 4 is a good setting? The lower it is, the larger your index files get (and so searches may get slower as well), so perhaps don't set it to 1 unless you need to.
    • All of the above points won't make 'worldtrade' match, because you're providing one long word instead of two separate ones. You could look into adding a wordforms file to your Sphinx configuration, but that may be overkill in this case, as you'd want to cover each specific case.