Skip to content

Lesson: make blacklight return search results

E. Lynette Rayle edited this page Jun 13, 2016 · 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: Define models with Hydra-Works you defined object models and in Lesson: Create instances of Hydra-Works models stored a new digital object in your repository. In Lesson: Explore Objects in Fedora and Solr, you extended the set of fields being indexed 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: Explicitly disable the code that enforces 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

The first line tells blacklight to enforce access controls on result view pages. For the time being we also need to disable access controls on the query side by adding a line to explicitly remove [-=] access controls in the SearchBuilder default_processor_chain, the code should look 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
  Hydra::SearchBuilder.default_processor_chain -= [:add_access_controls_to_solr_params]

Then, save the file.

Step 2: Run a search in the CatalogController

  • Start the server with rails s
  • Visit or reload the page at http://localhost:3000/. NOTE: You should see the Blacklight search interface with a search box.
  • 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.

  • Search for part of the title (e.g. raven). You will find your bibliographic resource.
  • Search for part of the author (e.g. poe). You will find your bibliographic resource.
  • Search for part of the abstract (e.g. sorrow). You will see No entries found message.

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 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: Tell Blacklight which fields to use in Queries

Let's make abstract searchable by setting the default 'qf' solr parameter. Open app/controllers/catalog_controller.rb and set the default_solr_params section (around line 23) to this:

    config.default_solr_params = {
      :qf => 'title_tesim author_tesim abstract_tesim',
      :qt => 'search',
      :rows => 10
    }

Save catalog_controller.rb and verify that you can now search the abstract in addition to title and author and test searching the abstract data.

  • Save catalog_controller.rb
  • Restart the server (<CTRL><C> and rails s)
  • Visit or reload the page at http://localhost:3000/.
  • Type sorrow into the search box and hit return.
  • Verify the search results list includes The Raven.

Step 4: Get rid of infrastructure objects

When you left the search box empty and pressed return, you see your collection, bibliographic work, and your bibliographic file set, and a few other results.

screen shot 2015-12-20

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 23 you should see this:

    config.default_solr_params = {
      :qf => 'title_tesim author_tesim abstract_tesim',
      :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"',
      :qf => 'title_tesim author_tesim abstract_tesim',
      :qt => 'search',
      :rows => 10
    }

Save catalog_controller.rb and verify that you no longer see the infrastructure objects. Restart the server (<CTRL><C> to stop and rails s to start). Visit or reload the page at http://localhost:3000/. Delete all text in the search box and hit return. You should see only the collections, works, and file sets listed in the search results.

screen shot 2015-12-20

NOTE: You will also see col-2, work-2, and fileset-2 if you created those objects.

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

If you click on the bibliographic work's title to view the details of the work (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
  • Restart the server (<CTRL><C> and rails s)
  • Visit or reload the page at http://localhost:3000/catalog/work-1.
  • Verify that you can now see the abstract field in addition to title and author in the detail page for the bibliographic resource.

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: Add pages to a bibliographic work or explore other [Dive into Hydra-Works](Dive into Hydra-Works#Bonus) tutorial bonus lessons.

Clone this wiki locally