Skip to content

Commit d70908b

Browse files
authored
Merge pull request #350 from MITLibraries/DISCO-60_graphql_facets
Adds ability to apply facets in GraphQL
2 parents 928641e + 031c31a commit d70908b

File tree

5 files changed

+209
-4
lines changed

5 files changed

+209
-4
lines changed

app/graphql/types/query_type.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ def record_id(id:)
2525
description: 'Search for timdex records' do
2626
argument :searchterm, String, required: true
2727
argument :from, String, required: false, default_value: '0'
28+
29+
# applied facets
30+
argument :content_type, String, required: false, default_value: nil
31+
argument :contributors, [String], required: false, default_value: nil
32+
argument :format, [String], required: false, default_value: nil
33+
argument :languages, [String], required: false, default_value: nil
34+
argument :literary_form, String, required: false, default_value: nil
2835
argument :source, String, required: false, default_value: 'All'
36+
argument :subjects, [String], required: false, default_value: nil
2937
end
3038

31-
def search(searchterm:, from:, source:)
32-
query = {}
33-
query[:q] = searchterm
34-
query[:source] = source if source != 'All'
39+
def search(searchterm:, from:, **facets)
40+
query = construct_query(searchterm, facets)
3541

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

@@ -42,6 +48,19 @@ def search(searchterm:, from:, source:)
4248
response
4349
end
4450

51+
def construct_query(searchterm, facets)
52+
query = {}
53+
query[:q] = searchterm
54+
query[:content_format] = facets[:format]
55+
query[:content_type] = facets[:content_type]
56+
query[:contributors] = facets[:contributors]
57+
query[:language] = facets[:languages]
58+
query[:literary_form] = facets[:literary_form]
59+
query[:source] = facets[:source] if facets[:source] != 'All'
60+
query[:subject] = facets[:subjects]
61+
query
62+
end
63+
4564
def collapse_buckets(es_aggs)
4665
{
4766
content_format: es_aggs['content_format']['buckets'],

test/controllers/graphql_controller_test.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,88 @@ class GraphqlControllerTest < ActionDispatch::IntegrationTest
109109
end
110110
end
111111

112+
test 'search with source facet applied' do
113+
VCR.use_cassette('graphql search wright only aspace') do
114+
post '/graphql', params: { query: '{
115+
search(searchterm: "wright",
116+
source: "mit archivesspace") {
117+
hits
118+
aggregations {
119+
source {
120+
key
121+
docCount
122+
}
123+
}
124+
}
125+
}' }
126+
assert_equal(200, response.status)
127+
json = JSON.parse(response.body)
128+
assert_equal('mit archivesspace',
129+
json['data']['search']['aggregations']['source']
130+
.first['key'])
131+
assert_equal(1,
132+
json['data']['search']['aggregations']['source']
133+
.first['docCount'])
134+
end
135+
end
136+
137+
test 'search with multiple subjects applied' do
138+
VCR.use_cassette('graphql search multiple subjects') do
139+
post '/graphql', params: { query: '{
140+
search(searchterm: "space",
141+
subjects: ["space and time.",
142+
"quantum theory."]) {
143+
hits
144+
}
145+
}' }
146+
assert_equal(200, response.status)
147+
json = JSON.parse(response.body)
148+
assert_equal(36, json['data']['search']['hits'])
149+
end
150+
end
151+
152+
test 'search with invalid facet applied' do
153+
VCR.use_cassette('graphql search wright fake facet') do
154+
post '/graphql', params: { query: '{
155+
search(searchterm: "wright",
156+
fake: "mit archivesspace") {
157+
hits
158+
aggregations {
159+
source {
160+
key
161+
docCount
162+
}
163+
}
164+
}
165+
}' }
166+
assert_equal(200, response.status)
167+
json = JSON.parse(response.body)
168+
assert(json['errors'].first['message'].present?)
169+
assert_equal("Field 'search' doesn't accept argument 'fake'",
170+
json['errors'].first['message'])
171+
end
172+
end
173+
174+
test 'valid facets can result in no results' do
175+
VCR.use_cassette('graphql legal facets can result in no results') do
176+
post '/graphql', params: { query: '{
177+
search(searchterm: "wright",
178+
subjects: ["fake facet value"]) {
179+
hits
180+
aggregations {
181+
source {
182+
key
183+
docCount
184+
}
185+
}
186+
}
187+
}' }
188+
assert_equal(200, response.status)
189+
json = JSON.parse(response.body)
190+
assert_equal(0, json['data']['search']['hits'])
191+
end
192+
end
193+
112194
test 'retrieve' do
113195
VCR.use_cassette('graphql retrieve') do
114196
post '/graphql', params: { query: '{

test/vcr_cassettes/graphql_legal_facets_can_result_in_no_results.yml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/graphql_search_multiple_subjects.yml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/vcr_cassettes/graphql_search_wright_only_aspace.yml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)