Created
December 21, 2010 14:56
-
-
Save zog/750004 to your computer and use it in GitHub Desktop.
In order to use highlights w/ Sunspot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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