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:

define_index do
  set_property :enable_star => true
  set_property :min_prefix_len => 3

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”

  1. pat Says:

    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.

  2. Ryan Says:

    @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:

      enable_star: 1
      min_prefix_len: 2
  3. faisal ejaz Says:

    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.

  4. Ben Koski Says:

    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.

Sorry, comments are closed for this article.