Wildcard (*) Searches With Thinking Sphinx
October 24th, 2008
Ryan Bates has a helpful screencast about the using the excellent Thinking Sphinx Rails plugin for the Sphinx search engine.
There seems to be a bit of confusion over how to enable wildcard (star) searches with Thinking Sphinx. Wildcard searches would let you search for partial words, so “thin*” would match “Thinking Sphinx” and “Thinner”. This is pretty useful, and for Donor Tools it is essential.
Turns out it’s really easy to turn on wildcard search. There is no need to make any changes to your Sphinx setup or add a config/sphinx.yml
file. In your define_index
block, simply add enable_star
and min_prefix_len
like so:
1 2 3 4 5 |
define_index do ... set_property :enable_star => true set_property :min_prefix_len => 3 end |
enable_star
simply turns on wildcard searching. However, this won’t do much good unless you also enable prefix indexing using min_prefix_len
.
min_prefix_len
sets the minimum number of letters that Sphinx will index. From the Sphinx docs:
...indexing a keyword “example” with min_prefix_len=3 will result in indexing “exa”, “exam”, “examp”, “exampl” prefixes along with the word itself.
You can also set min_infix_len
, which does the same thing as min_prefix_len
, except it does it on the middle of the word.
Infix indexing allows to implement wildcard searching by ‘start*’, ’*end’, and ’*middle*’ wildcards.
Caution: Infix indexing can cause your index to grow, and may slow down searching.
Now, re-run rake thinking_sphinx:configure
, re-index (rake ts:in
), and restart the Sphinx daemon (rake ts:run
), and it should work.
4 Responses to “Wildcard (*) Searches With Thinking Sphinx”
Sorry, comments are closed for this article.
October 24th, 2008 at 07:44 AM
Hey Ryan
Thanks for the clear write-up on wildcard searching – it’s definitely one of the areas needing some clearer explanations.
Just one thing though: you can put those two settings in sphinx.yml instead – which means they apply across all indexed models. Of course, given the overhead, that’s not always ideal.
October 24th, 2008 at 09:55 AM
@pat – yes, you can put those settings in sphinx.yml. For some reason last night I simply could not get that to work. This morning it works fine. Aargh! Minus one for late-night coding. Here’s a sample sphinx.yml file using that method:
November 12th, 2008 at 03:50 AM
Hey Ryan i m changing my project search from ferret to thinking sphinx, as ferret gives all data against * , thinking sphinx is not working for *,and how OR condition is handle is thinking sphinx condition clause. kindly give me solution.
January 14th, 2009 at 11:26 AM
Maybe this is version-specific, but on my install of ThinkingSphinx 1.1.2, I needed
set_property :enable_star => 1
With enable_star set to true, ThinkingSphinx spits out “enable_star = true” in the sphinx.conf—which Sphinx itself doesn’t seem to recognize.