Skip to content

Commit

Permalink
added code from ocracoke for search api
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Smith committed Jun 8, 2017
1 parent 65576bf commit 37af9e2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@ def index
end

def search
#solr = RSolr.connect url: Rails.configuration.ocracoke['solr_url']
solr = RSolr.connect url: "http://localhost:8983/solr/hydra-development"
solr_params = {
q: params[:q],
fq: "id:#{params[:id]}"
}
# FIXME:iiifsi
@response = solr.get 'select', params: solr_params

@docs = @response["response"]["docs"].map do |doc|
doc_hits = @response['highlighting'][doc['id']]['txt']
# Each "hit" here might have more than one match emphasized, so we pull those out now.
hits = doc_hits.map do |hit|
hit.scan(/<em>(.*?)<\/em>/).flatten
end.flatten
doc[:hit_number] = hits.length
doc[:hits] = hits
doc
end

@pages_json = {}
first_two_chars = params[:id][0,2]
@docs.map do |doc|
json_file = File.join Rails.configuration.ocracoke['ocr_directory'], first_two_chars, doc['id'], doc['id'] + ".json"
json = File.read json_file
page_json = JSON.parse(json)
@pages_json[doc['id']] = page_json
end

# We keep track of how many times a particular word has had a hit so that we
# pick the correct @pages_json word boundary. This compensates for how there
# could be more than one hit in a snippet.
@hits_used = {}

request.format = :json
respond_to :json
end
end
9 changes: 9 additions & 0 deletions app/views/search/_hit.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
json.set! '@type', 'search:Hit'
json.annotations do
# This is the way we trick UV to show the number of hits
urls = []
doc[:hit_number].times do |time|
urls << annotation_url(doc['id'], doc['resource'], time)
end
json.array! urls
end
30 changes: 30 additions & 0 deletions app/views/search/_resource.json.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
json.set! "@id", annotation_url(doc[:id], doc[:resource], doc[:time])
json.set! '@type', 'oa:Annotation'
json.motivation 'sc:painting'

json.resource do
json.set! '@type', "cnt:ContentAsText"
json.chars doc[:word]
end

page_word_list = @pages_json[doc[:id]]
xywh = if !@pages_json[doc[:id]] || page_word_list[doc[:word]].nil?
"0,0,0,0"
else
word_bounds = page_word_list[doc[:word]].shift
# FIXME: This is the wrong place to set all of these to integers
if word_bounds
x = word_bounds["x0"].to_i
y = word_bounds["y0"].to_i
w = word_bounds["x1"].to_i - word_bounds["x0"].to_i
h = word_bounds["y1"].to_i - word_bounds["y0"].to_i
"#{x},#{y},#{w},#{h}"
else
"0,0,0,0"
end
end

# FIXME: How to make this so it is possible to have different canvas URLs to
# match each institution's own URLs to canvases as found in their
# Presentation manifests?
json.on manifest_canvas_on_xywh(doc[:id], xywh) #manifest_canvas_id(doc[:id]) + "#xywh=#{xywh}"
21 changes: 21 additions & 0 deletions app/views/search/search.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,24 @@ json.set! '@context', 'http://iiif.io/api/search/0/context.json'
json.set! '@id', request.original_url
json.set! '@type', 'sc:AnnotationList'
json.startIndex 0

json.within do
json.ignored []
json.total 1 # FIXME
json.set! '@type', 'sc:Layer'
end

json.hits @docs, partial: 'search/hit', as: :doc

# We rework this into individual resource_docs/annotations so that
# we trick UV into showing the number of hits for each page.
resource_docs = []
@docs.each do |doc|
doc[:hit_number].times do |time|
word = doc[:hits][time]
new_doc = {id: doc['id'], resource: doc['resource'], time: time, word: word}
resource_docs << new_doc
end
end

json.resources resource_docs, partial: 'search/resource', as: :doc

0 comments on commit 37af9e2

Please sign in to comment.