Skip to content

Instantly share code, notes, and snippets.

@zog
Created December 21, 2010 14:56
Show Gist options
  • Save zog/750004 to your computer and use it in GitHub Desktop.
Save zog/750004 to your computer and use it in GitHub Desktop.
In order to use highlights w/ Sunspot
# in the controller (or wherever you perform the search), enable highlights
@search = Sunspot.search(Model) do
keyword 'foo', :highlights => true
end
# or
@search = Sunspot.search(Model) do
keyword 'foo', :highlights => :name
end
# or
@search = Sunspot.search(Model) do
keyword 'foo' do
highlight :name
end
end
p r.hits.first.highlights
#=> [#<Sunspot::Search::Highlight:0x4422774 @highlight="@@@hl@@@Foo@@@endhl@@@ Bar", @field_name=:name>]
p r.hits.first.highlight(:name)
#=> #<Sunspot::Search::Highlight:0x4422774 @highlight="@@@hl@@@Foo@@@endhl@@@ Bar", @field_name=:name>
p r.hits.first.highlight(:name).format{ |word| "<strong>#{word}</strong>" }
#=> "<strong>Foo</strong> Bar"
# In your model, define text fields with ":stored => true":
class Model < < ActiveRecord::Base
searchable do
text :name, :stored => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment