Skip to content

Commit

Permalink
Merge pull request #350 from MITLibraries/DISCO-60_graphql_facets
Browse files Browse the repository at this point in the history
Adds ability to apply facets in GraphQL
  • Loading branch information
JPrevost committed Oct 19, 2020
2 parents 928641e + 031c31a commit d70908b
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/graphql/types/query_type.rb
Expand Up @@ -25,13 +25,19 @@ def record_id(id:)
description: 'Search for timdex records' do
argument :searchterm, String, required: true
argument :from, String, required: false, default_value: '0'

# applied facets
argument :content_type, String, required: false, default_value: nil
argument :contributors, [String], required: false, default_value: nil
argument :format, [String], required: false, default_value: nil
argument :languages, [String], required: false, default_value: nil
argument :literary_form, String, required: false, default_value: nil
argument :source, String, required: false, default_value: 'All'
argument :subjects, [String], required: false, default_value: nil
end

def search(searchterm:, from:, source:)
query = {}
query[:q] = searchterm
query[:source] = source if source != 'All'
def search(searchterm:, from:, **facets)
query = construct_query(searchterm, facets)

results = Search.new.search(from, query)

Expand All @@ -42,6 +48,19 @@ def search(searchterm:, from:, source:)
response
end

def construct_query(searchterm, facets)
query = {}
query[:q] = searchterm
query[:content_format] = facets[:format]
query[:content_type] = facets[:content_type]
query[:contributors] = facets[:contributors]
query[:language] = facets[:languages]
query[:literary_form] = facets[:literary_form]
query[:source] = facets[:source] if facets[:source] != 'All'
query[:subject] = facets[:subjects]
query
end

def collapse_buckets(es_aggs)
{
content_format: es_aggs['content_format']['buckets'],
Expand Down
82 changes: 82 additions & 0 deletions test/controllers/graphql_controller_test.rb
Expand Up @@ -109,6 +109,88 @@ class GraphqlControllerTest < ActionDispatch::IntegrationTest
end
end

test 'search with source facet applied' do
VCR.use_cassette('graphql search wright only aspace') do
post '/graphql', params: { query: '{
search(searchterm: "wright",
source: "mit archivesspace") {
hits
aggregations {
source {
key
docCount
}
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)
assert_equal('mit archivesspace',
json['data']['search']['aggregations']['source']
.first['key'])
assert_equal(1,
json['data']['search']['aggregations']['source']
.first['docCount'])
end
end

test 'search with multiple subjects applied' do
VCR.use_cassette('graphql search multiple subjects') do
post '/graphql', params: { query: '{
search(searchterm: "space",
subjects: ["space and time.",
"quantum theory."]) {
hits
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)
assert_equal(36, json['data']['search']['hits'])
end
end

test 'search with invalid facet applied' do
VCR.use_cassette('graphql search wright fake facet') do
post '/graphql', params: { query: '{
search(searchterm: "wright",
fake: "mit archivesspace") {
hits
aggregations {
source {
key
docCount
}
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)
assert(json['errors'].first['message'].present?)
assert_equal("Field 'search' doesn't accept argument 'fake'",
json['errors'].first['message'])
end
end

test 'valid facets can result in no results' do
VCR.use_cassette('graphql legal facets can result in no results') do
post '/graphql', params: { query: '{
search(searchterm: "wright",
subjects: ["fake facet value"]) {
hits
aggregations {
source {
key
docCount
}
}
}
}' }
assert_equal(200, response.status)
json = JSON.parse(response.body)
assert_equal(0, json['data']['search']['hits'])
end
end

test 'retrieve' do
VCR.use_cassette('graphql retrieve') do
post '/graphql', params: { query: '{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions test/vcr_cassettes/graphql_search_multiple_subjects.yml

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions test/vcr_cassettes/graphql_search_wright_only_aspace.yml

Large diffs are not rendered by default.

0 comments on commit d70908b

Please sign in to comment.