Skip to content

Commit

Permalink
Merge pull request #1185 from mlibrary/epub-search-cfi-ranges
Browse files Browse the repository at this point in the history
Work towards resolving #1162, highlighting search terms in CSB. One of
  • Loading branch information
gkostin1966 authored Sep 7, 2017
2 parents c31f218 + 7e78c9f commit 86a1ed1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
33 changes: 25 additions & 8 deletions app/services/e_pubs_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ def walk_up_dom(el, cfi, i)
end
end

def snippet(e, q)
p0 = e.text.downcase.index(q.downcase)
p1 = p0 + q.length
def snippet(e, p0, p1)
before = e.text[p0 - 30..p0 - 1]
after = e.text[p1 + 1..p1 + 30]
"...#{before}#{e.text[p0..p1]}#{after}..."
end

def find_hits(e, q, offset, hits)
p0 = e.text.downcase.index(q.downcase, offset)
p1 = p0 + q.length

hits.push(
p0: p0,
p1: p1,
snippet: snippet(e, p0, p1)
)

find_hits(e, q, p1, hits) if e.text.downcase.index(q.downcase, p1)

hits
end

def chapters_from_db(q)
db_results = []
stm = @db.prepare "SELECT href, basecfi, chapter_id from chapters where chapters MATCH ?"
Expand Down Expand Up @@ -68,12 +81,16 @@ def cfis_from_epub(db_results, q)
els.each do |e|
# calculate the cfi of the dom element
cfi = walk_up_dom(e, "", 0)
# find all the hits in this element
hits = find_hits(e, q, 0, [])

results[:search_results].push(
cfi: "#{r[:basecfi]}#{cfi}",
chapter_id: r[:chapter_id],
snippet: snippet(e, q)
)
hits.each do |hit|
results[:search_results].push(
cfi: "#{r[:basecfi]}#{cfi},/1:#{hit[:p0]},/1:#{hit[:p1]}",
chapter_id: r[:chapter_id],
snippet: hit[:snippet]
)
end
end
end
results
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/e_pubs_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@
it do
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)["q"]).to eq "White Whale"
expect(JSON.parse(response.body)["search_results"].length).to eq 91
expect(JSON.parse(response.body)["search_results"][0]["cfi"]).to eq "/6/84[xchapter_036]!/4/2/42"
expect(JSON.parse(response.body)["search_results"].length).to eq 107
expect(JSON.parse(response.body)["search_results"][0]["cfi"]).to eq "/6/84[xchapter_036]!/4/2/42,/1:66,/1:77"
expect(JSON.parse(response.body)["search_results"][0]["snippet"]).to eq "... heard me give orders about a white whale. Look ye! d’ye see this Spanis..."
end
end
Expand All @@ -165,7 +165,7 @@

it do
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)["search_results"].length).to eq 2
expect(JSON.parse(response.body)["search_results"].length).to eq 3
end
end
end
Expand Down

0 comments on commit 86a1ed1

Please sign in to comment.