-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query instrumentation Minor Enhancements (#73)
* Query instrumentation minor fixes Signed-off-by: Siddhant Deshmukh <[email protected]> * Fix unit tests Signed-off-by: Siddhant Deshmukh <[email protected]> * Rename package Signed-off-by: Siddhant Deshmukh <[email protected]> * Spotless Signed-off-by: Siddhant Deshmukh <[email protected]> --------- Signed-off-by: Siddhant Deshmukh <[email protected]>
- Loading branch information
Showing
5 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
...ava/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeGeneratorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
* 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.core.service.categorizer; | ||
|
||
import org.opensearch.common.hash.MurmurHash3; | ||
import org.opensearch.plugin.insights.SearchSourceBuilderUtils; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
public final class QueryShapeGeneratorTests extends OpenSearchTestCase { | ||
|
||
public void testComplexSearch() { | ||
SearchSourceBuilder sourceBuilder = SearchSourceBuilderUtils.createDefaultSearchSourceBuilder(); | ||
|
||
String shapeShowFieldsTrue = QueryShapeGenerator.buildShape(sourceBuilder, true); | ||
String expectedShowFieldsTrue = "bool []\n" | ||
+ " must:\n" | ||
+ " term [field1]\n" | ||
+ " filter:\n" | ||
+ " match [field2]\n" | ||
+ " range [field4]\n" | ||
+ " should:\n" | ||
+ " regexp [field3]\n" | ||
+ "aggregation:\n" | ||
+ " significant_text []\n" | ||
+ " terms [key]\n" | ||
+ " aggregation:\n" | ||
+ " terms [key.sub1]\n" | ||
+ " terms [key.sub2]\n" | ||
+ " pipeline aggregation:\n" | ||
+ " max_bucket\n" | ||
+ " terms [model]\n" | ||
+ " terms [type]\n" | ||
+ " aggregation:\n" | ||
+ " terms [key.sub3]\n" | ||
+ " pipeline aggregation:\n" | ||
+ " derivative\n" | ||
+ " top_hits []\n" | ||
+ " pipeline aggregation:\n" | ||
+ " avg_bucket\n" | ||
+ " derivative\n" | ||
+ " max_bucket\n" | ||
+ "sort:\n" | ||
+ " asc [album]\n" | ||
+ " asc [price]\n" | ||
+ " desc [color]\n" | ||
+ " desc [vendor]\n"; | ||
assertEquals(expectedShowFieldsTrue, shapeShowFieldsTrue); | ||
|
||
String shapeShowFieldsFalse = QueryShapeGenerator.buildShape(sourceBuilder, false); | ||
String expectedShowFieldsFalse = "bool\n" | ||
+ " must:\n" | ||
+ " term\n" | ||
+ " filter:\n" | ||
+ " match\n" | ||
+ " range\n" | ||
+ " should:\n" | ||
+ " regexp\n" | ||
+ "aggregation:\n" | ||
+ " significant_text\n" | ||
+ " terms\n" | ||
+ " terms\n" | ||
+ " aggregation:\n" | ||
+ " terms\n" | ||
+ " pipeline aggregation:\n" | ||
+ " derivative\n" | ||
+ " terms\n" | ||
+ " aggregation:\n" | ||
+ " terms\n" | ||
+ " terms\n" | ||
+ " pipeline aggregation:\n" | ||
+ " max_bucket\n" | ||
+ " top_hits\n" | ||
+ " pipeline aggregation:\n" | ||
+ " avg_bucket\n" | ||
+ " derivative\n" | ||
+ " max_bucket\n" | ||
+ "sort:\n" | ||
+ " asc\n" | ||
+ " asc\n" | ||
+ " desc\n" | ||
+ " desc\n"; | ||
assertEquals(expectedShowFieldsFalse, shapeShowFieldsFalse); | ||
} | ||
|
||
public void testQueryShape() { | ||
SearchSourceBuilder sourceBuilder = SearchSourceBuilderUtils.createQuerySearchSourceBuilder(); | ||
|
||
String shapeShowFieldsTrue = QueryShapeGenerator.buildShape(sourceBuilder, true); | ||
String expectedShowFieldsTrue = "bool []\n" | ||
+ " must:\n" | ||
+ " term [field1]\n" | ||
+ " filter:\n" | ||
+ " match [field2]\n" | ||
+ " range [field4]\n" | ||
+ " should:\n" | ||
+ " regexp [field3]\n"; | ||
assertEquals(expectedShowFieldsTrue, shapeShowFieldsTrue); | ||
|
||
String shapeShowFieldsFalse = QueryShapeGenerator.buildShape(sourceBuilder, false); | ||
String expectedShowFieldsFalse = "bool\n" | ||
+ " must:\n" | ||
+ " term\n" | ||
+ " filter:\n" | ||
+ " match\n" | ||
+ " range\n" | ||
+ " should:\n" | ||
+ " regexp\n"; | ||
assertEquals(expectedShowFieldsFalse, shapeShowFieldsFalse); | ||
} | ||
|
||
public void testAggregationShape() { | ||
SearchSourceBuilder sourceBuilder = SearchSourceBuilderUtils.createAggregationSearchSourceBuilder(); | ||
|
||
String shapeShowFieldsTrue = QueryShapeGenerator.buildShape(sourceBuilder, true); | ||
String expectedShowFieldsTrue = "aggregation:\n" | ||
+ " significant_text []\n" | ||
+ " terms [key]\n" | ||
+ " aggregation:\n" | ||
+ " terms [key.sub1]\n" | ||
+ " terms [key.sub2]\n" | ||
+ " pipeline aggregation:\n" | ||
+ " max_bucket\n" | ||
+ " terms [model]\n" | ||
+ " terms [type]\n" | ||
+ " aggregation:\n" | ||
+ " terms [key.sub3]\n" | ||
+ " pipeline aggregation:\n" | ||
+ " derivative\n" | ||
+ " top_hits []\n" | ||
+ " pipeline aggregation:\n" | ||
+ " avg_bucket\n" | ||
+ " derivative\n" | ||
+ " max_bucket\n"; | ||
assertEquals(expectedShowFieldsTrue, shapeShowFieldsTrue); | ||
|
||
String shapeShowFieldsFalse = QueryShapeGenerator.buildShape(sourceBuilder, false); | ||
String expectedShowFieldsFalse = "aggregation:\n" | ||
+ " significant_text\n" | ||
+ " terms\n" | ||
+ " terms\n" | ||
+ " aggregation:\n" | ||
+ " terms\n" | ||
+ " pipeline aggregation:\n" | ||
+ " derivative\n" | ||
+ " terms\n" | ||
+ " aggregation:\n" | ||
+ " terms\n" | ||
+ " terms\n" | ||
+ " pipeline aggregation:\n" | ||
+ " max_bucket\n" | ||
+ " top_hits\n" | ||
+ " pipeline aggregation:\n" | ||
+ " avg_bucket\n" | ||
+ " derivative\n" | ||
+ " max_bucket\n"; | ||
assertEquals(expectedShowFieldsFalse, shapeShowFieldsFalse); | ||
} | ||
|
||
public void testSortShape() { | ||
SearchSourceBuilder sourceBuilder = SearchSourceBuilderUtils.createSortSearchSourceBuilder(); | ||
|
||
String shapeShowFieldsTrue = QueryShapeGenerator.buildShape(sourceBuilder, true); | ||
String expectedShowFieldsTrue = "sort:\n" + " asc [album]\n" + " asc [price]\n" + " desc [color]\n" + " desc [vendor]\n"; | ||
assertEquals(expectedShowFieldsTrue, shapeShowFieldsTrue); | ||
|
||
String shapeShowFieldsFalse = QueryShapeGenerator.buildShape(sourceBuilder, false); | ||
String expectedShowFieldsFalse = "sort:\n" + " asc\n" + " asc\n" + " desc\n" + " desc\n"; | ||
assertEquals(expectedShowFieldsFalse, shapeShowFieldsFalse); | ||
} | ||
|
||
public void testHashCode() { | ||
// Create test source builders | ||
SearchSourceBuilder defaultSourceBuilder = SearchSourceBuilderUtils.createDefaultSearchSourceBuilder(); | ||
SearchSourceBuilder querySourceBuilder = SearchSourceBuilderUtils.createQuerySearchSourceBuilder(); | ||
|
||
// showFields true | ||
MurmurHash3.Hash128 defaultHashTrue = QueryShapeGenerator.getShapeHashCode(defaultSourceBuilder, true); | ||
MurmurHash3.Hash128 queryHashTrue = QueryShapeGenerator.getShapeHashCode(querySourceBuilder, true); | ||
assertEquals(defaultHashTrue, QueryShapeGenerator.getShapeHashCode(defaultSourceBuilder, true)); | ||
assertEquals(queryHashTrue, QueryShapeGenerator.getShapeHashCode(querySourceBuilder, true)); | ||
assertNotEquals(defaultHashTrue, queryHashTrue); | ||
|
||
// showFields false | ||
MurmurHash3.Hash128 defaultHashFalse = QueryShapeGenerator.getShapeHashCode(defaultSourceBuilder, false); | ||
MurmurHash3.Hash128 queryHashFalse = QueryShapeGenerator.getShapeHashCode(querySourceBuilder, false); | ||
assertEquals(defaultHashFalse, QueryShapeGenerator.getShapeHashCode(defaultSourceBuilder, false)); | ||
assertEquals(queryHashFalse, QueryShapeGenerator.getShapeHashCode(querySourceBuilder, false)); | ||
assertNotEquals(defaultHashFalse, queryHashFalse); | ||
|
||
// Compare field data on vs off | ||
assertNotEquals(defaultHashTrue, defaultHashFalse); | ||
assertNotEquals(queryHashTrue, queryHashFalse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters