思考狮身人面像:单一模型的多个指数?

时间:2012-01-05 20:28:23

标签: search thinking-sphinx

我正在使用Thinking Sphinx搜索两种不同的模式:

  1. 在单个模型上搜索正常搜索功能
  2. 在所有型号中全面搜索自动完成下拉功能
  3. 为了这个问题,假设我有一个人和国家模型。

    执行常规搜索时,我想获取所有国家/地区名称与搜索字符串匹配的人。为此,我在Person索引中添加了国家名称的索引。到目前为止一直很好。

    在搜索以填充我的自动填充下拉列表时,我想显示所有国家/地区以及与我的搜索字符串匹配的所有人。这里出现了问题。在进行应用程序范围的搜索时,我现在得到:

    1. 名称与我的搜索字符串匹配的所有国家/地区
    2. 所有名字与我的搜索字符串匹配的医生,不幸的是......
    3. 所有属于与搜索字符串匹配的国家/地区的医生。
    4. 最后一部分为用户提供了一些非常令人困惑的自动完成结果。有没有简单的方法让我通过使用内置功能来避免这种情况,例如在Person模型上有两个索引,并选择哪一个用于每种搜索?

2 个答案:

答案 0 :(得分:2)

我认为你的模型如下所示:

class Person < ActiveRecord::Base
  belongs_to :country
  define_index
    indexes :name
    indexes country(:name), :as => country_name
  end
end

class Country < ActiveRecord::Base
  has_many :people # has_many :persons # depending on your singular/plural case
  define_index
    indexes :name
  end
end

因此,您可以通过执行查询来获得没有3(第三个条件)的结果:

ThinkingSphinx.search :conditions => {:name => params[:q]}, :classes => [Person, Country]

但是,如果要在模型上创建多个索引,可以像下面的示例一样完成:

class Person < ActiveRecord::Base
  belongs_to :country
  define_index :my_first_in do
    indexes :name
    indexes country(:name)
  end
  define_index :my_second_in do
    indexes :name
  end
end

答案 1 :(得分:2)

上面的答案的sphinx v3语法:

ThinkingSphinx::Index.define :country, name: "my_first_in", with: :active_record
  indexes name
end
相关问题