diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index 4adf47e6531a3..f0f504447cf82 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -16,7 +16,7 @@ import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; -import org.opensearch.plugin.insights.core.categorizer.SearchQueryCategorizer; +import org.opensearch.plugin.insights.core.service.categorizer.SearchQueryCategorizer; import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporterFactory; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/QueryShapeVisitor.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeVisitor.java similarity index 98% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/QueryShapeVisitor.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeVisitor.java index 2f3f729e79752..626e05dcabb4e 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/QueryShapeVisitor.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeVisitor.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; import org.apache.lucene.search.BooleanClause; import org.opensearch.common.SetOnce; diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryAggregationCategorizer.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryAggregationCategorizer.java similarity index 88% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryAggregationCategorizer.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryAggregationCategorizer.java index 876eec3813f5f..f252795aaca3e 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryAggregationCategorizer.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryAggregationCategorizer.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; import org.opensearch.search.aggregations.AggregationBuilder; import org.opensearch.search.aggregations.PipelineAggregationBuilder; @@ -43,7 +43,7 @@ public void incrementSearchQueryAggregationCounters(Collection subAggregations = aggregationBuilder.getSubAggregations(); @@ -57,7 +57,7 @@ private void incrementCountersRecursively(AggregationBuilder aggregationBuilder) Collection pipelineAggregations = aggregationBuilder.getPipelineAggregations(); for (PipelineAggregationBuilder pipelineAggregation : pipelineAggregations) { String pipelineAggregationType = pipelineAggregation.getType(); - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, pipelineAggregationType)); + searchQueryCounters.incrementAggCounter(1, Tags.create().addTag(TYPE_TAG, pipelineAggregationType)); } } } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizer.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizer.java similarity index 90% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizer.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizer.java index c2a0713fa6c83..dbbfec69dc600 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizer.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizer.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -34,7 +34,7 @@ public final class SearchQueryCategorizer { /** * Contains all the search query counters */ - public final SearchQueryCounters searchQueryCounters; + private final SearchQueryCounters searchQueryCounters; final SearchQueryAggregationCategorizer searchQueryAggregationCategorizer; @@ -72,10 +72,9 @@ public void categorize(SearchSourceBuilder source) { private void incrementQuerySortCounters(List> sorts) { if (sorts != null && sorts.size() > 0) { - for (ListIterator> it = sorts.listIterator(); it.hasNext();) { - SortBuilder sortBuilder = it.next(); + for (SortBuilder sortBuilder : sorts) { String sortOrder = sortBuilder.order().toString(); - searchQueryCounters.sortCounter.add(1, Tags.create().addTag("sort_order", sortOrder)); + searchQueryCounters.incrementSortCounter(1, Tags.create().addTag("sort_order", sortOrder)); } } } @@ -105,4 +104,7 @@ private void logQueryShape(QueryBuilder topLevelQueryBuilder) { log.trace("Query shape : {}", shapeVisitor.prettyPrintTree(" ")); } + public SearchQueryCounters getSearchQueryCounters() { + return this.searchQueryCounters; + } } diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizingVisitor.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizingVisitor.java similarity index 95% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizingVisitor.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizingVisitor.java index ed6411145376d..d4762e6967881 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCategorizingVisitor.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizingVisitor.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; import org.apache.lucene.search.BooleanClause; import org.opensearch.index.query.QueryBuilder; diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCounters.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCounters.java similarity index 78% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCounters.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCounters.java index 8c7c91f794f16..bcbbc442d6a04 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/SearchQueryCounters.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCounters.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; import org.opensearch.index.query.QueryBuilder; import org.opensearch.telemetry.metrics.Counter; @@ -27,20 +27,20 @@ public final class SearchQueryCounters { /** * Aggregation counter */ - public final Counter aggCounter; + private final Counter aggCounter; /** * Counter for all other query types (catch all) */ - public final Counter otherQueryCounter; + private final Counter otherQueryCounter; /** * Counter for sort */ - public final Counter sortCounter; + private final Counter sortCounter; private final Map, Counter> queryHandlers; /** * Counter name to Counter object map */ - public final ConcurrentHashMap nameToQueryTypeCounters; + private final ConcurrentHashMap nameToQueryTypeCounters; /** * Constructor @@ -80,6 +80,26 @@ public void incrementCounter(QueryBuilder queryBuilder, int level) { counter.add(1, Tags.create().addTag(LEVEL_TAG, level)); } + public void incrementAggCounter(double value, Tags tags) { + aggCounter.add(value, tags); + } + + public void incrementSortCounter(double value, Tags tags) { + sortCounter.add(value, tags); + } + + public Counter getAggCounter() { + return aggCounter; + } + + public Counter getSortCounter() { + return sortCounter; + } + + public Counter getCounterByQueryBuilderName(String queryBuilderName) { + return nameToQueryTypeCounters.get(queryBuilderName); + } + private Counter createQueryCounter(String counterName) { Counter counter = metricsRegistry.createCounter( "search.query.type." + counterName + ".count", diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/package-info.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/package-info.java similarity index 82% rename from plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/package-info.java rename to plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/package-info.java index a59f4490c40e0..3bcbc7d4e31ee 100644 --- a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/categorizer/package-info.java +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/core/service/categorizer/package-info.java @@ -9,4 +9,4 @@ /** * Query Insights search query categorizor to collect metrics related to search queries */ -package org.opensearch.plugin.insights.core.categorizer; +package org.opensearch.plugin.insights.core.service.categorizer; diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/QueryShapeVisitorTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/QueryShapeVisitorTests.java similarity index 92% rename from plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/QueryShapeVisitorTests.java rename to plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/QueryShapeVisitorTests.java index 2b6fd6f1daec1..bffe287ed8c25 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/QueryShapeVisitorTests.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/QueryShapeVisitorTests.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizor; +package org.opensearch.plugin.insights.core.service.categorizor; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.ConstantScoreQueryBuilder; @@ -16,7 +16,7 @@ import org.opensearch.index.query.RegexpQueryBuilder; import org.opensearch.index.query.TermQueryBuilder; import org.opensearch.index.query.TermsQueryBuilder; -import org.opensearch.plugin.insights.core.categorizer.QueryShapeVisitor; +import org.opensearch.plugin.insights.core.service.categorizer.QueryShapeVisitor; import org.opensearch.test.OpenSearchTestCase; public final class QueryShapeVisitorTests extends OpenSearchTestCase { diff --git a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/SearchQueryCategorizerTests.java b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/SearchQueryCategorizerTests.java similarity index 71% rename from plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/SearchQueryCategorizerTests.java rename to plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/SearchQueryCategorizerTests.java index e78fc7bd23f8d..6987ebee6b868 100644 --- a/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/categorizor/SearchQueryCategorizerTests.java +++ b/plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/service/categorizor/SearchQueryCategorizerTests.java @@ -6,7 +6,7 @@ * compatible open source license. */ -package org.opensearch.plugin.insights.core.categorizor; +package org.opensearch.plugin.insights.core.service.categorizor; import org.opensearch.index.query.BoolQueryBuilder; import org.opensearch.index.query.BoostingQueryBuilder; @@ -20,7 +20,7 @@ import org.opensearch.index.query.TermQueryBuilder; import org.opensearch.index.query.WildcardQueryBuilder; import org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder; -import org.opensearch.plugin.insights.core.categorizer.SearchQueryCategorizer; +import org.opensearch.plugin.insights.core.service.categorizer.SearchQueryCategorizer; import org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder; import org.opensearch.search.aggregations.bucket.terms.MultiTermsAggregationBuilder; import org.opensearch.search.aggregations.support.MultiTermsValuesSourceConfig; @@ -75,14 +75,12 @@ public void testAggregationsQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.aggCounter).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getAggCounter()).add(eq(1.0d), any(Tags.class)); - // capture the arguments passed to the aggCounter.add method ArgumentCaptor valueCaptor = ArgumentCaptor.forClass(Double.class); ArgumentCaptor tagsCaptor = ArgumentCaptor.forClass(Tags.class); - // Verify that aggCounter.add was called with the expected arguments - verify(searchQueryCategorizer.searchQueryCounters.aggCounter).add(valueCaptor.capture(), tagsCaptor.capture()); + verify(searchQueryCategorizer.getSearchQueryCounters().getAggCounter()).add(valueCaptor.capture(), tagsCaptor.capture()); double actualValue = valueCaptor.getValue(); String actualTag = (String) tagsCaptor.getValue().getTagsMap().get("type"); @@ -98,8 +96,8 @@ public void testBoolQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("bool")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("bool")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match")).add(eq(1.0d), any(Tags.class)); } public void testFunctionScoreQuery() { @@ -109,7 +107,7 @@ public void testFunctionScoreQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("function_score")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("function_score")).add(eq(1.0d), any(Tags.class)); } public void testMatchQuery() { @@ -119,7 +117,7 @@ public void testMatchQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match")).add(eq(1.0d), any(Tags.class)); } public void testMatchPhraseQuery() { @@ -129,7 +127,7 @@ public void testMatchPhraseQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match_phrase")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match_phrase")).add(eq(1.0d), any(Tags.class)); } public void testMultiMatchQuery() { @@ -139,7 +137,7 @@ public void testMultiMatchQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("multi_match")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("multi_match")).add(eq(1.0d), any(Tags.class)); } public void testOtherQuery() { @@ -153,9 +151,9 @@ public void testOtherQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("boosting")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match_none")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("term")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("boosting")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match_none")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("term")).add(eq(1.0d), any(Tags.class)); } public void testQueryStringQuery() { @@ -166,7 +164,7 @@ public void testQueryStringQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("query_string")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("query_string")).add(eq(1.0d), any(Tags.class)); } public void testRangeQuery() { @@ -178,7 +176,7 @@ public void testRangeQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("range")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("range")).add(eq(1.0d), any(Tags.class)); } public void testRegexQuery() { @@ -187,7 +185,7 @@ public void testRegexQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("regexp")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("regexp")).add(eq(1.0d), any(Tags.class)); } public void testSortQuery() { @@ -198,8 +196,8 @@ public void testSortQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.sortCounter, times(2)).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getSortCounter(), times(2)).add(eq(1.0d), any(Tags.class)); } public void testTermQuery() { @@ -209,7 +207,7 @@ public void testTermQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("term")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("term")).add(eq(1.0d), any(Tags.class)); } public void testWildcardQuery() { @@ -219,7 +217,7 @@ public void testWildcardQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("wildcard")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("wildcard")).add(eq(1.0d), any(Tags.class)); } public void testComplexQuery() { @@ -237,10 +235,10 @@ public void testComplexQuery() { searchQueryCategorizer.categorize(sourceBuilder); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("term")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("match")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("regexp")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.nameToQueryTypeCounters.get("bool")).add(eq(1.0d), any(Tags.class)); - verify(searchQueryCategorizer.searchQueryCounters.aggCounter).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("term")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("match")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("regexp")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getCounterByQueryBuilderName("bool")).add(eq(1.0d), any(Tags.class)); + verify(searchQueryCategorizer.getSearchQueryCounters().getAggCounter()).add(eq(1.0d), any(Tags.class)); } }