shajin
shajin

Reputation: 3264

Thinking_sphinx "begins_with" query

I need thinking_sphinx query to retrieve "begins_with" values. means if I give Student.search 'a', I want to display all student who have name starts with a. I have indexed the name field already.Now, to retrieve a student, I have to give the exact name.

Upvotes: 0

Views: 154

Answers (1)

pat
pat

Reputation: 16226

Sounds like you want wildcard searches. Either add this to your config/sphinx.yml file - or create it if you don't already have one:

development:
  enable_star: 1
  min_prefix_len: 1
# repeat for other environments

Or you can put it in a specific index - as infix/prefix settings increase the size of your indices dramatically:

define_index do
  # ...

  set_property :enable_star => 1
  set_property :min_prefix_len => 1
end

And then, run rake ts:rebuild so the changes are known by Sphinx and processed in the indices, and then you can search like this:

Student.search 'a*'
# or
Student.search :conditions => {:name => 'a*'}

And if you use min_infix_len instead of min_prefix_len, you can match within words as well - that is, put the star on either side:

Student.search '*a*'

Finally - if you always want your queries to have wildcard stars on each end of every term, use :star => true in your searches:

Student.search 'a b c', :star => true
# is the same as
Student.search '*a* *b* *c*'

Hope this helps you get the result you're looking for :)

Upvotes: 1

Related Questions