Search code examples
ruby-on-railsrubysolrfull-text-searchsunspot

solr sunspot fulltext search based on boolean field


I have two columns in my db field1 and field2. I want to do fulltext search based on another field.

I need below scenario

if field2.nil?
 fulltext search with field1
else
 fulltext search with field2
end

model.rb

searchable do 

 text :field1
 text :field2

  boolean :check_fields do
   self.field2.nil? ? false : true
  end
end

controller.rb

Model.search do     
 fulltext keyword, :fields => :field1 if :check_fields

 fulltext keyword, :fields => :field2 if :check_fields
end

I'm not getting expected results. How can I achieve condition based fulltext search.

I even tried "any do" between this two fulltext.

Can you guys please help me in getting output


Solution

  • This solved my problem I could directly write my condition in model.

    model.rb

    searchable do 
     text :field1
     text :searchable_field do
      field2.nil? ? field1 : field2
     end 
    end
    

    controller.rb

    Model.search do     
     fulltext keyword, :fields => :searchable_field
    end