Conditional indexing in rails sunspot

188 views
Skip to first unread message

varun...@gmail.com

unread,
May 5, 2013, 1:40:47 PM5/5/13
to ruby-s...@googlegroups.com

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?

Mark Weston

unread,
May 6, 2013, 9:27:47 AM5/6/13
to ruby-s...@googlegroups.com
I was going to reply inline but the email formatting kind of exploded.  Apologies for top posting.
(I've made up my own attribute names, assuming that each Question has a #text attribute (containing the text of the question, duh) as does each Answer, and each Topic has a #name)

class Question < ActiveRecord::Base

has_many :topics
has_many :answers

searchable, :if => :active? do     # question 2
  text :text  # question 1 (option a)
  string :topics, :multiple => true do   # question 1 (option b)
    topics.map {|topic| topic.name}
  end
  text :answers do   
    answers.map {|answer| answer.text}
  end
end

def active?
  return status == 'active'
end
end

The result is one Solr document indexed per Question in the questions table for which self.active? is true.

I've chosen to guess that I might want full text searching on the answer texts and have therefore concatenated all the answers for each question into a single text field, but I only want to scope results by topic name and therefore indexed them as a multiple string field.  Your choices here are obviously going to depend on how you might want to query the index.

You've got a lot of flexibility in the searchable block.  You can simply assign an attribute of the model to a field in the indexed document (as in option a), or pass it a block that returns a value to be indexed (as in option b).  Or the most flexible option will look like a) but the 'attribute' will be a method doing something more complicated in order to return the value to be indexed.  For example

...
searchable do
  text :question_and_answers
end

def question_and_answers
  result = []
  result << self.text
  result += answers.map {|answer| answer.text}
  return result
end

Hope that helps.

Mark


--
You received this message because you are subscribed to the Google Groups "Sunspot" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-sunspot...@googlegroups.com.
To post to this group, send email to ruby-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-sunspot?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages