Skip to content

Commit ef22307

Browse files
committed
rewrite filtered aggregation to be either nested or not
1 parent 3c223e4 commit ef22307

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

app/services/search_item_req.rb

+42-29
Original file line numberDiff line numberDiff line change
@@ -107,48 +107,61 @@ def facets
107107

108108
elsif f.include?("[")
109109
# will be an array including the original, and an alternate aggregation name
110+
111+
110112
options = JSON.parse(f)
111113
original = options[0]
112114
agg_name = options[1]
113115
facet = original.split("[")[0]
114-
path = facet.split(".").first
116+
# may or may not be nested
117+
nested = facet.include?(".")
118+
if nested
119+
path = facet.split(".").first
120+
end
115121
condition = original[/(?<=\[).+?(?=\])/]
116122
subject = condition.split("#").first
117123
predicate = condition.split("#").last
118-
aggs[agg_name] = {
119-
"nested" => {
120-
"path" => path
121-
},
122-
"aggs" => {
123-
agg_name => {
124-
"filter" => {
125-
"term" => {
126-
subject => predicate
127-
}
128-
},
129-
"aggs" => {
130-
agg_name => {
131-
"terms" => {
132-
"field" => facet,
133-
"order" => { type => dir },
134-
"size" => size
135-
},
136-
"aggs" => {
137-
"top_matches" => {
138-
"top_hits" => {
139-
"_source" => {
140-
"includes" => [ agg_name ]
141-
},
142-
"size" => 1
143-
}
124+
aggregation = {
125+
# common to nested and non-nested
126+
"filter" => {
127+
"term" => {
128+
subject => predicate
129+
}
130+
},
131+
"aggs" => {
132+
agg_name => {
133+
"terms" => {
134+
"field" => facet,
135+
"order" => { type => dir },
136+
"size" => size
137+
},
138+
"aggs" => {
139+
"top_matches" => {
140+
"top_hits" => {
141+
"_source" => {
142+
"includes" => [ agg_name ]
143+
},
144+
"size" => 1
144145
}
145146
}
146147
}
147148
}
148149
}
149150
}
150-
}
151-
# ordinary nested facet
151+
#interpolate above hash into nested query
152+
if nested
153+
aggs[agg_name] = {
154+
"nested" => {
155+
"path" => path
156+
},
157+
"aggs" => {
158+
agg_name => aggregation
159+
}
160+
}
161+
else
162+
#otherwise it is the whole query
163+
aggs[agg_name] = aggregation
164+
end
152165
elsif f.include?(".")
153166
path = f.split(".").first
154167
aggs[f] = {

0 commit comments

Comments
 (0)