Search code examples
thinking-sphinx

Thinking Sphinx : Multiple indices search


I want to create two indices for the same model and search separately

I am using

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

when i tried to search this way to get only the active products

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"

But it is running query like this and listing all products

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

How can i get only the active products or to make sure query runs like this?

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

Note: Above feature was working fine in Thinking sphinx version 2

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

Solution

  • In TS v3, the search option is now :indices rather than :index, and expects an array of index names. So, try the following:

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

    I've removed :sort_mode and :match_mode from the options you were using - the extended approaches are the only approaches available with Sphinx's SphinxQL protocol (and that's what TS v3 uses), so you don't need to specify them.