Add Metadata to search results and facets - RepoCamp/connect2017 GitHub Wiki
Metadata Part 3 Goals:
Add field to search results
One of the requirements of our ticket is to add our new metadata field to the search results screen.
Write a feature spec for our search
You might be able to guess by now, the first thing we'll do is write a feature spec.
- Copy this
search_etd_spec.rb
file from github tospec/features
- Run your test suite. Everything should pass. This feature spec describes the search functionality as it currently exists.
- Edit
search_etd_spec.rb
and add a line to check for your new metadata field, like this:
expect(page).to have_content etd.degree.first
- Run your test suite again. That test should now fail with the message
Failure/Error: expect(page).to have_content etd.degree.first
Add our metadata field to Blacklight config
Because our search behavior is inherited from Blacklight, in order to change what fields are being displayed in the search results, we have to update the application's Blacklight config.
- Edit app/controllers/catalog_controller.rb and look for the section including add_index_field statements. Add the following:
config.add_index_field solr_name("degree", :stored_searchable), label: "Degree"
- Run your test suite again, and your search feature should now pass.
Make a field facetable
Edit the search feature spec
This time, we're not just going to look for whether our degree
field shows up in the search results, we're going to make it a facetable field, and make it appear in the left hand facets menu.
- Add these lines to the end of your search feature spec:
expect(page).to have_xpath("//h3", text: "Creator")
expect(page).to have_link(etd.creator.first, class: "facet_select")
- Run your test suite (
bundle exec rake ci
). Everything should pass. These lines describe functionality that already exists on your search results page. - Now add a test for new behavior that doesn't exist yet:
expect(page).to have_xpath("//h3", text: "Degree")
expect(page).to have_link(etd.degree.first, class: "facet_select")
- Run your test suite again and your search feature will fail
Index the metadata field as facetable
- Edit
app/models/etd.rb
. Change the waydegree
is indexed to include:facetable
:
property :degree, predicate: "http://vivoweb.org/ontology/core#AcademicDegree" do |index|
index.as :stored_searchable, :facetable
end
- Edit
app/controllers/catalog_controller.rb
and around line 46 add this line, right under the Creator facet:
config.add_facet_field solr_name("degree", :facetable), label: "Degree", limit: 5
- Run your test suite again and it should pass.
Note: You can see the changes we made in this section on github.
Pair exercise
We made one field into a facet. Choose either department or school and add a second metadata field. Or, define your own metadata field, as long as you pick something that can be a simple string.