File tree 1 file changed +42
-29
lines changed
1 file changed +42
-29
lines changed Original file line number Diff line number Diff line change @@ -107,48 +107,61 @@ def facets
107
107
108
108
elsif f . include? ( "[" )
109
109
# will be an array including the original, and an alternate aggregation name
110
+
111
+
110
112
options = JSON . parse ( f )
111
113
original = options [ 0 ]
112
114
agg_name = options [ 1 ]
113
115
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
115
121
condition = original [ /(?<=\[ ).+?(?=\] )/ ]
116
122
subject = condition . split ( "#" ) . first
117
123
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
144
145
}
145
146
}
146
147
}
147
148
}
148
149
}
149
150
}
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
152
165
elsif f . include? ( "." )
153
166
path = f . split ( "." ) . first
154
167
aggs [ f ] = {
You can’t perform that action at this time.
0 commit comments