Noz
Noz

Reputation: 6346

Rails 3 Searching HTML Content with Sunspot

I'm storing some HTML content in my database and I would like to be able to perform a search using Sunspot while omitting HTML from the hit output and if possible the search itself.

My model:

class Article < ActiveRecord::Base

  attr_accessible :caption
  searchable do
    text :content, :stored => true
  end

end

Search action:

def find    
  @search = Article.search do
    fulltext params[:search] do
      highlight :name
    end
  end      
end

Template:

- @search.each_hit_with_result do |hit, article|
  - unless hit.highlight(:content).nil?
      %p= hit.highlight(:content).format { |word| "<span class=\"highlight\">#{strip_tags(word)}</span>"}.html_safe

Some sample content that is being search might be something like:

<h1>Hello world</h1>
<p> Search for me me!</p>
<a href="#">Link</a>

Notice that I'm marking the output as html_safe? This is because I would like the wrap the search text that gets hit with a highlight span, however besides that everything else I want to be stripped completely from the returned text that gets hit. Is this even possible?

Upvotes: 1

Views: 269

Answers (1)

Noz
Noz

Reputation: 6346

What ended up working for me was stripping the content that gets indexed by solr. To do so I had to make the following change inside of my model:

  include ActionView::Helpers::SanitizeHelper

  searchable do
    text :content, :stored => true do
      strip_tags(content)
    end
  end

Adding those changes and running rake sunspot:solr:reindex worked like a charm!

Upvotes: 3

Related Questions