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

D9 Migration #593

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 40 additions & 21 deletions app/services/library_website_api_search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,53 @@ class Response < AbstractSearchService::Response
QUERY_URL = Settings.LIBRARY_WEBSITE.QUERY_URL.freeze

def results
sanitizer = Rails::Html::FullSanitizer.new
Array.wrap(json['results']).first(3).collect do |doc|
result = AbstractSearchService::Result.new
result.title = doc['title']
result.link = doc['url']
result.description = sanitizer.sanitize(doc['description'])
result.breadcrumbs = doc['breadcrumbs']&.drop(1)
result
sanitizer = Rails::Html::FullSanitizer.new

# TODO: remove the json['results'] condition after D9 launch
if json['results']
json['results'].first(3).map do |doc|
result = AbstractSearchService::Result.new
result.title = doc['title']
result.link = doc['url']
result.description = sanitizer.sanitize(doc['description'])
result.breadcrumbs = doc['breadcrumbs']&.drop(1)
result
end
elsif json['data']
json['data'].map do |doc|
result = AbstractSearchService::Result.new
result.title = doc.dig('attributes', 'title')
result.link = doc.dig('attributes', 'path', 'alias')
result.description = sanitizer.sanitize(doc.dig('attributes', 'su_page_description'))
result
end
end
end

# Drupal 9 data for the library website does not support facets currently
# We still to implement the method to override AbstractSearchService's method, which throws a NotImplementedError
# TODO: remove the json['facets'] condition after D9 launch
def facets
facet_response = [{
'name' => HIGHLIGHTED_FACET_FIELD
}]
facet_response.first['items'] = json['facets']['items'].map do |facet|
{
'value' => facet['term']&.first,
'label' => facet['label'],
'hits' => facet['hits'],
}
end
facet_response
return [] unless json['facets']

[{
'name' => HIGHLIGHTED_FACET_FIELD,
'items' => json['facets']['items'].map do |facet|
{
'value' => facet['term']&.first,
'label' => facet['label'],
'hits' => facet['hits']
}
end
}]
end

# TODO: remove the json['facets'] condition after D9 launch
def total
facets = json["facets"]["items"]
facets.sum {|facet| facet["hits"]}
return json.dig('meta', 'count') unless json['facets']

facets = json["facets"]["items"]
facets.sum {|facet| facet["hits"]}
end

private
Expand Down
4 changes: 2 additions & 2 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ EXHIBITS:
NUM_RESULTS_SHOWN: 3
QUERY_URL: 'https://exhibits.stanford.edu/search?q=%{q}'
LIBRARY_WEBSITE:
API_URL: 'https://library.stanford.edu/bento?keys=%{q}'
QUERY_URL: 'https://library.stanford.edu/search/website?search=%{q}'
API_URL: <%= ENV["D9_LAUNCH"] == "true" ? "https://library.sites-pro.stanford.edu/jsonapi/index/full_site_content?fields[node--stanford_page]=path,title,su_page_description&filter[fulltext]=%{q}&page[limit]=%{max}" : "https://library.stanford.edu/bento?keys=%{q}" %>
QUERY_URL: <%= ENV["D9_LAUNCH"] == "true" ? "https://library.stanford.edu/search/?q=%{q}" : 'https://library.stanford.edu/search/website?search=%{q}' %>
EARTHWORKS:
API_URL: 'https://earthworks.stanford.edu?q=%{q}&rows=%{max}&format=json'
QUERY_URL: 'https://earthworks.stanford.edu?q=%{q}'
Expand Down
107 changes: 66 additions & 41 deletions spec/services/library_website_api_search_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,61 @@
subject(:service) { described_class.new }

let(:response) do
JSON.dump({
results: [
{
title: 'Chinese art: Traditional',
url: 'https://library.stanford.edu/guides/chinese-art-traditional',
description: 'This guide...',
breadcrumbs: [
{
"label": "Home",
"url": "https://library.stanford.edu/"
JSON.dump(
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": [
{
"type": "node--stanford_page",
"id": "0ad8a8c0-9c66-4fdc-96e5-ab0b6dabd8a0",
"links": {
"self": {
"href": "https://library.sites-pro.stanford.edu/jsonapi/node/stanford_page/0ad8a8c0-9c66-4fdc-96e5-ab0b6dabd8a0?resourceVersion=id%3A17206"
}
},
{
"label": "Guides",
"url": "https://library.stanford.edu/guides"
"attributes": {
"title": "Work with data",
"path": {
"alias": "/services/work-data",
"pid": 2086,
"langcode": "en"
},
"su_page_description": "First result description"
}
]
}
],
facets: {
name: 'format_facet',
items: [
},
{
value: 'blog',
label: 'Blog',
hits: 32,
term: ['type:blog']
"type": "node--stanford_page",
"id": "e1117434-f6d7-448a-9d4a-80f7e4bc992a",
"links": {
"self": {
"href": "https://library.sites-pro.stanford.edu/jsonapi/node/stanford_page/e1117434-f6d7-448a-9d4a-80f7e4bc992a?resourceVersion=id%3A18441"
}
},
"attributes": {
"title": "Promote your research",
"path": {
"alias": "/services/promote-your-research",
"pid": 2351,
"langcode": "en"
},
"su_page_description": "this is a page that describes how Stanford Libraries can help students and scholars to promote their research"
}
}
]
],
"meta": {
"count": 207
}
}
})
)
end
let(:query) { LibraryWebsiteApiSearchService::Request.new('chinese art') }

Expand All @@ -46,27 +71,27 @@
it { expect(service).to be_an AbstractSearchService }
it { expect(service.search(query)).to be_an LibraryWebsiteApiSearchService::Response }

describe '#facets' do
it 'constructs an API query url' do
facets = service.search(query).facets
expect(facets.first).to eq({
'name' => 'format_facet',
'items' => [{ 'hits' => 32, 'label' => 'Blog', 'value' => 'type:blog' }]
})
describe '#records' do
it 'sets the title, description, and link in the document' do
results = service.search(query).results
expect(results.length).to eq 2
expect(results.first.title).to eq 'Work with data'
expect(results.first.description).to eq 'First result description'
expect(results.first.link).to eq '/services/work-data'
end
end

describe '#records' do
it 'sets the title in the document' do
results = service.search(query).results
expect(results.length).to eq 1
expect(results.first.title).to eq 'Chinese art: Traditional'
describe '#facets' do
it 'returns an empty array' do
facets = service.search(query).facets
expect(facets).to eq []
end
end

it 'provides libweb breadcrumbs' do
results = service.search(query).results
expect(results.first.breadcrumbs.length).to eq 1
expect(results.first.breadcrumbs.first["label"]).to eq 'Guides'
describe '#total' do
it 'returns an empty array' do
count = service.search(query).total
expect(count).to eq 207
end
end
end