From d56c5fe3a79222fa5a9dcd612f9a9f2dbe1fbace Mon Sep 17 00:00:00 2001 From: Siddhant Deshmukh Date: Sat, 4 May 2024 07:11:06 -0700 Subject: [PATCH] Basic query shape related classes Signed-off-by: Siddhant Deshmukh --- .../aggregations/SumAggregationShape.java | 28 ++++++++++++ .../aggregations/TermsAggregationShape.java | 28 ++++++++++++ .../queryshape/core/AggregationFullShape.java | 25 +++++++++++ .../core/PipelineAggregationShape.java | 23 ++++++++++ .../core/QueryBuilderFullShape.java | 22 ++++++++++ .../model/queryshape/core/QueryShape.java | 41 +++++++++++++++++ .../model/queryshape/core/SortShape.java | 23 ++++++++++ .../queryshape/misc/AggregationShape.java | 17 +++++++ .../queryshape/misc/QueryBuilderShape.java | 17 +++++++ .../model/queryshape/misc/SortShapeField.java | 21 +++++++++ .../queries/BoolQueryBuilderShape.java | 44 +++++++++++++++++++ .../queries/ExistsQueryBuilderShape.java | 22 ++++++++++ .../queries/MatchQueryBuilderShape.java | 22 ++++++++++ .../queries/MustQueryBuilderShape.java | 22 ++++++++++ .../queries/TermQueryBuilderShape.java | 22 ++++++++++ 15 files changed, 377 insertions(+) create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/SumAggregationShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/TermsAggregationShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/AggregationFullShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/PipelineAggregationShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryBuilderFullShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/SortShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/AggregationShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/QueryBuilderShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/SortShapeField.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/BoolQueryBuilderShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/ExistsQueryBuilderShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MatchQueryBuilderShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MustQueryBuilderShape.java create mode 100644 plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/TermQueryBuilderShape.java diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/SumAggregationShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/SumAggregationShape.java new file mode 100644 index 0000000000000..012d15b6c9712 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/SumAggregationShape.java @@ -0,0 +1,28 @@ +/* + * 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.plugin.insights.rules.model.queryshape.aggregations; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.AggregationShape; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class SumAggregationShape extends AggregationShape { + String fieldName; + List subAggregations; + + @Override + public int hashCode() { + if (subAggregations != null) { + Collections.sort(subAggregations); + } + return Objects.hash(fieldName, subAggregations); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/TermsAggregationShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/TermsAggregationShape.java new file mode 100644 index 0000000000000..07fabbb580037 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/aggregations/TermsAggregationShape.java @@ -0,0 +1,28 @@ +/* + * 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.plugin.insights.rules.model.queryshape.aggregations; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.AggregationShape; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class TermsAggregationShape extends AggregationShape { + String fieldName; + List subAggregations; + + @Override + public int hashCode() { + if (subAggregations != null) { + Collections.sort(subAggregations); + } + return Objects.hash(fieldName, subAggregations); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/AggregationFullShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/AggregationFullShape.java new file mode 100644 index 0000000000000..7c14b649b5c12 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/AggregationFullShape.java @@ -0,0 +1,25 @@ +/* + * 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.plugin.insights.rules.model.queryshape.core; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.AggregationShape; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +class AggregationFullShape { + List aggregations; + + @Override + public int hashCode() { + Collections.sort(aggregations); + return Objects.hash(aggregations); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/PipelineAggregationShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/PipelineAggregationShape.java new file mode 100644 index 0000000000000..7d8205fba62f8 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/PipelineAggregationShape.java @@ -0,0 +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.plugin.insights.rules.model.queryshape.core; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +class PipelineAggregationShape { + List pipelineAggregations; + + @Override + public int hashCode() { + Collections.sort(pipelineAggregations); + return Objects.hash(pipelineAggregations); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryBuilderFullShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryBuilderFullShape.java new file mode 100644 index 0000000000000..69c777840b585 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryBuilderFullShape.java @@ -0,0 +1,22 @@ +/* + * 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.plugin.insights.rules.model.queryshape.core; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Objects; + +class QueryBuilderFullShape { + QueryBuilderShape queryBuilderShape; + + @Override + public int hashCode() { + return Objects.hash(queryBuilderShape); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryShape.java new file mode 100644 index 0000000000000..1051ea319be36 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/QueryShape.java @@ -0,0 +1,41 @@ +/* + * 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.plugin.insights.rules.model.queryshape.core; + +import org.opensearch.index.query.QueryBuilder; +import org.opensearch.search.builder.SearchSourceBuilder; + +import java.util.Objects; + +/** + * Representation of a Query Shape primarily used to group Top N queries by latency and resource usage. + * https://github.com/opensearch-project/OpenSearch/issues/13357 + * @opensearch.internal + */ +public class QueryShape { + private QueryBuilderFullShape queryBuilderFullShape; + private SortShape sortShape; + private AggregationFullShape aggregationFullShape; + private PipelineAggregationShape pipelineAggregationShape; + + @Override + public int hashCode() { + return Objects.hash(queryBuilderFullShape, sortShape, aggregationFullShape, pipelineAggregationShape); + } + + public void parse(SearchSourceBuilder source) { + // Parse the QueryBuilder to QueryShape + // Populate QueryBuilderFullShape, SortShape, AggregationFullShape, PipelineAggregationShape + } + + public String getStringQueryShape() { + // Provide the String Query Shape + return null; + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/SortShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/SortShape.java new file mode 100644 index 0000000000000..5e68e61e24915 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/core/SortShape.java @@ -0,0 +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.plugin.insights.rules.model.queryshape.core; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.SortShapeField; + +import java.util.List; +import java.util.Objects; + +class SortShape { + List sortShapeFields; + + @Override + public int hashCode() { + return Objects.hash(sortShapeFields); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/AggregationShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/AggregationShape.java new file mode 100644 index 0000000000000..7d0b40a95ccb4 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/AggregationShape.java @@ -0,0 +1,17 @@ +/* + * 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.plugin.insights.rules.model.queryshape.misc; + +public abstract class AggregationShape implements Comparable { + @Override + public int compareTo(AggregationShape other) { + return this.getClass().getName().compareTo(other.getClass().getName()); + } +} + diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/QueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/QueryBuilderShape.java new file mode 100644 index 0000000000000..72cff9e882e36 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/QueryBuilderShape.java @@ -0,0 +1,17 @@ +/* + * 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.plugin.insights.rules.model.queryshape.misc; + +public abstract class QueryBuilderShape implements Comparable { + + @Override + public int compareTo(QueryBuilderShape other) { + return this.getClass().getName().compareTo(other.getClass().getName()); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/SortShapeField.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/SortShapeField.java new file mode 100644 index 0000000000000..ab36df907133f --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/misc/SortShapeField.java @@ -0,0 +1,21 @@ +/* + * 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.plugin.insights.rules.model.queryshape.misc; + +import java.util.Objects; + +public class SortShapeField { + String fieldName; + String sortOrder; + + @Override + public int hashCode() { + return Objects.hash(fieldName, sortOrder); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/BoolQueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/BoolQueryBuilderShape.java new file mode 100644 index 0000000000000..28813aa588b66 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/BoolQueryBuilderShape.java @@ -0,0 +1,44 @@ +/* + * 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.plugin.insights.rules.model.queryshape.queries; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class BoolQueryBuilderShape extends QueryBuilderShape { + List filterClause; + List mustClause; + List mustNotClause; + List shouldClause; + + @Override + public int hashCode() { + // Sort the lists before calculating the hash code + sortLists(); + return Objects.hash(filterClause, mustClause, mustNotClause, shouldClause); + } + + private void sortLists() { + if (filterClause != null) { + Collections.sort(filterClause); + } + if (mustClause != null) { + Collections.sort(mustClause); + } + if (mustNotClause != null) { + Collections.sort(mustNotClause); + } + if (shouldClause != null) { + Collections.sort(shouldClause); + } + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/ExistsQueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/ExistsQueryBuilderShape.java new file mode 100644 index 0000000000000..5a3d45f0413d4 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/ExistsQueryBuilderShape.java @@ -0,0 +1,22 @@ +/* + * 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.plugin.insights.rules.model.queryshape.queries; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Objects; + +public class ExistsQueryBuilderShape extends QueryBuilderShape { + String fieldName; + String fieldValue; + @Override + public int hashCode() { + return Objects.hash(fieldName, fieldValue); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MatchQueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MatchQueryBuilderShape.java new file mode 100644 index 0000000000000..d7b073b15e282 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MatchQueryBuilderShape.java @@ -0,0 +1,22 @@ +/* + * 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.plugin.insights.rules.model.queryshape.queries; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Objects; + +public class MatchQueryBuilderShape extends QueryBuilderShape { + String fieldName; + String fieldValue; + @Override + public int hashCode() { + return Objects.hash(fieldName, fieldValue); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MustQueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MustQueryBuilderShape.java new file mode 100644 index 0000000000000..634a7b09ce7e6 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/MustQueryBuilderShape.java @@ -0,0 +1,22 @@ +/* + * 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.plugin.insights.rules.model.queryshape.queries; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Objects; + +public class MustQueryBuilderShape extends QueryBuilderShape { + String fieldName; + String fieldValue; + @Override + public int hashCode() { + return Objects.hash(fieldName, fieldValue); + } +} diff --git a/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/TermQueryBuilderShape.java b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/TermQueryBuilderShape.java new file mode 100644 index 0000000000000..afb98d9008400 --- /dev/null +++ b/plugins/query-insights/src/main/java/org/opensearch/plugin/insights/rules/model/queryshape/queries/TermQueryBuilderShape.java @@ -0,0 +1,22 @@ +/* + * 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.plugin.insights.rules.model.queryshape.queries; + +import org.opensearch.plugin.insights.rules.model.queryshape.misc.QueryBuilderShape; + +import java.util.Objects; + +public class TermQueryBuilderShape extends QueryBuilderShape { + String fieldName; + String fieldValue; + @Override + public int hashCode() { + return Objects.hash(fieldName, fieldValue); + } +}