是否可以在动态条件下使用Sphinx搜索?

时间:2010-05-19 09:57:12

标签: mysql full-text-search sphinx thinking-sphinx ultrasphinx

在我的网络应用中,我需要在items表格上执行以下三种类型的搜索:

  1. items.is_public = 1(使用title字段进行索引) - 可以检索到很多结果(基数远高于其他情况)

  2. items.category_id = {X}(使用title + private_notes字段进行索引) - 通常少于100个结果

  3. items.user_id = {X}(使用title + private_notes字段进行索引) - 通常少于100个结果

  4. 在所有这些情况下,我找不到让Sphinx工作的方法,但在第一种情况下效果很好。 我应该仅仅针对第一种情况使用Sphinx并在MySQL中使用普通的“慢”FULLTEXT搜索(至少因为2-3例中的基数较低)?

    或者只是我和狮身人面像可以做几乎所有事情?

1 个答案:

答案 0 :(得分:1)

如果不完全了解你的模型,我可能会遗漏一些东西,但是这是怎么回事:

class item < ActiveRecord::Base
  define_index do
    indexes :title
    indexes :private_notes
    has :is_public, :type => :boolean
    has :category_id
    has :user_id
  end
end

1)

Item.search(:conditions => {:title => "blah"}, :with => {:is_public => true})

2)

Item.search("blah", :with => {:category_id => 1})

3)

Item.search("blah", :with => {:user_id => 196})