Search code examples
ruby-on-railsrubysphinxthinking-sphinx

Better Thinking Sphinx Results


I have recently started playing around with the thinking sphinx gem and have been having great success with running searches, however I have noticed that for some reason, certain searches aren't returning the correct records.

config/thinking_sphinx.yml

development:
  mysql41: 3313
  morphology: stem_en

indicies/product_index.rb

indexes :title

lib/tasks/my_rake_task.rake

term = "Garmin Edge 500 GPS Enabled Computer"
@product = Product.search Riddle::Query.escape(term), ranker: :none 

The rake task should match the title in the database of 'Garmin Edge 500 GPS Cycle Computer' however for some reason it returns an empty array. When I remove the term 'Enabled' it returns the record.

I was just wondering if there is a simple way to improve the accuracy of a search?


Solution

  • Hi Paul (just replied to the TS list, but here's my response again, with examples):

    What you're trying to do (match some but not necessarily all words) is a little tricky, but here's a couple of things to note:

    If you want to match on any word, rather than all, you can insert |'s between each word (| is Sphinx's OR). There is also the quorum operator, but that may be harder, given you don't have control over how many terms the user may enter. http://sphinxsearch.com/docs/current.html#extended-syntax

    # Using ORs
    Product.search "Garmin | Edge | 500 | GPS | Enabled | Computer"
    # Using quorum
    Product.search '"Garmin Edge 500 GPS Enabled Computer"/4'
    

    Either way, you'll be slicing up the user-provided queries to count or split words, which is slightly annoying, but perhaps worthwhile.

    Hope this helps.