9
9
package org .opensearch .plugin .insights .core .listener ;
10
10
11
11
import static org .opensearch .plugin .insights .settings .QueryCategorizationSettings .SEARCH_QUERY_METRICS_ENABLED_SETTING ;
12
+ import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_QUERIES_GROUPING_FIELD_NAME ;
13
+ import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_QUERIES_GROUPING_FIELD_TYPE ;
12
14
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_QUERIES_GROUP_BY ;
13
15
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N ;
14
16
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNEnabledSetting ;
@@ -54,6 +56,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
54
56
55
57
private final QueryInsightsService queryInsightsService ;
56
58
private final ClusterService clusterService ;
59
+ private boolean groupingFieldNameEnabled ;
60
+ private boolean groupingFieldTypeEnabled ;
57
61
58
62
/**
59
63
* Constructor for QueryInsightsListener
@@ -64,6 +68,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
64
68
@ Inject
65
69
public QueryInsightsListener (final ClusterService clusterService , final QueryInsightsService queryInsightsService ) {
66
70
this (clusterService , queryInsightsService , false );
71
+ groupingFieldNameEnabled = false ;
72
+ groupingFieldTypeEnabled = false ;
67
73
}
68
74
69
75
/**
@@ -126,9 +132,16 @@ public QueryInsightsListener(
126
132
this .queryInsightsService .validateMaximumGroups (clusterService .getClusterSettings ().get (TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N ));
127
133
this .queryInsightsService .setMaximumGroups (clusterService .getClusterSettings ().get (TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N ));
128
134
135
+ // Internal settings for grouping attributes
136
+ clusterService .getClusterSettings ().addSettingsUpdateConsumer (TOP_N_QUERIES_GROUPING_FIELD_NAME , this ::setGroupingFieldNameEnabled );
137
+ setGroupingFieldNameEnabled (clusterService .getClusterSettings ().get (TOP_N_QUERIES_GROUPING_FIELD_NAME ));
138
+
139
+ clusterService .getClusterSettings ().addSettingsUpdateConsumer (TOP_N_QUERIES_GROUPING_FIELD_TYPE , this ::setGroupingFieldTypeEnabled );
140
+ setGroupingFieldTypeEnabled (clusterService .getClusterSettings ().get (TOP_N_QUERIES_GROUPING_FIELD_TYPE ));
141
+
129
142
// Settings endpoints set for search query metrics
130
143
clusterService .getClusterSettings ()
131
- .addSettingsUpdateConsumer (SEARCH_QUERY_METRICS_ENABLED_SETTING , v -> setSearchQueryMetricsEnabled ( v ) );
144
+ .addSettingsUpdateConsumer (SEARCH_QUERY_METRICS_ENABLED_SETTING , this :: setSearchQueryMetricsEnabled );
132
145
setSearchQueryMetricsEnabled (clusterService .getClusterSettings ().get (SEARCH_QUERY_METRICS_ENABLED_SETTING ));
133
146
}
134
147
@@ -154,6 +167,14 @@ public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) {
154
167
updateQueryInsightsState ();
155
168
}
156
169
170
+ public void setGroupingFieldNameEnabled (Boolean fieldNameEnabled ) {
171
+ this .groupingFieldNameEnabled = fieldNameEnabled ;
172
+ }
173
+
174
+ public void setGroupingFieldTypeEnabled (Boolean fieldTypeEnabled ) {
175
+ this .groupingFieldTypeEnabled = fieldTypeEnabled ;
176
+ }
177
+
157
178
/**
158
179
* Update the query insights service state based on the enabled features.
159
180
* If any feature is enabled, it starts the service. If no features are enabled, it stops the service.
@@ -241,16 +262,18 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
241
262
);
242
263
}
243
264
244
- String hashcode = QueryShapeGenerator .getShapeHashCodeAsString (request .source (), false );
245
-
246
265
Map <Attribute , Object > attributes = new HashMap <>();
247
266
attributes .put (Attribute .SEARCH_TYPE , request .searchType ().toString ().toLowerCase (Locale .ROOT ));
248
267
attributes .put (Attribute .SOURCE , request .source ());
249
268
attributes .put (Attribute .TOTAL_SHARDS , context .getNumShards ());
250
269
attributes .put (Attribute .INDICES , request .indices ());
251
270
attributes .put (Attribute .PHASE_LATENCY_MAP , searchRequestContext .phaseTookMap ());
252
271
attributes .put (Attribute .TASK_RESOURCE_USAGES , tasksResourceUsages );
253
- attributes .put (Attribute .QUERY_HASHCODE , hashcode );
272
+
273
+ if (queryInsightsService .isGroupingEnabled ()) {
274
+ String hashcode = QueryShapeGenerator .getShapeHashCodeAsString (request .source (), groupingFieldNameEnabled );
275
+ attributes .put (Attribute .QUERY_HASHCODE , hashcode );
276
+ }
254
277
255
278
Map <String , Object > labels = new HashMap <>();
256
279
// Retrieve user provided label if exists
0 commit comments