Skip to content

Lesson: make blacklight return search results

E. Lynette Rayle edited this page Sep 22, 2015 · 11 revisions

Goals

  • (for now) Turn off access controls for Blacklight-based searches and "show" views
  • Tell Blacklight which fields to use in searches
  • Run a search in Blacklight and see results rendered

Explanation

In Lesson: Build models with Hydra-Works you defined an object model and stored a new digital object in your repository. We saved the objects and saw that their metadata was indexed in Solr. Now we will make your repository objects appear in your Blacklight searches.

One of the main features that Hydra adds to Blacklight is the ability to control who has access to which information in search results. That topic gets a little bit complicated. For the purpose of this Tutorial we want to stay focused on showing you how to set up an app, define models and create objects based on those models, so in order to keep things simple we will make this Hydra Head behave like an open access repository where everyone can see everything. Once you've completed this tutorial, you can check out Access Controls with Hydra to learn how to assert access controls on objects and enforce those access controls in a Hydra Head.

Once you've turned off access controls in step #1, we show you how to tell blacklight which fields you want to use for default searches in the remaining steps.

Steps

Step 1: Comment out the lines that enforce access controls in Blacklight's CatalogController

If you open app/controllers/catalog_controller.rb and look at the code near lines 8-12 you should see this:

  # These before_filters apply the hydra access controls
  before_filter :enforce_show_permissions, :only=>:show
  # This applies appropriate access controls to all solr queries
  CatalogController.solr_search_params_logic += [:add_access_controls_to_solr_params]

This code tells blacklight to enforce access controls on the search and result view pages. For the time being we will turn this off by commenting out two lines so that it looks like this:

  # These before_filters apply the hydra access controls
  #before_filter :enforce_show_permissions, :only=>:show
  # This applies appropriate access controls to all solr queries
  #CatalogController.solr_search_params_logic += [:add_access_controls_to_solr_params]

Then, save the file.

Step 2: Run a search in the CatalogController

Visit or reload the page at http://localhost:3000/. You should see the Blacklight search interface with a search box. If you just hit enter on the blank search box (effectively asking blacklight to return all objects) you should get a response with the objects you've created using the rails console. If you search for parts of the title or author, you will find your bibliographic resource. HOWEVER If you search for terms only appearing in your abstract, you don't see any results. This happens even though you just saw results when you submitted for the unconstrained (blank) search, and the title and author searches. This is because we haven't told Blacklight which fields to search against and Blacklight does not know which Hydra fields to search against, except for a few defaults (e.g. title, author, subject) configured in catalog_controller.rb.

Step 3: Get rid of infrastructure objects

When you left the search box empty and pressed return, you saw your bibliographic resource, your collection, and a few other results. Fedora keeps in-between objects for containers like the Collection. For each member in the collection, it also keeps a proxy. We don't want to display these infrastructure objects to users in the search results. We can use filters to prevent their return in the result set from solr.

If you open app/controllers/catalog_controller.rb and look at the code near lines 15 you should see this:

    config.default_solr_params = {
      :qt => 'search',
      :rows => 10
    }

This code modifies the search parameters defined in solrconfig.xml, allowing you to customize the parameters Blacklight uses when searching. We'll add a filter (fq) to prevent the in-between infrastructure objects from appearing in search results.

    config.default_solr_params = {
      :fq => '-has_model_ssim:"ActiveFedora::IndirectContainer"-has_model_ssim:"ActiveFedora::Aggregation::Proxy"',
      :qt => 'search',
      :rows => 10
    }

Save catalog_controller.rb and verify that you no longer see the infrastructure objects. Visit or reload the page at http://localhost:3000/. Delete all text in the search box and hit return. You should see only the collection, bibliographic resource work and bibliographic resource file listed in the search results.

Step 4: Tell Blacklight which fields to use in Queries

In general, we always need to tell Blacklight which fields to search in. Let's do that by setting the default 'qf' solr parameter. Open app/controllers/catalog_controller.rb and set the default_solr_params section (around line 15) to this:

    config.default_solr_params = {
      :qf => 'title_tesim author_tesim abstract_tesim',
      :fq => '-has_model_ssim:"ActiveFedora::IndirectContainer"-has_model_ssim:"ActiveFedora::Aggregation::Proxy"',
      :qt => 'search',
      :rows => 10
    }

Save catalog_controller.rb and verify that you can now search the abstract in addition to title and author. Visit or reload the page at http://localhost:3000/. Type 'greatest novel' into the search box and hit return. You should see the bibliographic resource listed in the search results.

Step 5: View the bibliographic resource work & add abstract as a display field

If you click on the bibliographic resource title to view the details of the bibliographic resource (http://localhost:3000/catalog/work-1), you will see Title and Author displayed, but not Abstract. To include Abstract in the set of displayed fields, open app/controllers/catalog_controller.rb and add the following line to the section controlling display fields (starting around line 76). Look for lines beginning with config.add_show_field solr_name. Append the following line to the end of the set of show fields.

    config.add_show_field solr_name('abstract', :stored_searchable, type: :string), :label => 'Abstract:'

Save catalog_controller.rb and verify that you can now see the abstract field in addition to title and author in the detail page for the bibliographic resource. Visit or reload the page at http://localhost:3000/catalog/work-1. You should see the bibliographic resource with title, author, and abstract.

Step 6: Commit your changes

Now that we've updated our search functionality, it's a great time to commit to git:

git add .
git commit -m "Disabled access controls and set default search fields"

Next Step

This completes the basic tutorial. Go on to BONUS Lesson: Adding pages to a bibliographic resource work or explore other [Dive into Hydra-Works](Dive into Hydra-Works#Bonus) tutorial bonus lessons.