Skip to content

Commit

Permalink
Merge pull request #788 from MITLibraries/gdt-162
Browse files Browse the repository at this point in the history
Add support for geodistance search to GraphQL
  • Loading branch information
matt-bernhardt authored Feb 12, 2024
2 parents 369cd40 + ecc44f7 commit b4cfbb7
Show file tree
Hide file tree
Showing 6 changed files with 607 additions and 24 deletions.
18 changes: 0 additions & 18 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,9 @@
"stack": "heroku-22",
"scripts": {},
"env": {
"AWS_ACCESS_KEY": {
"required": true
},
"AWS_ELASTICSEARCH": {
"required": true
},
"AWS_SECRET_ACCESS_KEY": {
"required": true
},
"AWS_REGION": {
"required": true
},
"ELASTICSEARCH_LOG": {
"required": true
},
"ELASTICSEARCH_INDEX": {
"required": true
},
"ELASTICSEARCH_URL": {
"required": true
},
"EMAIL_FROM": {
"required": true
},
Expand Down
8 changes: 8 additions & 0 deletions app/graphql/types/geodistance_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Types
class GeodistanceType < Types::BaseInputObject
description 'Search within a certain distance of a given latitude and longitude'
argument :distance, String, description: 'Search distance to the location? (include units; i.e. "100km" or "50mi")'
argument :latitude, Float, description: 'A decimal between -90.0 and 90.0 (Southern hemisphere is negative)'
argument :longitude, Float, description: 'A decimal between -180.0 and 180.0 (Western hemisphere is negative)'
end
end
15 changes: 9 additions & 6 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def record_id(id:, index:)
argument :funding_information, String, required: false, default_value: nil,
description: 'Search by funding information; e.g., funding source, ' \
'award name, etc.'
argument :geodistance, GeodistanceType, required: false, default_value: nil,
description: 'Search within a certain distance of a specific location'
argument :identifiers, String, required: false, default_value: nil,
description: 'Search by unique indentifier; e.g., ISBN, DOI, etc.'
argument :locations, String, required: false, default_value: nil, description: 'Search by locations'
Expand Down Expand Up @@ -77,10 +79,10 @@ def record_id(id:, index:)
'for a list of possible values'
end

def search(searchterm:, citation:, contributors:, funding_information:, identifiers:, locations:, subjects:,
title:, index:, source:, from:, **filters)
query = construct_query(searchterm, citation, contributors, funding_information, identifiers, locations,
subjects, title, source, filters)
def search(searchterm:, citation:, contributors:, funding_information:, geodistance:, identifiers:, locations:,
subjects:, title:, index:, source:, from:, **filters)
query = construct_query(searchterm, citation, contributors, funding_information, geodistance, identifiers,
locations, subjects, title, source, filters)

results = Opensearch.new.search(from, query, Timdex::OSClient, highlight_requested?, index)

Expand Down Expand Up @@ -109,13 +111,14 @@ def inject_hits_fields_into_source(hits)
modded_sources
end

def construct_query(searchterm, citation, contributors, funding_information, identifiers, locations, subjects,
title, source, filters)
def construct_query(searchterm, citation, contributors, funding_information, geodistance, identifiers, locations,
subjects, title, source, filters)
query = {}
query[:q] = searchterm
query[:citation] = citation
query[:contributors] = contributors
query[:funding_information] = funding_information
query[:geodistance] = geodistance
query[:identifiers] = identifiers
query[:locations] = locations
query[:subjects] = subjects
Expand Down
69 changes: 69 additions & 0 deletions test/controllers/graphql_controller_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,75 @@ def setup
end
end

test 'graphqlv2 geodistance search returns results' do
VCR.use_cassette('graphqlv2 geodistance') do
post '/graphql', params: { query: '{
search(geodistance: {
distance: "100000km",
latitude: 42.3596653,
longitude: -71.0921384
}) {
hits
records {
title
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)

assert_nil(json['errors'])
assert(json['data']['search']['hits'].positive?)
end
end

test 'graphqlv2 geodistance search fails without three required arguments' do
post '/graphql', params: { query: '{
search(geodistance: {
latitude: 42.3596653,
longitude: -71.0921384
}) {
hits
records {
title
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)

assert(json['errors'].length.positive?)
assert_equal(
"Argument 'distance' on InputObject 'Geodistance' is required. Expected type String!",
json['errors'].first['message']
)
end

test 'graphqlv2 geodistance search with another argument' do
VCR.use_cassette('graphqlv2 geodistance with searchterm') do
post '/graphql', params: { query: '{
search(
searchterm: "train stations",
geodistance: {
distance: "100000km",
latitude: 42.3596653,
longitude: -71.0921384
}
) {
hits
records {
title
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)

assert_nil(json['errors'])
assert(json['data']['search']['hits'].positive?)
end
end

test 'graphqlv2 search aggregations' do
VCR.use_cassette('graphql v2 search data') do
post '/graphql', params: { query: '{
Expand Down
Loading

0 comments on commit b4cfbb7

Please sign in to comment.