Skip to content

Commit 96b20df

Browse files
committed
Convert aggregation fields to format expected by aggregations model
1 parent 5d5f88c commit 96b20df

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

app/graphql/types/query_type.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,20 @@ def highlight_requested?
123123
context[:tracers].first.log_data[:used_fields].include?('Record.highlight')
124124
end
125125

126+
# Convert aggregation fields to format expected by aggregations model.
127+
# We believe format was set to `content_format` because when TIMDEX was a rest REST API, the
128+
# format parameter would likely have triggered the format feature (i.e. format html, format
129+
# json, format xml, etc). We are retaining this out of caution.
130+
def requested_aggregation_field(field_name)
131+
return :content_format if field_name == 'format'
132+
133+
field_name.underscore.to_sym
134+
end
135+
126136
def requested_aggregations
127137
used_fields = context[:tracers].first.log_data[:used_fields]
128138
used_fields.select { |field| field.start_with?('Aggregations.') }
129-
.map { |field| field.sub('Aggregations.', '').to_sym }
139+
.map { |field| requested_aggregation_field(field.sub('Aggregations.', '')) }
130140
end
131141

132142
# Long-term, we will probably want to define these fields discretely in RecordType. However, this might end up

test/controllers/graphql_controller_test.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,4 +1062,82 @@ class GraphqlControllerTest < ActionDispatch::IntegrationTest
10621062
end
10631063
end
10641064
end
1065+
1066+
test 'graphql search with camelCase aggregation fields (accessToFiles, contentType)' do
1067+
VCR.use_cassette('opensearch init') do
1068+
VCR.use_cassette('graphql search data analytics') do
1069+
post '/graphql', params: { query: '{
1070+
search(searchterm: "data analytics") {
1071+
aggregations {
1072+
accessToFiles {
1073+
key
1074+
docCount
1075+
}
1076+
contentType {
1077+
key
1078+
docCount
1079+
}
1080+
}
1081+
}
1082+
}' }
1083+
assert_equal(200, response.status)
1084+
json = JSON.parse(response.body)
1085+
aggs = json['data']['search']['aggregations']
1086+
1087+
# Verify aggregations are present when requested
1088+
assert_not_nil aggs
1089+
assert_not_nil aggs['accessToFiles']
1090+
assert_not_nil aggs['contentType']
1091+
1092+
# Verify structure
1093+
assert aggs['accessToFiles'].is_a?(Array)
1094+
assert aggs['contentType'].is_a?(Array)
1095+
end
1096+
end
1097+
end
1098+
1099+
test 'graphql search with format aggregation (should map to content_format)' do
1100+
VCR.use_cassette('opensearch init') do
1101+
VCR.use_cassette('graphql search data analytics') do
1102+
post '/graphql', params: { query: '{
1103+
search(searchterm: "data analytics") {
1104+
aggregations {
1105+
format {
1106+
key
1107+
docCount
1108+
}
1109+
}
1110+
}
1111+
}' }
1112+
assert_equal(200, response.status)
1113+
json = JSON.parse(response.body)
1114+
aggs = json['data']['search']['aggregations']
1115+
1116+
# Verify format aggregation is present and populated
1117+
assert_not_nil aggs
1118+
assert_not_nil aggs['format']
1119+
assert aggs['format'].is_a?(Array)
1120+
end
1121+
end
1122+
end
1123+
1124+
test 'graphql search without aggregations excludes them from OpenSearch query' do
1125+
VCR.use_cassette('opensearch init') do
1126+
VCR.use_cassette('graphql search data analytics') do
1127+
post '/graphql', params: { query: '{
1128+
search(searchterm: "data analytics") {
1129+
hits
1130+
records {
1131+
title
1132+
}
1133+
}
1134+
}' }
1135+
assert_equal(200, response.status)
1136+
json = JSON.parse(response.body)
1137+
1138+
# Verify aggregations field is null when not requested
1139+
assert_nil json['data']['search']['aggregations']
1140+
end
1141+
end
1142+
end
10651143
end

0 commit comments

Comments
 (0)