32
32
33
33
package org .opensearch .search .aggregations .bucket .adjacency ;
34
34
35
+ import org .opensearch .Version ;
35
36
import org .opensearch .core .ParseField ;
36
37
import org .opensearch .core .common .io .stream .StreamInput ;
37
38
import org .opensearch .core .common .io .stream .StreamOutput ;
@@ -71,7 +72,10 @@ public class AdjacencyMatrixAggregationBuilder extends AbstractAggregationBuilde
71
72
72
73
private static final ParseField SEPARATOR_FIELD = new ParseField ("separator" );
73
74
private static final ParseField FILTERS_FIELD = new ParseField ("filters" );
75
+ private static final ParseField SHOW_ONLY_INTERSECTING = new ParseField ("show_only_intersecting" );
76
+
74
77
private List <KeyedFilter > filters ;
78
+ private boolean showOnlyIntersecting = false ;
75
79
private String separator = DEFAULT_SEPARATOR ;
76
80
77
81
private static final ObjectParser <AdjacencyMatrixAggregationBuilder , String > PARSER = ObjectParser .fromBuilder (
@@ -81,6 +85,10 @@ public class AdjacencyMatrixAggregationBuilder extends AbstractAggregationBuilde
81
85
static {
82
86
PARSER .declareString (AdjacencyMatrixAggregationBuilder ::separator , SEPARATOR_FIELD );
83
87
PARSER .declareNamedObjects (AdjacencyMatrixAggregationBuilder ::setFiltersAsList , KeyedFilter .PARSER , FILTERS_FIELD );
88
+ PARSER .declareBoolean (
89
+ AdjacencyMatrixAggregationBuilder ::setShowOnlyIntersecting ,
90
+ AdjacencyMatrixAggregationBuilder .SHOW_ONLY_INTERSECTING
91
+ );
84
92
}
85
93
86
94
public static AggregationBuilder parse (XContentParser parser , String name ) throws IOException {
@@ -115,6 +123,7 @@ protected AdjacencyMatrixAggregationBuilder(
115
123
super (clone , factoriesBuilder , metadata );
116
124
this .filters = new ArrayList <>(clone .filters );
117
125
this .separator = clone .separator ;
126
+ this .showOnlyIntersecting = clone .showOnlyIntersecting ;
118
127
}
119
128
120
129
@ Override
@@ -138,13 +147,50 @@ public AdjacencyMatrixAggregationBuilder(String name, String separator, Map<Stri
138
147
setFiltersAsMap (filters );
139
148
}
140
149
150
+ /**
151
+ * @param name
152
+ * the name of this aggregation
153
+ * @param filters
154
+ * the filters and their key to use with this aggregation.
155
+ * @param showOnlyIntersecting
156
+ * show only the buckets that intersection multiple documents
157
+ */
158
+ public AdjacencyMatrixAggregationBuilder (String name , Map <String , QueryBuilder > filters , boolean showOnlyIntersecting ) {
159
+ this (name , DEFAULT_SEPARATOR , filters , showOnlyIntersecting );
160
+ }
161
+
162
+ /**
163
+ * @param name
164
+ * the name of this aggregation
165
+ * @param separator
166
+ * the string used to separate keys in intersections buckets e.g.
167
+ * & character for keyed filters A and B would return an
168
+ * intersection bucket named A&B
169
+ * @param filters
170
+ * the filters and their key to use with this aggregation.
171
+ * @param showOnlyIntersecting
172
+ * show only the buckets that intersection multiple documents
173
+ */
174
+ public AdjacencyMatrixAggregationBuilder (
175
+ String name ,
176
+ String separator ,
177
+ Map <String , QueryBuilder > filters ,
178
+ boolean showOnlyIntersecting
179
+ ) {
180
+ this (name , separator , filters );
181
+ this .showOnlyIntersecting = showOnlyIntersecting ;
182
+ }
183
+
141
184
/**
142
185
* Read from a stream.
143
186
*/
144
187
public AdjacencyMatrixAggregationBuilder (StreamInput in ) throws IOException {
145
188
super (in );
146
189
int filtersSize = in .readVInt ();
147
190
separator = in .readString ();
191
+ if (in .getVersion ().onOrAfter (Version .V_3_0_0 )) {
192
+ showOnlyIntersecting = in .readBoolean ();
193
+ }
148
194
filters = new ArrayList <>(filtersSize );
149
195
for (int i = 0 ; i < filtersSize ; i ++) {
150
196
filters .add (new KeyedFilter (in ));
@@ -155,6 +201,9 @@ public AdjacencyMatrixAggregationBuilder(StreamInput in) throws IOException {
155
201
protected void doWriteTo (StreamOutput out ) throws IOException {
156
202
out .writeVInt (filters .size ());
157
203
out .writeString (separator );
204
+ if (out .getVersion ().onOrAfter (Version .V_3_0_0 )) {
205
+ out .writeBoolean (showOnlyIntersecting );
206
+ }
158
207
for (KeyedFilter keyedFilter : filters ) {
159
208
keyedFilter .writeTo (out );
160
209
}
@@ -185,6 +234,11 @@ private AdjacencyMatrixAggregationBuilder setFiltersAsList(List<KeyedFilter> fil
185
234
return this ;
186
235
}
187
236
237
+ public AdjacencyMatrixAggregationBuilder setShowOnlyIntersecting (boolean showOnlyIntersecting ) {
238
+ this .showOnlyIntersecting = showOnlyIntersecting ;
239
+ return this ;
240
+ }
241
+
188
242
/**
189
243
* Set the separator used to join pairs of bucket keys
190
244
*/
@@ -214,6 +268,10 @@ public Map<String, QueryBuilder> filters() {
214
268
return result ;
215
269
}
216
270
271
+ public boolean isShowOnlyIntersecting () {
272
+ return showOnlyIntersecting ;
273
+ }
274
+
217
275
@ Override
218
276
protected AdjacencyMatrixAggregationBuilder doRewrite (QueryRewriteContext queryShardContext ) throws IOException {
219
277
boolean modified = false ;
@@ -224,7 +282,9 @@ protected AdjacencyMatrixAggregationBuilder doRewrite(QueryRewriteContext queryS
224
282
rewrittenFilters .add (new KeyedFilter (kf .key (), rewritten ));
225
283
}
226
284
if (modified ) {
227
- return new AdjacencyMatrixAggregationBuilder (name ).separator (separator ).setFiltersAsList (rewrittenFilters );
285
+ return new AdjacencyMatrixAggregationBuilder (name ).separator (separator )
286
+ .setFiltersAsList (rewrittenFilters )
287
+ .setShowOnlyIntersecting (showOnlyIntersecting );
228
288
}
229
289
return this ;
230
290
}
@@ -245,7 +305,16 @@ protected AggregatorFactory doBuild(QueryShardContext queryShardContext, Aggrega
245
305
+ "] index level setting."
246
306
);
247
307
}
248
- return new AdjacencyMatrixAggregatorFactory (name , filters , separator , queryShardContext , parent , subFactoriesBuilder , metadata );
308
+ return new AdjacencyMatrixAggregatorFactory (
309
+ name ,
310
+ filters ,
311
+ showOnlyIntersecting ,
312
+ separator ,
313
+ queryShardContext ,
314
+ parent ,
315
+ subFactoriesBuilder ,
316
+ metadata
317
+ );
249
318
}
250
319
251
320
@ Override
@@ -257,7 +326,8 @@ public BucketCardinality bucketCardinality() {
257
326
protected XContentBuilder internalXContent (XContentBuilder builder , Params params ) throws IOException {
258
327
builder .startObject ();
259
328
builder .field (SEPARATOR_FIELD .getPreferredName (), separator );
260
- builder .startObject (AdjacencyMatrixAggregator .FILTERS_FIELD .getPreferredName ());
329
+ builder .field (SHOW_ONLY_INTERSECTING .getPreferredName (), showOnlyIntersecting );
330
+ builder .startObject (FILTERS_FIELD .getPreferredName ());
261
331
for (KeyedFilter keyedFilter : filters ) {
262
332
builder .field (keyedFilter .key (), keyedFilter .filter ());
263
333
}
@@ -268,7 +338,7 @@ protected XContentBuilder internalXContent(XContentBuilder builder, Params param
268
338
269
339
@ Override
270
340
public int hashCode () {
271
- return Objects .hash (super .hashCode (), filters , separator );
341
+ return Objects .hash (super .hashCode (), filters , showOnlyIntersecting , separator );
272
342
}
273
343
274
344
@ Override
@@ -277,7 +347,9 @@ public boolean equals(Object obj) {
277
347
if (obj == null || getClass () != obj .getClass ()) return false ;
278
348
if (super .equals (obj ) == false ) return false ;
279
349
AdjacencyMatrixAggregationBuilder other = (AdjacencyMatrixAggregationBuilder ) obj ;
280
- return Objects .equals (filters , other .filters ) && Objects .equals (separator , other .separator );
350
+ return Objects .equals (filters , other .filters )
351
+ && Objects .equals (separator , other .separator )
352
+ && Objects .equals (showOnlyIntersecting , other .showOnlyIntersecting );
281
353
}
282
354
283
355
@ Override
0 commit comments