Working with Blacklight Part 3 – Linking to Your Solr Index

We are using Blacklight to provide a search interface for a Solr index.  I expected it to be super straightforward to plug in our Solr index to the Blacklight configuration.  It wasn’t quite the case! Most of the basic features do plugin nicely, but if you use more advanced Solr features (like facet pivot) or if your solrconfig.xml differs from the Blacklight example solrconfig.xml file, then you are out of luck.  There is not currently much documentation to help you out.

SolrConfig.xml – requestDispatcher

After 3.6, Solr ships with <requestDispatcher handleSelect=”false”> in the solrconfig.xml file.  But Blacklight works with <requestDispatcher handleSelect=”true”>, and passes in the parameter qt (request handler) explicitly .  An example of a SOLR request sent by Blacklight looks like this: http://example.com:8983/solr/ltdl3test/select?q=dt:email&qt=document&wt=xml.

/select request handler should not be defined in solrconfig.xml. This allows the request dispatcher to dispatch to the request handler specified in the qt parameter. Blacklight, by default, expects a search and a document request handler (note the absence of /).

We could override the controller code for Blacklight to call our request handlers.  But a simpler solution is to update the solrconfig.xml to follow the Blacklight convention.

The ‘document’ Request Handler and id Passing

Blacklight expects there to be a document request handler defined in the solrconfig.xml file like this:

<!-- for requests to get a single document; use id=666 instead of q=id:666-->   
<requestHandler name="document" class="solr.SearchHandler">
    <lst name="defaults">
        <str name="echoParams">all</str>       
        <str name="fl">*</str>       
        <str name="rows">1</str>       
        <str name="q">{!raw f=id v=$id}</str> <!-- use id=666 instead of q=id:666 -->
    </lst>
</requestHandler>

As the comment says, Blacklight will pass in the request to SOLR in the format of id=666 instead of q=id:666.  It achieves this by using the SOLR raw query parser.  However, this only works if your unique id is a String.  In our case, the unique id is a long and passing in id=666 does not return anything in the SOLR response.

There are two ways to solve this issue.  The first is to rebuild the index and change the id type from long to String.  The other is to override solr_helper.rb to pass in q=id:xxx instead of id=xxx.  And the code snippet is below.

  require "#{Blacklight.root}/lib/blacklight/solr_helper.rb"
  module Blacklight::SolrHelper
  extend ActiveSupport::Concern

  # returns a params hash for finding a single solr document (CatalogController #show action)
  # If the id arg is nil, then the value is fetched from params[:id]
  # This method is primary called by the get_solr_response_for_doc_id method.
  def solr_doc_params(id=nil)
    id ||= params[:id]
    p = blacklight_config.default_document_solr_params.merge({
      #:id => id # this assumes the document request handler will map the 'id' param to the unique key field
      :q => "id:" + id.to_s
    })
    p[:qt] ||= 'document'

    p
  end
end

Getting Facet Pivot to work

In our index, we have a top-level facet called industry and a child facet called source that should be displayed in a hierarchical tree.    It should look something like:
Screen Shot 2014-03-04 at 2.48.23 PM

The correct configuration is in the code snippet below.

#Industry  
    config.add_facet_field 'industry', :label => 'Industry', :show => false
# Source
    config.add_facet_field 'source_facet', :label => 'Source', :show => false
#Industry -> Source
    config.add_facet_field 'industry_source_pivot_field', :label => 'Industry/Source',   :pivot => ['industry', 'source_facet']

You must add the two base fields  (Industry and Source) to the catalog_controller.rb file and set :show => false if they should not be displayed.  And it usually is the case since the data is already displayed in the pivot tree.  The current documentation on Blacklight facet pivot support makes it seem like only the last line is needed.  But if only the last line is defined, then the facet pivot will render correctly in the refine panel and it makes you think that facet pivot is working OK. But when you click on the facet, you will get an error, “undefined method ‘label’ for nil:NilClass”:

Screen Shot 2014-03-04 at 2.38.24 PM

One thought on “Working with Blacklight Part 3 – Linking to Your Solr Index”

  1. Thank you for this blog post!! We are working on setting up Blacklight to speak with our Solr index and I found this blogpost very useful!!

Comments are closed.