Created
July 28, 2009 20:56
-
-
Save marceldegraaf/157661 to your computer and use it in GitHub Desktop.
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 lib/query/base_query.rb | |
# | |
def to_params | |
params = {} | |
if @keywords | |
params[:q] = @keywords | |
params[:fl] = '* score' | |
params[:fq] = types_phrase | |
params[:qf] = text_field_names.join(' ') | |
params[:defType] = 'dismax' | |
params[:hl] = 'on' # I added this line to enable highlighting in Solr | |
else | |
params[:q] = types_phrase | |
end | |
params | |
end | |
# In lib/sunspot/search/search.rb | |
# | |
# | |
# Get the search result highlights from Solr. Highlighting can be turned on by setting | |
# hl = true in the query parameters of the keyword search. | |
# Solr returns an XML-node <highlighting> wich contains a separate node for each object that was found | |
# during the keyword search. These separate nodes contain the highlighted keyword on which basis the object | |
# was found. | |
# | |
# === Returns | |
# | |
# Hash:: A hash of Arrays, each consisting of the document ID ('Movie 104') and the highlighted text | |
# | |
def highlights | |
highlights = {} | |
@solr_result['highlighting'].map{|hit| highlights.merge!({hit.first => hit.last['information_text']})} | |
highlights | |
end | |
# In my app's ApplicationHelper | |
# | |
def search_highlight(search, object) | |
id = "#{object.class} #{object.id}" | |
if search.respond_to?(:highlights) && search.highlights[id] | |
search.highlights[id] | |
end | |
end | |
# This way I can do this in a view: | |
# | |
- @search.results.each do |result| | |
... | |
%p= search_highlight(@search, result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment