Skip to content

Commit

Permalink
Basic query shape related classes
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd committed May 4, 2024
1 parent defbd60 commit d56c5fe
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<? extends AggregationShape> subAggregations;

@Override
public int hashCode() {
if (subAggregations != null) {
Collections.sort(subAggregations);
}
return Objects.hash(fieldName, subAggregations);
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends AggregationShape> subAggregations;

@Override
public int hashCode() {
if (subAggregations != null) {
Collections.sort(subAggregations);
}
return Objects.hash(fieldName, subAggregations);
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends AggregationShape> aggregations;

@Override
public int hashCode() {
Collections.sort(aggregations);
return Objects.hash(aggregations);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> pipelineAggregations;

@Override
public int hashCode() {
Collections.sort(pipelineAggregations);
return Objects.hash(pipelineAggregations);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<SortShapeField> sortShapeFields;

@Override
public int hashCode() {
return Objects.hash(sortShapeFields);
}
}
Original file line number Diff line number Diff line change
@@ -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<AggregationShape> {
@Override
public int compareTo(AggregationShape other) {
return this.getClass().getName().compareTo(other.getClass().getName());
}
}

Original file line number Diff line number Diff line change
@@ -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<QueryBuilderShape> {

@Override
public int compareTo(QueryBuilderShape other) {
return this.getClass().getName().compareTo(other.getClass().getName());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends QueryBuilderShape> filterClause;
List<? extends QueryBuilderShape> mustClause;
List<? extends QueryBuilderShape> mustNotClause;
List<? extends QueryBuilderShape> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d56c5fe

Please sign in to comment.