diff --git a/server/src/main/java/org/opensearch/action/search/SearchQueryAggregationCategorizer.java b/server/src/main/java/org/opensearch/action/search/SearchQueryAggregationCategorizer.java index 5aa87dd664d60..10534f6723cec 100644 --- a/server/src/main/java/org/opensearch/action/search/SearchQueryAggregationCategorizer.java +++ b/server/src/main/java/org/opensearch/action/search/SearchQueryAggregationCategorizer.java @@ -1,11 +1,3 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - package org.opensearch.action.search; import org.opensearch.search.aggregations.AggregationBuilder; @@ -29,144 +21,79 @@ import org.opensearch.search.aggregations.bucket.terms.SignificantTermsAggregationBuilder; import org.opensearch.search.aggregations.bucket.terms.SignificantTextAggregationBuilder; import org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder; -import org.opensearch.search.aggregations.metrics.AvgAggregationBuilder; -import org.opensearch.search.aggregations.metrics.CardinalityAggregationBuilder; -import org.opensearch.search.aggregations.metrics.ExtendedStatsAggregationBuilder; -import org.opensearch.search.aggregations.metrics.GeoCentroidAggregationBuilder; -import org.opensearch.search.aggregations.metrics.MaxAggregationBuilder; -import org.opensearch.search.aggregations.metrics.MedianAbsoluteDeviationAggregationBuilder; -import org.opensearch.search.aggregations.metrics.MinAggregationBuilder; -import org.opensearch.search.aggregations.metrics.PercentileRanksAggregationBuilder; -import org.opensearch.search.aggregations.metrics.PercentilesAggregationBuilder; -import org.opensearch.search.aggregations.metrics.ScriptedMetricAggregationBuilder; -import org.opensearch.search.aggregations.metrics.StatsAggregationBuilder; -import org.opensearch.search.aggregations.metrics.SumAggregationBuilder; -import org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder; -import org.opensearch.search.aggregations.metrics.ValueCountAggregationBuilder; -import org.opensearch.search.aggregations.metrics.WeightedAvgAggregationBuilder; +import org.opensearch.search.aggregations.metrics.*; import org.opensearch.telemetry.metrics.tags.Tags; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; +/** + * Increments the counters related to Aggregation Search Queries. + */ public class SearchQueryAggregationCategorizer { + private static final String TYPE_TAG = "type"; - private static final String VALUE_COUNT_AGGREGATION = "value_count"; - private static final String AVG_AGGREGATION = "avg"; - private static final String WEIGHTED_AVG_AGGREGATION = "weighted_avg"; - private static final String MAX_AGGREGATION = "max"; - private static final String MIN_AGGREGATION = "min"; - private static final String SUM_AGGREGATION = "sum"; - private static final String STATS_AGGREGATION = "stats"; - private static final String EXTENDED_STATS_AGGREGATION = "extended_stats"; - private static final String FILTER_AGGREGATION = "filter"; - private static final String FILTERS_AGGREGATION = "filters"; - private static final String ADJACENCY_MATRIX_AGGREGATION = "adjacency_matrix"; - private static final String SAMPLER_AGGREGATION = "sampler"; - private static final String DIVERSIFIED_AGGREGATION = "diversified"; - private static final String GLOBAL_AGGREGATION = "global"; - private static final String MISSING_AGGREGATION = "missing"; - private static final String NESTED_AGGREGATION = "nested"; - private static final String REVERSE_NESTED_AGGREGATION = "reverse_nested"; - private static final String GEO_DISTANCE_AGGREGATION = "geo_distance"; - private static final String HISTOGRAM_AGGREGATION = "histogram"; - private static final String SIGNIFICANT_TERMS_AGGREGATION = "significant_terms"; - private static final String SIGNIFICANT_TEXT_AGGREGATION = "significant_text"; - private static final String DATE_HISTOGRAM_AGGREGATION = "date_histogram"; - private static final String RANGE_AGGREGATION = "range"; - private static final String DATE_RANGE_AGGREGATION = "date_range"; - private static final String IP_RANGE_AGGREGATION = "ip_range"; - private static final String TERMS_AGGREGATION = "terms"; - private static final String PERCENTILES_AGGREGATION = "percentiles"; - private static final String PERCENTILE_RANKS_AGGREGATION = "percentile_ranks"; - private static final String MEDIAN_ABSOLUTE_DEVIATION_AGGREGATION = "median_absolute_deviation"; - private static final String CARDINALITY_AGGREGATION = "cardinality"; - private static final String TOP_HITS_AGGREGATION = "top_hits"; - private static final String GEO_CENTROID_AGGREGATION = "geo_centroid"; - private static final String SCRIPTED_METRIC_AGGREGATION = "scripted_metric"; - private static final String COMPOSITE_AGGREGATION = "composite"; - private static final String MULTI_TERMS_AGGREGATION = "multi_terms"; - private static final String OTHER_AGGREGATION = "other"; private final SearchQueryCounters searchQueryCounters; + private final Map, Function> aggregationHandlers; public SearchQueryAggregationCategorizer(SearchQueryCounters searchQueryCounters) { this.searchQueryCounters = searchQueryCounters; + this.aggregationHandlers = initializeAggregationHandlers(); + } + + private Map, Function> initializeAggregationHandlers() { + Map, Function> handlers = new HashMap<>(); + + handlers.put(TermsAggregationBuilder.class, aggregationBuilder -> ((TermsAggregationBuilder) aggregationBuilder).NAME); + handlers.put(AvgAggregationBuilder.class, aggregationBuilder -> ((AvgAggregationBuilder) aggregationBuilder).NAME); + handlers.put(SumAggregationBuilder.class, aggregationBuilder -> ((SumAggregationBuilder) aggregationBuilder).NAME); + handlers.put(MaxAggregationBuilder.class, aggregationBuilder -> ((MaxAggregationBuilder) aggregationBuilder).NAME); + handlers.put(MinAggregationBuilder.class, aggregationBuilder -> ((MinAggregationBuilder) aggregationBuilder).NAME); + handlers.put(ScriptedMetricAggregationBuilder.class, aggregationBuilder -> ((ScriptedMetricAggregationBuilder) aggregationBuilder).NAME); + handlers.put(ExtendedStatsAggregationBuilder.class, aggregationBuilder -> ((ExtendedStatsAggregationBuilder) aggregationBuilder).NAME); + handlers.put(FilterAggregationBuilder.class, aggregationBuilder -> ((FilterAggregationBuilder) aggregationBuilder).NAME); + handlers.put(FiltersAggregationBuilder.class, aggregationBuilder -> ((FiltersAggregationBuilder) aggregationBuilder).NAME); + handlers.put(AdjacencyMatrixAggregationBuilder.class, aggregationBuilder -> ((AdjacencyMatrixAggregationBuilder) aggregationBuilder).NAME); + handlers.put(SamplerAggregationBuilder.class, aggregationBuilder -> ((SamplerAggregationBuilder) aggregationBuilder).NAME); + handlers.put(DiversifiedAggregationBuilder.class, aggregationBuilder -> ((DiversifiedAggregationBuilder) aggregationBuilder).NAME); + handlers.put(GlobalAggregationBuilder.class, aggregationBuilder -> ((GlobalAggregationBuilder) aggregationBuilder).NAME); + handlers.put(MissingAggregationBuilder.class, aggregationBuilder -> ((MissingAggregationBuilder) aggregationBuilder).NAME); + handlers.put(NestedAggregationBuilder.class, aggregationBuilder -> ((NestedAggregationBuilder) aggregationBuilder).NAME); + handlers.put(ReverseNestedAggregationBuilder.class, aggregationBuilder -> ((ReverseNestedAggregationBuilder) aggregationBuilder).NAME); + handlers.put(GeoDistanceAggregationBuilder.class, aggregationBuilder -> ((GeoDistanceAggregationBuilder) aggregationBuilder).NAME); + handlers.put(HistogramAggregationBuilder.class, aggregationBuilder -> ((HistogramAggregationBuilder) aggregationBuilder).NAME); + handlers.put(SignificantTermsAggregationBuilder.class, aggregationBuilder -> ((SignificantTermsAggregationBuilder) aggregationBuilder).NAME); + handlers.put(SignificantTextAggregationBuilder.class, aggregationBuilder -> ((SignificantTextAggregationBuilder) aggregationBuilder).NAME); + handlers.put(DateHistogramAggregationBuilder.class, aggregationBuilder -> ((DateHistogramAggregationBuilder) aggregationBuilder).NAME); + handlers.put(RangeAggregationBuilder.class, aggregationBuilder -> ((RangeAggregationBuilder) aggregationBuilder).NAME); + handlers.put(DateRangeAggregationBuilder.class, aggregationBuilder -> ((DateRangeAggregationBuilder) aggregationBuilder).NAME); + handlers.put(IpRangeAggregationBuilder.class, aggregationBuilder -> ((IpRangeAggregationBuilder) aggregationBuilder).NAME); + handlers.put(PercentilesAggregationBuilder.class, aggregationBuilder -> ((PercentilesAggregationBuilder) aggregationBuilder).NAME); + handlers.put(PercentileRanksAggregationBuilder.class, aggregationBuilder -> ((PercentileRanksAggregationBuilder) aggregationBuilder).NAME); + handlers.put(MedianAbsoluteDeviationAggregationBuilder.class, aggregationBuilder -> ((MedianAbsoluteDeviationAggregationBuilder) aggregationBuilder).NAME); + handlers.put(CardinalityAggregationBuilder.class, aggregationBuilder -> ((CardinalityAggregationBuilder) aggregationBuilder).NAME); + handlers.put(TopHitsAggregationBuilder.class, aggregationBuilder -> ((TopHitsAggregationBuilder) aggregationBuilder).NAME); + handlers.put(GeoCentroidAggregationBuilder.class, aggregationBuilder -> ((GeoCentroidAggregationBuilder) aggregationBuilder).NAME); + handlers.put(CompositeAggregationBuilder.class, aggregationBuilder -> ((CompositeAggregationBuilder) aggregationBuilder).NAME); + handlers.put(MultiTermsAggregationBuilder.class, aggregationBuilder -> ((MultiTermsAggregationBuilder) aggregationBuilder).NAME); + + return handlers; } public void incrementSearchQueryAggregationCounters(Collection aggregatorFactories) { for (AggregationBuilder aggregationBuilder : aggregatorFactories) { - if (aggregationBuilder instanceof ValueCountAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, VALUE_COUNT_AGGREGATION)); - } else if (aggregationBuilder instanceof AvgAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, AVG_AGGREGATION)); - } else if (aggregationBuilder instanceof WeightedAvgAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, WEIGHTED_AVG_AGGREGATION)); - } else if (aggregationBuilder instanceof MaxAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, MAX_AGGREGATION)); - } else if (aggregationBuilder instanceof MinAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, MIN_AGGREGATION)); - } else if (aggregationBuilder instanceof SumAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, SUM_AGGREGATION)); - } else if (aggregationBuilder instanceof StatsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, STATS_AGGREGATION)); - } else if (aggregationBuilder instanceof ExtendedStatsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, EXTENDED_STATS_AGGREGATION)); - } else if (aggregationBuilder instanceof FilterAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, FILTER_AGGREGATION)); - } else if (aggregationBuilder instanceof FiltersAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, FILTERS_AGGREGATION)); - } else if (aggregationBuilder instanceof AdjacencyMatrixAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, ADJACENCY_MATRIX_AGGREGATION)); - } else if (aggregationBuilder instanceof SamplerAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, SAMPLER_AGGREGATION)); - } else if (aggregationBuilder instanceof DiversifiedAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, DIVERSIFIED_AGGREGATION)); - } else if (aggregationBuilder instanceof GlobalAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, GLOBAL_AGGREGATION)); - } else if (aggregationBuilder instanceof MissingAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, MISSING_AGGREGATION)); - } else if (aggregationBuilder instanceof NestedAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, NESTED_AGGREGATION)); - } else if (aggregationBuilder instanceof ReverseNestedAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, REVERSE_NESTED_AGGREGATION)); - } else if (aggregationBuilder instanceof GeoDistanceAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, GEO_DISTANCE_AGGREGATION)); - } else if (aggregationBuilder instanceof HistogramAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, HISTOGRAM_AGGREGATION)); - } else if (aggregationBuilder instanceof SignificantTermsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, SIGNIFICANT_TERMS_AGGREGATION)); - } else if (aggregationBuilder instanceof SignificantTextAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, SIGNIFICANT_TEXT_AGGREGATION)); - } else if (aggregationBuilder instanceof DateHistogramAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, DATE_HISTOGRAM_AGGREGATION)); - } else if (aggregationBuilder instanceof RangeAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, RANGE_AGGREGATION)); - } else if (aggregationBuilder instanceof DateRangeAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, DATE_RANGE_AGGREGATION)); - } else if (aggregationBuilder instanceof IpRangeAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, IP_RANGE_AGGREGATION)); - } else if (aggregationBuilder instanceof TermsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, TERMS_AGGREGATION)); - } else if (aggregationBuilder instanceof PercentilesAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, PERCENTILES_AGGREGATION)); - } else if (aggregationBuilder instanceof PercentileRanksAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, PERCENTILE_RANKS_AGGREGATION)); - } else if (aggregationBuilder instanceof MedianAbsoluteDeviationAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, MEDIAN_ABSOLUTE_DEVIATION_AGGREGATION)); - } else if (aggregationBuilder instanceof CardinalityAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, CARDINALITY_AGGREGATION)); - } else if (aggregationBuilder instanceof TopHitsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, TOP_HITS_AGGREGATION)); - } else if (aggregationBuilder instanceof GeoCentroidAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, GEO_CENTROID_AGGREGATION)); - } else if (aggregationBuilder instanceof ScriptedMetricAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, SCRIPTED_METRIC_AGGREGATION)); - } else if (aggregationBuilder instanceof CompositeAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, COMPOSITE_AGGREGATION)); - } else if (aggregationBuilder instanceof MultiTermsAggregationBuilder) { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, MULTI_TERMS_AGGREGATION)); - } else { - searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, OTHER_AGGREGATION)); - } + String aggregationType = getAggregationType(aggregationBuilder); + searchQueryCounters.aggCounter.add(1, Tags.create().addTag(TYPE_TAG, aggregationType)); } } + + private String getAggregationType(AggregationBuilder aggregationBuilder) { + return aggregationHandlers.getOrDefault(aggregationBuilder.getClass(), this::handleUnknownAggregation).apply(aggregationBuilder); + } + + private String handleUnknownAggregation(AggregationBuilder aggregationBuilder) { + return "other"; + } } diff --git a/server/src/main/java/org/opensearch/action/search/SearchQueryCategorizingVisitor.java b/server/src/main/java/org/opensearch/action/search/SearchQueryCategorizingVisitor.java index d03999fe4cc51..ad885ad07537f 100644 --- a/server/src/main/java/org/opensearch/action/search/SearchQueryCategorizingVisitor.java +++ b/server/src/main/java/org/opensearch/action/search/SearchQueryCategorizingVisitor.java @@ -1,52 +1,23 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - package org.opensearch.action.search; import org.apache.lucene.search.BooleanClause; -import org.opensearch.index.query.BoolQueryBuilder; -import org.opensearch.index.query.BoostingQueryBuilder; -import org.opensearch.index.query.ConstantScoreQueryBuilder; -import org.opensearch.index.query.DisMaxQueryBuilder; -import org.opensearch.index.query.DistanceFeatureQueryBuilder; -import org.opensearch.index.query.ExistsQueryBuilder; -import org.opensearch.index.query.FieldMaskingSpanQueryBuilder; -import org.opensearch.index.query.FuzzyQueryBuilder; -import org.opensearch.index.query.GeoBoundingBoxQueryBuilder; -import org.opensearch.index.query.GeoDistanceQueryBuilder; -import org.opensearch.index.query.GeoPolygonQueryBuilder; -import org.opensearch.index.query.GeoShapeQueryBuilder; -import org.opensearch.index.query.IntervalQueryBuilder; -import org.opensearch.index.query.MatchAllQueryBuilder; -import org.opensearch.index.query.MatchPhraseQueryBuilder; -import org.opensearch.index.query.MatchQueryBuilder; -import org.opensearch.index.query.MultiMatchQueryBuilder; -import org.opensearch.index.query.PrefixQueryBuilder; -import org.opensearch.index.query.QueryBuilder; -import org.opensearch.index.query.QueryBuilderVisitor; -import org.opensearch.index.query.QueryStringQueryBuilder; -import org.opensearch.index.query.RangeQueryBuilder; -import org.opensearch.index.query.RegexpQueryBuilder; -import org.opensearch.index.query.ScriptQueryBuilder; -import org.opensearch.index.query.SimpleQueryStringBuilder; -import org.opensearch.index.query.TermQueryBuilder; -import org.opensearch.index.query.WildcardQueryBuilder; +import org.opensearch.index.query.*; import org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.opensearch.telemetry.metrics.tags.Tags; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + /** - * Class to visit the querybuilder tree and also track the level information. + * Class to visit the query builder tree and also track the level information. * Increments the counters related to Search Query type. */ final class SearchQueryCategorizingVisitor implements QueryBuilderVisitor { private static final String LEVEL_TAG = "level"; private final int level; private final SearchQueryCounters searchQueryCounters; + private final Map, Consumer> queryHandlers; public SearchQueryCategorizingVisitor(SearchQueryCounters searchQueryCounters) { this(searchQueryCounters, 0); @@ -55,68 +26,155 @@ public SearchQueryCategorizingVisitor(SearchQueryCounters searchQueryCounters) { private SearchQueryCategorizingVisitor(SearchQueryCounters counters, int level) { this.searchQueryCounters = counters; this.level = level; + this.queryHandlers = initializeQueryHandlers(); + } + + private Map, Consumer> initializeQueryHandlers() { + Map, Consumer> handlers = new HashMap<>(); + + handlers.put(BoolQueryBuilder.class, this::handleBoolQuery); + handlers.put(FunctionScoreQueryBuilder.class, this::handleFunctionScoreQuery); + handlers.put(MatchQueryBuilder.class, this::handleMatchQuery); + handlers.put(MatchPhraseQueryBuilder.class, this::handleMatchPhrasePrefixQuery); + handlers.put(MultiMatchQueryBuilder.class, this::handleMultiMatchQuery); + handlers.put(QueryStringQueryBuilder.class, this::handleQueryStringQuery); + handlers.put(RangeQueryBuilder.class, this::handleRangeQuery); + handlers.put(RegexpQueryBuilder.class, this::handleRegexpQuery); + handlers.put(TermQueryBuilder.class, this::handleTermQuery); + handlers.put(WildcardQueryBuilder.class, this::handleWildcardQuery); + handlers.put(BoostingQueryBuilder.class, this::handleBoostingQuery); + handlers.put(ConstantScoreQueryBuilder.class, this::handleConstantScoreQuery); + handlers.put(DisMaxQueryBuilder.class, this::handleDisMaxQuery); + handlers.put(DistanceFeatureQueryBuilder.class, this::handleDistanceFeatureQuery); + handlers.put(ExistsQueryBuilder.class, this::handleExistsQuery); + handlers.put(FieldMaskingSpanQueryBuilder.class, this::handleFieldMaskingSpanQuery); + handlers.put(FuzzyQueryBuilder.class, this::handleFuzzyQuery); + handlers.put(GeoBoundingBoxQueryBuilder.class, this::handleGeoBoundingBoxQuery); + handlers.put(GeoDistanceQueryBuilder.class, this::handleGeoDistanceQuery); + handlers.put(GeoPolygonQueryBuilder.class, this::handleGeoPolygonQuery); + handlers.put(GeoShapeQueryBuilder.class, this::handleGeoShapeQuery); + handlers.put(IntervalQueryBuilder.class, this::handleIntervalQuery); + handlers.put(MatchAllQueryBuilder.class, this::handleMatchAllQuery); + handlers.put(PrefixQueryBuilder.class, this::handlePrefixQuery); + handlers.put(ScriptQueryBuilder.class, this::handleScriptQuery); + handlers.put(SimpleQueryStringBuilder.class, this::handleSimpleQueryStringQuery); + + return handlers; } public void accept(QueryBuilder qb) { - if (qb instanceof BoolQueryBuilder) { - searchQueryCounters.boolCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof FunctionScoreQueryBuilder) { - searchQueryCounters.functionScoreCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof MatchQueryBuilder) { - searchQueryCounters.matchCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof MatchPhraseQueryBuilder) { - searchQueryCounters.matchPhrasePrefixCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof MultiMatchQueryBuilder) { - searchQueryCounters.multiMatchCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof QueryStringQueryBuilder) { - searchQueryCounters.queryStringQueryCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof RangeQueryBuilder) { - searchQueryCounters.rangeCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof RegexpQueryBuilder) { - searchQueryCounters.regexCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof TermQueryBuilder) { - searchQueryCounters.termCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof WildcardQueryBuilder) { - searchQueryCounters.wildcardCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } - if (qb instanceof BoostingQueryBuilder) { - searchQueryCounters.boostCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof ConstantScoreQueryBuilder) { - searchQueryCounters.constantScoreCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof DisMaxQueryBuilder) { - searchQueryCounters.disMaxCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof DistanceFeatureQueryBuilder) { - searchQueryCounters.distanceFeatureCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof ExistsQueryBuilder) { - searchQueryCounters.existsCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof FieldMaskingSpanQueryBuilder) { - searchQueryCounters.fieldMaskingSpanCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof FuzzyQueryBuilder) { - searchQueryCounters.fuzzyCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof GeoBoundingBoxQueryBuilder) { - searchQueryCounters.geoBoundingBoxCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof GeoDistanceQueryBuilder) { - searchQueryCounters.geoDistanceCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof GeoPolygonQueryBuilder) { - searchQueryCounters.geoPolygonCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof GeoShapeQueryBuilder) { - searchQueryCounters.geoShapeCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof IntervalQueryBuilder) { - searchQueryCounters.intervalCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof MatchAllQueryBuilder) { - searchQueryCounters.matchallCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof PrefixQueryBuilder) { - searchQueryCounters.prefixCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof ScriptQueryBuilder) { - searchQueryCounters.scriptCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else if (qb instanceof SimpleQueryStringBuilder) { - searchQueryCounters.simpleQueryStringCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } else { - searchQueryCounters.otherQueryCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); - } + queryHandlers.getOrDefault(qb.getClass(), this::handleOtherQuery).accept(qb); } public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { return new SearchQueryCategorizingVisitor(searchQueryCounters, level + 1); } + + private void handleBoolQuery(QueryBuilder qb) { + searchQueryCounters.boolCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleFunctionScoreQuery(QueryBuilder qb) { + searchQueryCounters.functionScoreCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleMatchQuery(QueryBuilder qb) { + searchQueryCounters.matchCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleMatchPhrasePrefixQuery(QueryBuilder qb) { + searchQueryCounters.matchPhrasePrefixCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleMultiMatchQuery(QueryBuilder qb) { + searchQueryCounters.multiMatchCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleQueryStringQuery(QueryBuilder qb) { + searchQueryCounters.queryStringQueryCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleRangeQuery(QueryBuilder qb) { + searchQueryCounters.rangeCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleRegexpQuery(QueryBuilder qb) { + searchQueryCounters.regexCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleTermQuery(QueryBuilder qb) { + searchQueryCounters.termCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleWildcardQuery(QueryBuilder qb) { + searchQueryCounters.wildcardCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleBoostingQuery(QueryBuilder qb) { + searchQueryCounters.boostCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleConstantScoreQuery(QueryBuilder qb) { + searchQueryCounters.constantScoreCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleDisMaxQuery(QueryBuilder qb) { + searchQueryCounters.disMaxCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleDistanceFeatureQuery(QueryBuilder qb) { + searchQueryCounters.distanceFeatureCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleExistsQuery(QueryBuilder qb) { + searchQueryCounters.existsCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleFieldMaskingSpanQuery(QueryBuilder qb) { + searchQueryCounters.fieldMaskingSpanCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleFuzzyQuery(QueryBuilder qb) { + searchQueryCounters.fuzzyCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleGeoBoundingBoxQuery(QueryBuilder qb) { + searchQueryCounters.geoBoundingBoxCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleGeoDistanceQuery(QueryBuilder qb) { + searchQueryCounters.geoDistanceCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleGeoPolygonQuery(QueryBuilder qb) { + searchQueryCounters.geoPolygonCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleGeoShapeQuery(QueryBuilder qb) { + searchQueryCounters.geoShapeCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleIntervalQuery(QueryBuilder qb) { + searchQueryCounters.intervalCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleMatchAllQuery(QueryBuilder qb) { + searchQueryCounters.matchallCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handlePrefixQuery(QueryBuilder qb) { + searchQueryCounters.prefixCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleScriptQuery(QueryBuilder qb) { + searchQueryCounters.scriptCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleSimpleQueryStringQuery(QueryBuilder qb) { + searchQueryCounters.simpleQueryStringCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } + + private void handleOtherQuery(QueryBuilder qb) { + searchQueryCounters.otherQueryCounter.add(1, Tags.create().addTag(LEVEL_TAG, level)); + } }