-
Notifications
You must be signed in to change notification settings - Fork 4
Add Metadata to search results and facets
If you have changes in your current branch -- you can check on this via git status
-- you'll want to save those before starting this lesson (which uses a separate branch):
git checkout -b wip_metadata_3_start
git add .
git commit -m 'checkpoint before beginning third metadata lesson'
git checkout add_new_metadata_field_2_end
NOTE: If you make experimental changes and want to get back to the minimal code state necessary to run this lesson, you can check the starting code out again using:
git checkout add_new_metadata_field_2_end
See lesson setup for the first metadata lesson
One of the requirements of our ticket is to add our new metadata field to the search results screen.
You might be able to guess by now, the first thing we'll do is write a feature spec.
- Create
spec/features/search_image_spec.rb
require 'rails_helper'
RSpec.feature 'Search for an image' do
let(:title) { ['Journey to Skull Island'] }
let(:creator) { ['Quest, Jane'] }
let(:keyword) { ['Pirates', 'Adventure'] }
let(:visibility) { Hydra::AccessControls::AccessRight::VISIBILITY_TEXT_VALUE_PUBLIC }
let(:year) { ['1520'] }
let(:image) do
Image.new(title: title,
creator: creator,
keyword: keyword,
visibility: visibility,
year: year)
end
context 'general search' do
before do
image.save
end
scenario "Search for an image" do
visit("/")
fill_in "q", with: "Journey"
click_button "Go"
# Uncomment this to display the HTML capybara is seeing
# puts page.body
expect(page).to have_content image.title.first
expect(page).to have_content image.creator.first
expect(page).to have_content image.keyword.first
end
end
end
- Run your feature test (
docker-compose run web rspec spec/features/search_image_spec.rb
). Everything should pass. This feature spec describes the search functionality as it currently exists. - Edit
spec/features/search_image_spec.rb
and add a line to check for your new metadata field, like this:
expect(page).to have_content image.year.first
- Run your feature test again (
docker-compose run web rspec spec/features/search_image_spec.rb
). That test should now fail with the messageFailure/Error: expect(page).to have_content image.year.first
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 includingadd_index_field
statements. Add the following:
config.add_index_field solr_name("year", :stored_searchable), label: "Year"
- Run your feature test again, and your search feature should now pass.
This time, we're not just going to look for whether our year
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(image.creator.first, class: "facet_select")
- Run your feature test (
docker-compose run web rspec spec/features/search_image_spec.rb
). 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: "Year")
expect(page).to have_link(image.year.first, class: "facet_select")
- Run your test suite again and your search feature will fail
- Edit
app/models/image.rb
. Change the wayyear
is indexed to include:facetable
:
property :year, predicate: "http://www.europeana.eu/schemas/edm/year" 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("year", :facetable), label: "Year", limit: 5
- Run your feature test again and it should pass.
Note: You can see the changes we made in this section on github.
We made one field into a facet. Choose either extent or references and add a second metadata field. Or, define your own metadata field, as long as you pick something that can be a simple string.