Search code examples
ruby-on-railsrubysphinxthinking-sphinx

How to index only specific (based on value of attribute) records in Thinking Sphinx


Let's assume that we have a table users.

CREATE TABLE users
(
  id    INT,
  guest BOOL,
  name  VARCHAR(255)
)

I would like to index using Thinking Sphinx only records with guest equal to false. At this moment I have:

ThinkingSphinx::Index.define :user, with: :active_record do
  indexes :name
end

Thanks.


Solution

  • Index definitions have a where method to apply conditions to the generated SQL for indices:

    ThinkingSphinx::Index.define :user, with: :active_record do
      indexes name
    
      where "users.guest = FALSE"
    end