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

🐛 Correct search count #259

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
31 changes: 28 additions & 3 deletions app/models/iiif_print/iiif_search_response_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@ module IiifSearchResponseDecorator
# @see https://github.com/scientist-softserv/louisville-hyku/commit/67467e5cf9fdb755f54419f17d3c24c87032d0af
def annotation_list
json_results = super
resources = json_results&.[]('resources')

resources&.each do |result_hit|
# Break down the json_results for easy access
resources = json_results['resources']
hits = json_results['hits']
within = json_results['within']

# Check and process invalid hit
if resources
remove_invalid_hit(resources, hits, within)
add_metadata_match(resources)
end

json_results
end

def remove_invalid_hit(resources, hits, within)
invalid_hit = resources.detect { |resource| resource["on"].include?(IiifPrint::BlacklightIiifSearch::AnnotationDecorator::INVALID_MATCH_TEXT) }
return unless invalid_hit

# Delete invalid hit from resources, remove first hit (which is from the invalid hit), decrement total within
resources.delete(invalid_hit)
hits.shift
within['total'] -= 1
end

def add_metadata_match(resources)
resources.each do |result_hit|
next if result_hit['resource'].present?

# Add resource details if not present
result_hit['resource'] = {
"@type": "cnt:ContentAsText",
"chars": "Metadata match, see sidebar for details"
}
end
json_results
end
end
end
37 changes: 35 additions & 2 deletions lib/iiif_print/blacklight_iiif_search/annotation_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module IiifPrint
module BlacklightIiifSearch
module AnnotationDecorator
INVALID_MATCH_TEXT = "#xywh=INVALID,INVALID,INVALID,INVALID".freeze
##
# Create a URL for the annotation
# use a Hyrax-y URL syntax:
Expand All @@ -28,19 +29,30 @@ def canvas_uri_for_annotation
# @return [String]
def coordinates
return default_coords if query.blank?

sanitized_query = sanitize_query
coords_json = fetch_and_parse_coords
return default_coords unless coords_json && coords_json['coords']
sanitized_query = query.match(additional_query_terms_regex)[1].strip

coords_check_result = check_coords_json_and_properties(coords_json, sanitized_query)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about moving the guard condition of the original method outside of the original method, as it has a surprising return value (based on the given method name).

return derived_coords_json_and_properties(coords_json, sanitized_query) unless coords_json['coords']

return coords_check_result if coords_check_result

query_terms = sanitized_query.split(' ').map(&:downcase)

matches = coords_json['coords'].select do |k, _v|
k.downcase =~ /(#{query_terms.join('|')})/
end
return default_coords if matches.blank?

coords_array = matches.values.flatten(1)[hl_index]
return default_coords unless coords_array

"#xywh=#{coords_array.join(',')}"
end

def sanitize_query
query.match(additional_query_terms_regex)[1].strip
end

##
# return the JSON word-coordinates file contents
# @return [JSON]
Expand All @@ -54,6 +66,20 @@ def fetch_and_parse_coords
end
end

# This is a bit hacky but it is checking if any of the properties contain the query term
# if there are no coords and there is a metadata property match
# then we return the default coords
# else we insert a invalid match text to be stripped out at a later point
# @see IiifPrint::IiifSearchResponseDecorator#annotation_list
def check_coords_json_and_properties(coords_json, sanitized_query)
return if coords_json && coords_json['coords']

properties = @document.keys.select { |key| key.ends_with? "_tesim" }
Copy link
Contributor

Choose a reason for hiding this comment

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

returning_value = INVALID_MATCH_TEXT

@document.each_pair do |key, value|
   next if key.ends_with?("_tesim")
   next unless value.join.downcase.include?(sanitized_query)

   returning_value = default_coords
   break
  end
end

returning_value

properties.each { |property| return default_coords if @document[property].join.downcase.include?(sanitized_query) }

INVALID_MATCH_TEXT
end

##
# a default set of coordinates
# @return [String]
Expand Down Expand Up @@ -97,6 +123,13 @@ def file_set_id
def additional_query_terms_regex
/(.*)(?= AND (\(.+\)|\w+)$)/
end

##
# @return [IIIF::Presentation::Resource]
def text_resource_for_annotation
IIIF::Presentation::Resource.new('@type' => 'cnt:ContentAsText',
'chars' => sanitize_query)
end
end
end
end
Loading