Skip to content

Commit

Permalink
Merge 1f00b74 into c72035c
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenenglish committed May 13, 2022
2 parents c72035c + 1f00b74 commit 5cb1358
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
112 changes: 112 additions & 0 deletions app/models/blacklight_iiif_search/iiif_search_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

# corresponds to a IIIF Annotation List
# LOCAL OVERRIDE to modify logic in #resources method
# remove when https://github.com/boston-library/blacklight_iiif_search/issues/6 solved
module BlacklightIiifSearch
class IiifSearchResponse
include BlacklightIiifSearch::Ignored

attr_reader :solr_response, :controller, :iiif_config

##
# @param [Blacklight::Solr::Response] solr_response
# @param [SolrDocument] parent_document
# @param [CatalogController] controller
def initialize(solr_response, parent_document, controller)
@solr_response = solr_response
@parent_document = parent_document
@controller = controller
@iiif_config = controller.iiif_search_config
@resources = []
@hits = []
end

##
# constructs the IIIF::Presentation::AnnotationList
# @return [IIIF::OrderedHash]
def annotation_list
list_id = controller.request.original_url
anno_list = IIIF::Presentation::AnnotationList.new('@id' => list_id)
anno_list['@context'] = %w(
http://iiif.io/api/presentation/2/context.json
http://iiif.io/api/search/1/context.json
)
anno_list['resources'] = resources
anno_list['hits'] = @hits
anno_list['within'] = within
anno_list['prev'] = paged_url(solr_response.prev_page) if solr_response.prev_page
anno_list['next'] = paged_url(solr_response.next_page) if solr_response.next_page
anno_list['startIndex'] = 0 unless solr_response.total_pages > 1
anno_list.to_ordered_hash(force: true, include_context: false)
end

##
# Return an array of IiifSearchAnnotation objects
# @return [Array]
def resources
@total = 0
solr_response['highlighting'].each do |id, hl_hash|
hit = { '@type': 'search:Hit', 'annotations': [] }
document = solr_response.documents.find { |v| v[:id] == id }
if hl_hash.empty?
@total += 1
annotation = IiifSearchAnnotation.new(document,
solr_response.params['q'],
0, nil, controller,
@parent_document)
@resources << annotation.as_hash
hit[:annotations] << annotation.annotation_id
else
hl_hash.each_value do |hl_array|
hl_array.each_with_index do |hl, hl_index|
# iterate over highlighted segments,
# using highlighted text as "query" passed to IiifSearchAnnotation.new
# assume that <em></em> is the highlight tag
hl.scan(/<em>[\S]*<\/em>/)&.map { |v| v.gsub(/<[\/]?em>/, '') }&.each do |hl_string|
@total += 1
annotation = IiifSearchAnnotation.new(document,
hl_string,
hl_index, hl, controller,
@parent_document)
@resources << annotation.as_hash
hit[:annotations] << annotation.annotation_id
end
end
end
end
@hits << hit
end
@resources
end

##
# @return [IIIF::Presentation::Layer]
def within
within_hash = IIIF::Presentation::Layer.new
within_hash['ignored'] = ignored
if solr_response.total_pages > 1
within_hash['first'] = paged_url(1)
within_hash['last'] = paged_url(solr_response.total_pages)
else
within_hash['total'] = @total
end
within_hash
end

##
# create a URL for the previous/next page of results
# @return [String]
def paged_url(page_index)
controller.solr_document_iiif_search_url(clean_params.merge(page: page_index))
end

##
# remove ignored or irrelevant params from the params hash
# @return [ActionController::Parameters]
def clean_params
remove = ignored.map(&:to_sym)
controller.iiif_search_params.except(*%i(page solr_document_id) + remove)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ def coordinates

coords_json = fetch_and_parse_coords
if coords_json && coords_json['words']
matches = coords_json['words'].select { |k, _v| k.downcase =~ /#{query.downcase}/ }
matches = coords_json['words'].select { |k, _v| k.casecmp?(query) }
return default unless matches

term_coords_array = matches.values.flatten(1)[hl_index]
# when multi-word query, hl_index may not exist for term, so use the highest index available
term_coords_array = nil
hl_index.downto(0) do |h_i|
next if term_coords_array

term_coords_array = matches.values.flatten(1)[h_i]
end

return default unless term_coords_array

width = term_coords_array[2] - term_coords_array[0]
Expand Down

0 comments on commit 5cb1358

Please sign in to comment.