Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search parent metadata instead of no match found #234

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/models/iiif_print/iiif_search_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

# OVERRIDE: Blacklight IIIF Search v1.0.0
# IiifSearchDecorator module extends the functionality of the BlacklightIiifSearch::IiifSearch class
# by overriding the solr_params method to modify the search query to include the parent's metadata.
module IiifPrint
module IiifSearchDecorator
##
# Overrides the solr_params method from BlacklightIiifSearch::IiifSearch to modify the search query.
# The method adds an additional filter to the query to include either the object_relation_field OR the
# parent document's id and removes the :f parameter from the query.
# :object_relation_field refers to the CatalogController's configuration which is typically set to
# 'is_page_of_ssim' in the host application which only searches child works by default.
#
# config.iiif_search = {
# full_text_field: 'all_text_tsimv',
# object_relation_field: 'is_page_of_ssim',
# supported_params: %w[q page],
# autocomplete_handler: 'iiif_suggest',
# suggester_name: 'iiifSuggester'
# }
#
# @return [Hash] A hash containing the modified Solr search parameters
#
def solr_params
return { q: 'nil:nil' } unless q

{
q: "#{q} AND (#{iiif_config[:object_relation_field]}:\"#{parent_document.id}\" OR id:\"#{parent_document.id}\")",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we don't have a parent_document? What happens if iiif_config doesn't have the key :object_relation_field or if iiif_config[:object_relation_field] is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like not having a parent_document should never happen because the work itself comes in as its own parent even when looking at a work that has no parent/child relationships.

if iiif_config lost its :object_relation_field or if iiif_config[:object_relation_field] is nil, then most likely someone altered the code from the generator. I think maybe we need to raise an error at that point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried commenting out the :object_relation_field and it does not cause a crash. This however seems like a programmer if the :object_relation_field is missing (since it comes in through the generator). I'm rethinking if we need to raise an error just to account for this scenario.

rows: rows,
page: page
}
end
end
end
1 change: 1 addition & 0 deletions lib/iiif_print/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Engine < ::Rails::Engine

::BlacklightIiifSearch::IiifSearchResponse.prepend(IiifPrint::IiifSearchResponseDecorator)
::BlacklightIiifSearch::IiifSearchAnnotation.prepend(IiifPrint::BlacklightIiifSearch::AnnotationDecorator)
::BlacklightIiifSearch::IiifSearch.prepend(IiifPrint::IiifSearchDecorator)
Hyrax::Actors::FileSetActor.prepend(IiifPrint::Actors::FileSetActorDecorator)

Hyrax.config do |config|
Expand Down
27 changes: 27 additions & 0 deletions spec/models/iiif_print/iiif_search_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

RSpec.describe IiifPrint::IiifSearchDecorator do
let(:iiif_config) { { object_relation_field: 'is_page_of_ssim' } }
let(:parent_document) { double(SolrDocument, id: 'abc123') }
let(:iiif_search) { BlacklightIiifSearch::IiifSearch.new(params, iiif_config, parent_document) }

describe '#solr_params' do
subject { iiif_search.solr_params }

context 'when q is nil' do
let(:params) { { q: nil } }

it 'returns nil:nil' do
expect(subject).to eq({ q: 'nil:nil' })
end
end

context 'when q is not nil' do
let(:params) { { q: 'catscan' } }

it 'returns a query with the search term and filters for child or parent id' do
expect(subject).to eq({ q: "catscan AND (is_page_of_ssim:\"abc123\" OR id:\"abc123\")", rows: 50, page: nil })
end
end
end
end