思考狮身人面像:多指数搜索

时间:2017-12-12 12:21:10

标签: thinking-sphinx

我想为同一个模型创建两个索引并单独搜索

我正在使用

gem 'thinking-sphinx', '3.2.0'
gem 'riddle', '1.5.11'
ThinkingSphinx::Index.define :product, :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
    indexes :field_a
end

ThinkingSphinx::Index.define :product, :name => "active_product", :with => :active_record, :delta => ThinkingSphinx::Deltas::DelayedDelta do
    indexes :field_a
    where "(active = 1)"
end

当我试图搜索这种方式只获得活跃的产品

Product.search_for_ids "", :match_mode => :extended, :index => "active_product_core, active_product_delta", :page => params[:page], :per_page => 50, :sort_mode => :extended, :order => "field_a desc"

但它正在运行这样的查询并列出所有产品

 SELECT * FROM `product_core`, `product_delta` WHERE `sphinx_deleted` = 0 ORDER BY `field_a` desc LIMIT 0, 50 OPTION max_matches=50000

我如何只获取有效产品或确保查询运行如此?

SELECT * FROM `active_product_core`, `active_product_delta` WHERE `sphinx_deleted` = 0 ORDER BY `field_a` desc LIMIT 0, 50 OPTION max_matches=50000

注意:上述功能在Thinking sphinx version 2中运行良好

gem 'thinking-sphinx', '2.0.14'
gem 'riddle', '1.5.3'

1 个答案:

答案 0 :(得分:0)

在TS v3中,搜索选项现在是:indices而不是:index,并且需要一个索引名称数组。所以,请尝试以下方法:

Product.search_for_ids(
  :indices  => ["active_product_core", "active_product_delta"],
  :page     => params[:page],
  :per_page => 50,
  :order    => "field_a desc"
)

我已从您使用的选项中删除:sort_mode:match_mode - extended方法是Sphinx的SphinxQL协议可用的唯一方法(和这就是TS v3使用的内容),因此您无需指定它们。