Superq
Superq

Reputation: 153

Conditional indexing in rails sunspot

I am well versed with Solr and have used it previously using DIH. In the current application that I am working on my Solr document would consist of fields from 3 tables (because of RDBMS design of the db).

Questions: This is the main table, with the id (question id) being the foreign key to all other tables

Questions and topic mapping table: Maps multiple topics to single question.

Answers: Each question can have multiple answers and these are stored in this table with the foreign key from questions table.

  1. Each of these three tables have their model files in the app, however in Solr I would want a single document (per record of the main driving table) consisting of fields from the various tables.
  2. I also want to add a record (a question) only if it's status in the db is set to 'active'. Is it possible to add this condition while indexing with sunspot?

Can these two requirements be met with sunspot? If not, is there some other extension available for rails with Solr where I can configure solr separately (through DIH, schema.xml etc.) and use it with functions available in the extension?

Upvotes: 1

Views: 695

Answers (1)

Ruy Diaz
Ruy Diaz

Reputation: 3122

I haven't tested this code, but maybe it will get you in the right direction. I know the :if will definitely work, what I'm not sure of is the use of multiple on a text field.

class Question < ActiveRecord::Base
  has_many :topics
  has_many :answers

  searchable :if => active? do
    text :question_text

    text :topics, :multiple => true do
      topics.pluck(&:topic_text)
    end
    # Not sure if `multiple` works with text fields, so alternatively:
    #text :topics do
    #  topics.pluck(&:topic_text).join()
    #end

    text :answers, :multiple => true do
      answers.pluck(&:answer_text)
    end
  end
end

Upvotes: 1

Related Questions