From 04435cb2f0b15dcc687006e57d4fce0d4c0d3a99 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 9 Oct 2024 17:34:16 +0000 Subject: [PATCH] Add grouping settings for query field name and type (#135) Signed-off-by: Siddhant Deshmukh (cherry picked from commit f89eb881ded84d5f86e983a428d890868aebd46d) Signed-off-by: github-actions[bot] --- .../plugin/insights/QueryInsightsPlugin.java | 2 ++ .../core/listener/QueryInsightsListener.java | 31 ++++++++++++++++--- .../settings/QueryInsightsSettings.java | 16 ++++++++++ .../insights/QueryInsightsPluginTests.java | 2 ++ .../insights/QueryInsightsTestUtils.java | 2 ++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java index a105cfd..862f4c8 100644 --- a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java +++ b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java @@ -142,6 +142,8 @@ public List> getSettings() { QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS, QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY, QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N, + QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME, + QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE, QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING ); } diff --git a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java index 33ea533..2b18692 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java +++ b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java @@ -9,6 +9,8 @@ package org.opensearch.plugin.insights.core.listener; import static org.opensearch.plugin.insights.settings.QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING; +import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME; +import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNEnabledSetting; @@ -54,6 +56,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener private final QueryInsightsService queryInsightsService; private final ClusterService clusterService; + private boolean groupingFieldNameEnabled; + private boolean groupingFieldTypeEnabled; /** * Constructor for QueryInsightsListener @@ -64,6 +68,8 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener @Inject public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) { this(clusterService, queryInsightsService, false); + groupingFieldNameEnabled = false; + groupingFieldTypeEnabled = false; } /** @@ -126,9 +132,16 @@ public QueryInsightsListener( this.queryInsightsService.validateMaximumGroups(clusterService.getClusterSettings().get(TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N)); this.queryInsightsService.setMaximumGroups(clusterService.getClusterSettings().get(TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N)); + // Internal settings for grouping attributes + clusterService.getClusterSettings().addSettingsUpdateConsumer(TOP_N_QUERIES_GROUPING_FIELD_NAME, this::setGroupingFieldNameEnabled); + setGroupingFieldNameEnabled(clusterService.getClusterSettings().get(TOP_N_QUERIES_GROUPING_FIELD_NAME)); + + clusterService.getClusterSettings().addSettingsUpdateConsumer(TOP_N_QUERIES_GROUPING_FIELD_TYPE, this::setGroupingFieldTypeEnabled); + setGroupingFieldTypeEnabled(clusterService.getClusterSettings().get(TOP_N_QUERIES_GROUPING_FIELD_TYPE)); + // Settings endpoints set for search query metrics clusterService.getClusterSettings() - .addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, v -> setSearchQueryMetricsEnabled(v)); + .addSettingsUpdateConsumer(SEARCH_QUERY_METRICS_ENABLED_SETTING, this::setSearchQueryMetricsEnabled); setSearchQueryMetricsEnabled(clusterService.getClusterSettings().get(SEARCH_QUERY_METRICS_ENABLED_SETTING)); } @@ -154,6 +167,14 @@ public void setSearchQueryMetricsEnabled(boolean searchQueryMetricsEnabled) { updateQueryInsightsState(); } + public void setGroupingFieldNameEnabled(Boolean fieldNameEnabled) { + this.groupingFieldNameEnabled = fieldNameEnabled; + } + + public void setGroupingFieldTypeEnabled(Boolean fieldTypeEnabled) { + this.groupingFieldTypeEnabled = fieldTypeEnabled; + } + /** * Update the query insights service state based on the enabled features. * If any feature is enabled, it starts the service. If no features are enabled, it stops the service. @@ -241,8 +262,6 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final ); } - String hashcode = QueryShapeGenerator.getShapeHashCodeAsString(request.source(), false); - Map attributes = new HashMap<>(); attributes.put(Attribute.SEARCH_TYPE, request.searchType().toString().toLowerCase(Locale.ROOT)); attributes.put(Attribute.SOURCE, request.source()); @@ -250,7 +269,11 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final attributes.put(Attribute.INDICES, request.indices()); attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap()); attributes.put(Attribute.TASK_RESOURCE_USAGES, tasksResourceUsages); - attributes.put(Attribute.QUERY_HASHCODE, hashcode); + + if (queryInsightsService.isGroupingEnabled()) { + String hashcode = QueryShapeGenerator.getShapeHashCodeAsString(request.source(), groupingFieldNameEnabled); + attributes.put(Attribute.QUERY_HASHCODE, hashcode); + } Map labels = new HashMap<>(); // Retrieve user provided label if exists diff --git a/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java b/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java index 8bf03a8..5aa6364 100644 --- a/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java +++ b/src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java @@ -83,6 +83,8 @@ public class QueryInsightsSettings { public static final String TOP_QUERIES_BASE_URI = PLUGINS_BASE_URI + "/top_queries"; /** Default prefix for top N queries feature */ public static final String TOP_N_QUERIES_SETTING_PREFIX = "search.insights.top_queries"; + /** Default prefix for top N queries grouping feature */ + public static final String TOP_N_QUERIES_GROUPING_SETTING_PREFIX = "search.insights.top_queries.grouping"; /** Default prefix for top N queries by latency feature */ public static final String TOP_N_LATENCY_QUERIES_PREFIX = TOP_N_QUERIES_SETTING_PREFIX + ".latency"; /** Default prefix for top N queries by cpu feature */ @@ -139,6 +141,20 @@ public class QueryInsightsSettings { Setting.Property.Dynamic ); + public static final Setting TOP_N_QUERIES_GROUPING_FIELD_NAME = Setting.boolSetting( + TOP_N_QUERIES_GROUPING_SETTING_PREFIX + ".attributes.field_name", + false, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + + public static final Setting TOP_N_QUERIES_GROUPING_FIELD_TYPE = Setting.boolSetting( + TOP_N_QUERIES_GROUPING_SETTING_PREFIX + ".attributes.field_type", + false, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + /** * Boolean setting for enabling top queries by cpu. */ diff --git a/src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java b/src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java index 2d2c1dc..e695994 100644 --- a/src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java +++ b/src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java @@ -79,6 +79,8 @@ public void testGetSettings() { QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS, QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY, QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N, + QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME, + QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE, QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING ), queryInsightsPlugin.getSettings() diff --git a/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java b/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java index 8c2a6e4..980ee6a 100644 --- a/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java +++ b/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java @@ -335,6 +335,8 @@ public static void registerAllQueryInsightsSettings(ClusterSettings clusterSetti clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_MEMORY_EXPORTER_SETTINGS); clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUP_BY); clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_MAX_GROUPS_EXCLUDING_N); + clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME); + clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE); clusterSettings.registerSetting(QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING); } }