Skip to content

Commit dcf52c2

Browse files
authored
Add field type to query shape and extensive unit tests (#140)
* Add query type to shape and extensive unit tests Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> * Log query shape if trace log enabled Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> * Refactor code, handle multifield with test, remove properties cache Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> * Use only first index and optimization to get properties once per query Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> * Further refactoring Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> --------- Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
1 parent f89eb88 commit dcf52c2

File tree

6 files changed

+957
-103
lines changed

6 files changed

+957
-103
lines changed

src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
5858
private final ClusterService clusterService;
5959
private boolean groupingFieldNameEnabled;
6060
private boolean groupingFieldTypeEnabled;
61+
private final QueryShapeGenerator queryShapeGenerator;
6162

6263
/**
6364
* Constructor for QueryInsightsListener
@@ -87,6 +88,7 @@ public QueryInsightsListener(
8788
super(initiallyEnabled);
8889
this.clusterService = clusterService;
8990
this.queryInsightsService = queryInsightsService;
91+
this.queryShapeGenerator = new QueryShapeGenerator(clusterService);
9092

9193
// Setting endpoints set up for top n queries, including enabling top n queries, window size, and top n size
9294
// Expected metricTypes are Latency, CPU, and Memory.
@@ -270,9 +272,25 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
270272
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
271273
attributes.put(Attribute.TASK_RESOURCE_USAGES, tasksResourceUsages);
272274

273-
if (queryInsightsService.isGroupingEnabled()) {
274-
String hashcode = QueryShapeGenerator.getShapeHashCodeAsString(request.source(), groupingFieldNameEnabled);
275-
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
275+
if (queryInsightsService.isGroupingEnabled() || log.isTraceEnabled()) {
276+
// Generate the query shape only if grouping is enabled or trace logging is enabled
277+
final String queryShape = queryShapeGenerator.buildShape(
278+
request.source(),
279+
groupingFieldNameEnabled,
280+
groupingFieldTypeEnabled,
281+
searchRequestContext.getSuccessfulSearchShardIndices()
282+
);
283+
284+
// Print the query shape if tracer is enabled
285+
if (log.isTraceEnabled()) {
286+
log.trace("Query Shape:\n{}", queryShape);
287+
}
288+
289+
// Add hashcode attribute when grouping is enabled
290+
if (queryInsightsService.isGroupingEnabled()) {
291+
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(queryShape);
292+
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
293+
}
276294
}
277295

278296
Map<String, Object> labels = new HashMap<>();

0 commit comments

Comments
 (0)