Skip to content

Commit 7678c06

Browse files
Add grouping settings for query field name and type (#135) (#138)
(cherry picked from commit f89eb88) Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4daa192 commit 7678c06

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public List<Setting<?>> getSettings() {
142142
QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS,
143143
QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY,
144144
QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N,
145+
QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME,
146+
QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE,
145147
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING
146148
);
147149
}

src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package org.opensearch.plugin.insights.core.listener;
1010

1111
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;
1214
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY;
1315
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N;
1416
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNEnabledSetting;
@@ -54,6 +56,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
5456

5557
private final QueryInsightsService queryInsightsService;
5658
private final ClusterService clusterService;
59+
private boolean groupingFieldNameEnabled;
60+
private boolean groupingFieldTypeEnabled;
5761

5862
/**
5963
* Constructor for QueryInsightsListener
@@ -64,6 +68,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
6468
@Inject
6569
public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) {
6670
this(clusterService, queryInsightsService, false);
71+
groupingFieldNameEnabled = false;
72+
groupingFieldTypeEnabled = false;
6773
}
6874

6975
/**
@@ -126,9 +132,16 @@ public QueryInsightsListener(
126132
this.queryInsightsService.validateMaximumGroups(clusterService.getClusterSettings().get(TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N));
127133
this.queryInsightsService.setMaximumGroups(clusterService.getClusterSettings().get(TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N));
128134

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+
129142
// Settings endpoints set for search query metrics
130143
clusterService.getClusterSettings()
131-
.addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, v -> setSearchQueryMetricsEnabled(v));
144+
.addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, this::setSearchQueryMetricsEnabled);
132145
setSearchQueryMetricsEnabled(clusterService.getClusterSettings().get(SEARCH_QUERY_METRICS_ENABLED_SETTING));
133146
}
134147

@@ -154,6 +167,14 @@ public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) {
154167
updateQueryInsightsState();
155168
}
156169

170+
public void setGroupingFieldNameEnabled(Boolean fieldNameEnabled) {
171+
this.groupingFieldNameEnabled = fieldNameEnabled;
172+
}
173+
174+
public void setGroupingFieldTypeEnabled(Boolean fieldTypeEnabled) {
175+
this.groupingFieldTypeEnabled = fieldTypeEnabled;
176+
}
177+
157178
/**
158179
* Update the query insights service state based on the enabled features.
159180
* 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
241262
);
242263
}
243264

244-
String hashcode = QueryShapeGenerator.getShapeHashCodeAsString(request.source(), false);
245-
246265
Map<Attribute, Object> attributes = new HashMap<>();
247266
attributes.put(Attribute.SEARCH_TYPE, request.searchType().toString().toLowerCase(Locale.ROOT));
248267
attributes.put(Attribute.SOURCE, request.source());
249268
attributes.put(Attribute.TOTAL_SHARDS, context.getNumShards());
250269
attributes.put(Attribute.INDICES, request.indices());
251270
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
252271
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+
}
254277

255278
Map<String, Object> labels = new HashMap<>();
256279
// Retrieve user provided label if exists

src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java

+16
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public class QueryInsightsSettings {
8383
public static final String TOP_QUERIES_BASE_URI = PLUGINS_BASE_URI + "/top_queries";
8484
/** Default prefix for top N queries feature */
8585
public static final String TOP_N_QUERIES_SETTING_PREFIX = "search.insights.top_queries";
86+
/** Default prefix for top N queries grouping feature */
87+
public static final String TOP_N_QUERIES_GROUPING_SETTING_PREFIX = "search.insights.top_queries.grouping";
8688
/** Default prefix for top N queries by latency feature */
8789
public static final String TOP_N_LATENCY_QUERIES_PREFIX = TOP_N_QUERIES_SETTING_PREFIX + ".latency";
8890
/** Default prefix for top N queries by cpu feature */
@@ -139,6 +141,20 @@ public class QueryInsightsSettings {
139141
Setting.Property.Dynamic
140142
);
141143

144+
public static final Setting<Boolean> TOP_N_QUERIES_GROUPING_FIELD_NAME = Setting.boolSetting(
145+
TOP_N_QUERIES_GROUPING_SETTING_PREFIX + ".attributes.field_name",
146+
false,
147+
Setting.Property.Dynamic,
148+
Setting.Property.NodeScope
149+
);
150+
151+
public static final Setting<Boolean> TOP_N_QUERIES_GROUPING_FIELD_TYPE = Setting.boolSetting(
152+
TOP_N_QUERIES_GROUPING_SETTING_PREFIX + ".attributes.field_type",
153+
false,
154+
Setting.Property.Dynamic,
155+
Setting.Property.NodeScope
156+
);
157+
142158
/**
143159
* Boolean setting for enabling top queries by cpu.
144160
*/

src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public void testGetSettings() {
7979
QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS,
8080
QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY,
8181
QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N,
82+
QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME,
83+
QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE,
8284
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING
8385
),
8486
queryInsightsPlugin.getSettings()

src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ public static void registerAllQueryInsightsSettings(ClusterSettings clusterSetti
335335
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS);
336336
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY);
337337
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N);
338+
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME);
339+
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE);
338340
clusterSettings.registerSetting(QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING);
339341
}
340342
}

0 commit comments

Comments
 (0)