Skip to content

Instantly share code, notes, and snippets.

@cbeer
Created March 2, 2010 19:40
Show Gist options
  • Save cbeer/319834 to your computer and use it in GitHub Desktop.
Save cbeer/319834 to your computer and use it in GitHub Desktop.
Solr highlighting for blacklight
<% # container for all documents in index view -%>
<div id="documents">
<% # loop thru each doc -%>
<% @document_list.each_with_index do |document,counter| %>
<% # container for a single doc -%>
<div class="document">
<% # header bar for doc items in index view -%>
<div class="documentHeader yui-g">
<% # list controls for items/docs -%>
<div class="yui-u documentFunctions">
<%= render :partial=>'bookmark_control', :locals=>{:document=>document} %>
</div>
<% # main title container for doc partial view -%>
<div class="yui-u">
<h3 class="index_title"><%= counter + 1 + @response.params[:start].to_i %>. <%= link_to_document document, :label=>document_show_link_field, :counter => (counter + 1 + @response.params[:start].to_i) %></h3>
<% # here's the line to add the highlighted field: -%>
<div class="highlight"><%= field_with_highlighting(document, 'content') %></div>
</div>
</div>
<% # main container for doc partial view -%>
<%= render_document_partial document, :index %>
</div>
<% end %>
</div>
require_dependency 'vendor/plugins/blacklight/app/helpers/application_helper.rb'
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
#[...]
def field_with_highlighting document, field, sep='...'
#this is pretty ugly..
return @response['highlighting'][document.get(:id)][field].join(sep) unless @response['highlighting'][document.get(:id)].blank?
end
end
<!-- Searching ("All Fields") -->
<requestHandler name="search" class="solr.SearchHandler" default="true" >
<lst name="defaults">
[...]
<!-- Enable field highlighting using the default (pretty naive..) settings-->
<str name="hl">true</str>
<str name="hl.fl">content</str>
</lst>
[...]
</requestHandler>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment