Search code examples
ruby-on-railssolrsunspothighlightingsearchable

Highlighting a field from an owned object with sunspot_solr


I am using sunspot_solr to implement a quickfind in ruby on rails. I am fulltext searching through a number of project objects, but would also like to search through content of the owned notes as well. I have the search set up to show highlighted hit results as the user types. I can get the code to either show the highlighted results OR return hits in notes, but not both.

Code to return note results:

in Project.rb attr_accessible :title, :description has_many notes

searchable do
    text :title, :boost => 5
    text :description
    text :notes do
      notes.map(&:content)
    end
  end

In Project_controller.rb

def quick
     unless params[:q].nil? || params[:q].empty?
       @search = Project.search do
        fulltext params[:q]
       end

      @projects = @search.results
     else
      @projects = Project.none
     end

     respond_to do |format|
      format.json { render :json => @projects }
     end
   end

in Note.rb attr_accessible :content, :project_id belongs_to :project

Code to highlight hits, does not search note content: Project_controller.rb

def quick
    unless params[:q].nil? || params[:q].empty?
      @search = Project.search do
        fulltext params[:q] do
          highlight :title
          highlight :description
        end

        adjust_solr_params do |sunspot_params|
          sunspot_params[:rows] = 15
        end
      end

      @result = []
      @search.each_hit_with_result do |hit, project|
        @highlight = hit.highlight(nil)
        @field_name = @highlight.field_name
        @text = hit.highlight(nil).format { |word| "<span style='color: orange;'>#{word}</span>" }
        @result << {:text => @text, :id => project.id, :fieldname => @field_name}
      end
    else
      @result = []
    end

    respond_to do |format|
      format.json { render :json => @result }
    end
  end

Project.rb and Note.rb are essentially the same here, except I add :stored => true to the title/description of project.

However, despite trying a variety of options to full text search on the note content and return highlighted results for the note content, I can't seem to figure out how. Every time I try to add any sort of code to make the note content stored and/or highlighted, the code stops working. Is it just not possible, or am I missing the correct syntax?

Solr is on version 4.2.0, Sunspot is version 2.1.1


Solution

  • So, in searching for the solution to this, I have discovered more about solr's performance. Particularly the performance hits that you take for storing fields vs just indexing them. I am starting to think that storing fields on tables with a many-to-one relationship with the table being search may not be possible, and even if it is, would not be worth the performance hit. http://wiki.apache.org/solr/SolrPerformanceFactors

    So, while the feature to highlight text from notes in real time as you type may seem really nice to me in theory, in practice it would be too slow to be useful, so not only am I not bothering with highlighting notes, but I am probably also going to drop storing the description as well, only showing highlighted hits if they are in the title.

    So I suppose the answer to the question is "It might be possible, but definitely wouldn't be worth it even if it is."