Thinking_sphinx“starts_with”查询

时间:2011-05-19 15:35:15

标签: ruby-on-rails thinking-sphinx

我需要thinking_sphinx查询来检索“starts_with”值。 意思是如果我给Student.search'a',我想显示所有名字以a开头的学生。 我已经将名称字段编入索引。现在,要检索学生,我必须给出确切的名称。

1 个答案:

答案 0 :(得分:1)

听起来像你想要wildcard searches。将其添加到您的config/sphinx.yml文件中 - 如果您还没有,请创建它:

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

或者您可以将其放在特定索引中 - 因为中缀/前缀设置会显着增加索引的大小:

define_index do
  # ...

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

然后,运行rake ts:rebuild以便Sphinx知道更改并在索引中处理,然后您可以像这样搜索:

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

如果你使用min_infix_len而不是min_prefix_len,你也可以匹配中的字样 - 也就是说,将星号放在两边:

Student.search '*a*'

最后 - 如果您总是希望您的查询在每个字词的每一端都有通配符,请在搜索中使用:star => true

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

希望这可以帮助您获得所需的结果:)