Skip to content

Commit

Permalink
Merge pull request #4230 from tulibraries/BL-1829-update-digital-coll…
Browse files Browse the repository at this point in the history
…ections-results-on-bento-page-ENGINE

Bl 1829 update digital collections results on bento page
  • Loading branch information
ebtoner committed Feb 27, 2024
2 parents 35d5547 + 6a90834 commit 4c3cfe6
Show file tree
Hide file tree
Showing 17 changed files with 942 additions and 123 deletions.
17 changes: 7 additions & 10 deletions app/controllers/search_controller.rb
Expand Up @@ -14,11 +14,12 @@ class SearchController < CatalogController

def index
@per_page = 3
cdm_fields = "title!date"
cdm_format = "json"
if params[:q]
engines = %i(books_and_media articles databases journals library_website lib_guides cdm)
searcher = BentoSearch::ConcurrentSearcher.new(*engines)
searcher.search(params[:q], per_page: @per_page, semantic_search_field: params[:field])

searcher.search(params[:q], per_page: @per_page, semantic_search_field: params[:field], cdm_fields: cdm_fields, cdm_format: cdm_format)
@results = process_results(searcher.results)
@lib_guides_query_term = helpers.derived_lib_guides_search_term(@response)
end
Expand All @@ -39,25 +40,21 @@ def process_results(results)
results.each_value do |result|
Honeybadger.notify(result.error[:exception]) if result.failed?
end
# We only care about cdm results count not bento box.
cdm_total_items = view_context.number_with_delimiter(results["cdm"]&.total_items)

unless results["books_and_media"].blank?
items = BentoSearch::Results.new(results["books_and_media"][0...-1])
items.engine_id = results["books_and_media"].engine_id

items.total_items = results["books_and_media"].total_items
items.display_configuration = results["books_and_media"].display_configuration

# Grabbing and setting @response in order to render facets.
# Merges cdm totals into the @response.
@response = results["books_and_media"].last.custom_data
@response.merge_facet(name: "format", value: "digital_collections", hits: cdm_total_items)

results.merge(
"books_and_media" => items,
).except("cdm")
"books_and_media" => items
)
else
results.except("cdm")
results
end
end
end
7 changes: 7 additions & 0 deletions app/helpers/search_helper.rb
Expand Up @@ -129,4 +129,11 @@ def engine_display_configurations
[engine_id, config]
}.to_h
end

def cdm_collection_name(cdm_alias)
cdm_url = "https://digital.library.temple.edu/digital/bl/dmwebservices/index.php?q=dmGetCollectionList/json"
response = JSON.load(URI.open(cdm_url))
collection = response.select { |collection| collection["secondary_alias"] if collection["secondary_alias"] == cdm_alias }
collection.first["name"]
end
end
57 changes: 49 additions & 8 deletions app/search_engines/bento_search/cdm_engine.rb
Expand Up @@ -4,24 +4,65 @@ module BentoSearch
class CDMEngine
include BentoSearch::SearchEngine

delegate :blacklight_config, to: ::SearchController
delegate :blacklight_config, :search_service_class, to: ::SearchController

def conform_to_bento_result(item)
cdm_collection = item.fetch("collection", "").gsub("/", "")
cdm_id = item.fetch("pointer")
BentoSearch::ResultItem.new(
title: item.fetch("title"),
publication_date: item.fetch("date"),
source_title: cdm_collection,
unique_id: cdm_id,
link: "https://digital.library.temple.edu/digital/collection/#{cdm_collection}/id/#{cdm_id}",
other_links: [image_scale(cdm_collection, cdm_id)]
)
end

def image_scale(collection, id)
begin
image_info = JSON.load(URI.open("https://digital.library.temple.edu/digital/bl/dmwebservices/index.php?q=dmGetImageInfo/#{collection}/#{id}/json"))
image_width = image_info["width"]
image_scale = (image_width <= 2500) ? 50 : 6 #this may take some fine tuning depending on all available sizes
rescue StandardError => e
Honeybadger.notify("Ran into error while try to process CDM image info api call: #{e.message}")
end
"https://digital.library.temple.edu/utils/ajaxhelper/?CISOROOT=#{collection}&CISOPTR=#{id}&action=2&DMSCALE=#{image_scale}&DMHEIGHT=340"
end

def search_implementation(args)
query = args.fetch(:query, "").gsub("/", " ")
bento_results = BentoSearch::Results.new
query = ERB::Util.url_encode(query)
results = BentoSearch::Results.new
response = {}
fields = args.fetch(:cdm_fields)
format = args.fetch(:cdm_format)
cdm_url = "https://digital.library.temple.edu/digital/bl/dmwebservices/index.php?q=dmQuery/all/CISOSEARCHALL^#{query}^all^and/#{fields}/sortby/9/#{format}"
response = []

begin
response = CDM::find(query)
results.total_items = response.dig("results", "pager", "total") || 0
response = JSON.load(URI.open(cdm_url))
bento_results.total_items = response.dig("pager", "total") || 0

response["records"].each do |i|
item = BentoSearch::ResultItem.new
item = conform_to_bento_result(i)
if (bento_results.size < 3) && (image_available?(item.other_links[0])) # only take records with images and with alphanumeric titles
bento_results << item unless is_int?(item.title)
end
end
rescue StandardError => e
results.total_items = 0
Honeybadger.notify("Ran into error while try to process CDM: #{e.message}")
end
bento_results
end

def is_int?(str)
!!(str =~ /\A[-+]?[0-9]+\z/)
end

results << BentoSearch::ResultItem.new(custom_data: response)
results
def image_available?(link)
res = URI.open(link)
res.size > 0
end
end
end
3 changes: 3 additions & 0 deletions app/views/bento_search/_image_link.html.erb
@@ -0,0 +1,3 @@
<%= link_to collection[:item_link] do %>
<%= image_tag collection[:image_link][0][:link], class: "d-block" %>
<% end %>
6 changes: 5 additions & 1 deletion app/views/bento_search/_item_title.html.erb
Expand Up @@ -21,12 +21,16 @@
<% if local_assigns[:index] %>
<% id_attr = item.html_id(local_assigns[:id_prefix], index) %>
<%= content_tag("span", :class => "bento_index", :id => (id_attr if id_attr)) do %>
<%= index %>.
<%= index %>
<% end %>
<% end %>
<%= link_to_unless(item.link.blank?, item.complete_title.truncate(130), item.link) %>
<% item.custom_data["records"].each do |item| #cdm api response %>
<%= cdm_record_link(item) %>
<% end if item.custom_data["records"].present? %>
<% if item.display_format.present? || item.display_language.present? %>
<small class="bento_item_about">
Expand Down
3 changes: 3 additions & 0 deletions app/views/bento_search/_link.html.erb
@@ -0,0 +1,3 @@
<% collection.each do |link| %>
<%= link_to link[:label], link[:link] %><br />
<% end %>
38 changes: 20 additions & 18 deletions app/views/bento_search/_std_item.html.erb
Expand Up @@ -17,15 +17,14 @@
%>
<% bento_decorate(item) do |item| %>
<%# for debugging purposes, we'll include the vendor-specific unique_id, if we have
one, in a data-unique-id attribute. %>

<div class="bento_item border-bottom border-lighter-grey pr-2 py-3" data-unique-id="<%= item.unique_id %>">
<%= render :partial => "bento_search/item_title", :object => item, :as => 'item' %>

<%= render :partial => "bento_search/item_title", :object => item, :as => 'item' %>

<div class="bento_item_body ml-3">

Expand Down Expand Up @@ -53,7 +52,7 @@
<% if item.abstract %>
<p class="bento_item_row abstract">
<%= item.render_summary %>
<%= item.render_summary %>
</p>
<% end %>
Expand All @@ -65,25 +64,28 @@
<% if item.other_links.present? %>
<p class="bento_item_other_links">
<%= render :partial => "bento_search/link", :collection => item.other_links %>
<%= render partial: "bento_search/link", locals: { collection: item.other_links } %>
</p>
<% end %>
</div>
<% if item.engine_id == "books_and_media" %>
<div class="bento_item_body ml-3">
<%= render partial: "fields", locals: { document: item.custom_data, document_counter: 0 } %>
</div>
<% end %>

<% if item.custom_data.fetch("availability_facet", []).include?("Online") %>
<%= render :partial => "blacklight_availability", :object => item, :as => 'item' %>
<% end %>
<% if item.engine_id == "books_and_media" %>
<div class="bento_item_body ml-3">
<%= render partial: "fields", locals: { document: item.custom_data, document_counter: 0 } %>
</div>
<% end %>
<% if item.custom_data.fetch("availability_facet", []).include?("Online") %>
<%= render :partial => "blacklight_availability", :object => item, :as => 'item' %>
<% end %>
<% if item.engine_id == "articles" %>
<div class="bento_item_body ml-3">
<%= render partial: "fields", locals: { document: item.custom_data, document_counter: 0 } %>
</div>
<%= render :partial => "primo_availability", :object => item, :as => 'item' %>
<% end %>

<% if item.engine_id == "articles" %>
<div class="bento_item_body ml-3">
<%= render partial: "fields", locals: { document: item.custom_data, document_counter: 0 } %>
</div>
<%= render :partial => "primo_availability", :object => item, :as => 'item' %>
<% end %>
</div>

<% end %>
2 changes: 2 additions & 0 deletions app/views/search/_bento_results.html.erb
Expand Up @@ -4,6 +4,7 @@
<%= render :partial => "lib_guide_recommender_bento" %>
</div>
<% end %>
<% unless engine_id == "cdm" %>
<div class="col mx-auto p-2 bento_compartment <%= engine_id %>">
<h2 class="m-0"><%= bento_icons(engine_id) %><%= bento_titleize(engine_id) %> </h2>
<%= render :layout => "layouts/bento_box_wrapper", :locals => {:results => result } do %>
Expand All @@ -17,4 +18,5 @@
<% end %>
<%= render_linked_results(engine_id) %>
</div>
<% end %>
<% end %>
35 changes: 20 additions & 15 deletions app/views/search/_digital_collections.html.erb
@@ -1,17 +1,22 @@
<% format_facet = @response.aggregations["format"] %>

<div class="bento-resource-container">
<h2 class="bento-resource-types bg-lighter-grey"><%= t("bento.digital_collections_header") %></h2>
<ul class="bento-resource-types-list pt-2 list-inline d-flex flex-wrap justify-content-start">
<% format_facet.items.select do |item| -%>
<% if item.value == "digital_collections" %>
<li class="bento-resource-list-item list-inline-item pb-1">
<%= render_bento_format_facet_value(item) %>
<%= link_to path_for_books_and_media_facet(format_facet, item) do %>
<span class='external-link-icon'></span>
<% end %>
</li>
<% end %>
<div class="bento-resource-container">
<h2 class="bento-resource-types bg-lighter-grey"><%= t("bento.digital_collections_header") %></h2>
<ul class="bento-resource-types-list pt-2 pb-0 list-inline d-flex flex-wrap justify-content-start">
<% @results["cdm"].each do |cdm_record| %>
<li class="bento-resource-list-item list-inline-item pb-1">
<%= link_to cdm_record.link do %>
<%= image_tag cdm_record.other_links[0] %>
<div class="pt-2 w-75">
<%= cdm_record.title %>
</div>
<% end %>
</ul>
<p class="w-75" style="color:black;font-size:1rem;">
<%= "Date: #{cdm_record.publication_date}" if cdm_record.publication_date.present? %>
<br /><%= cdm_collection_name(cdm_record.source_title) %>
</p>
</li>
<% end %>
</ul>
<div class="bento-resource-types-list">
<%= link_to "View all digitized collections results >", "https://digital.library.temple.edu/digital/search/searchterm/#{params[:q]}/order/nosort" %>
</div>
</div>
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -81,7 +81,7 @@
# config.i18n.raise_on_missing_translations = true

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
config.action_view.annotate_rendered_view_with_filenames = true

# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/cdm.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

CDM.configure do |config|
config.server_url = "https://server16002.contentdm.oclc.org"
config.max_recs = "1"
config.server_url = "https://digital.library.temple.edu/digital"
config.max_recs = "3"
end
2 changes: 1 addition & 1 deletion config/locales/en.yml
Expand Up @@ -251,7 +251,7 @@ en:
faq_link: "https://answers.library.temple.edu"
resource_types_header: "More search results"
research_guides_header: "Browse our Research Guides and FAQ"
digital_collections_header: "Search our Digitized Collections for unique materials held by Temple"
digital_collections_header: "Digitized Collections for unique materials held by Temple"
application_name: "Library Bento Search Beta"
explanation_html: "<strong>Library Search:</strong> results from %{articles}, %{journals}, and %{more}."
section_heading_html: "Library Search is your gateway to discover library content. Search <strong>Everything</strong> to discover results in %{more}, %{articles}, %{databases}, %{journals}, and %{website}."
Expand Down
32 changes: 5 additions & 27 deletions spec/controllers/search_controller_spec.rb
Expand Up @@ -5,34 +5,16 @@
RSpec.describe SearchController, type: :controller do

describe "#process_results" do
let(:results) { BentoSearch::ConcurrentSearcher.new(:books_and_media, :cdm).search("foo").results }
let(:books_media_results) { BentoSearch::ConcurrentSearcher.new(:books_and_media).search("ymca").results }

before {
stub_request(:get, /contentdm/)
.to_return(status: 200,
headers: { "Content-Type" => "application/json" },
body: JSON.dump(content_dm_results))
controller.send(:process_results, results)
controller.send(:process_results, books_media_results)
}

context "content dm and regular results are present" do

it "defines @reponse instance variable for the controller" do
context "regular results are present" do
it "defines @response instance variable for the controller" do
expect(controller.instance_variable_get(:@response)).not_to be_nil
end

it "adds content-dm totals to facet" do
facet_fields = controller.instance_variable_get(:@response).facet_fields
expect(facet_fields).to include("format" => [ "digital_collections", "415" ])
end
end

context "only cdm results present" do
let(:results) { BentoSearch::ConcurrentSearcher.new(:cdm).search("foo").results }

it "should still remove cdm results from bento results" do
expect(results[:cdm]).to be_nil
end
end

context "one or more concurrent searches fails" do
Expand All @@ -49,7 +31,7 @@ def search_implementation(args)
conf.engine = "BadService"
end

results = BentoSearch::ConcurrentSearcher.new(:books_and_media, :cdm, :bad_service).search("foo").results
results = BentoSearch::ConcurrentSearcher.new(:books_and_media, :bad_service).search("foo").results
expect {
expect { controller.send(:process_results, results) }.to_not raise_error
Honeybadger.flush
Expand All @@ -58,8 +40,4 @@ def search_implementation(args)
end
end
end

def content_dm_results
{ "results" => { "pager" => { "total" => "415" } } }
end
end

0 comments on commit 4c3cfe6

Please sign in to comment.