Skip to content

Commit 9810982

Browse files
authoredJan 15, 2025
Make sure query_group_hashcode is present in top_queries response in all cases (#187)
* Make sure query_group_hashcode is present in top_queries response in all cases Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> * Rename query_group_hashcoe to id Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> --------- Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
1 parent 6ec873c commit 9810982

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
288288
// Add hashcode attribute when grouping is enabled
289289
if (queryInsightsService.isGroupingEnabled()) {
290290
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(queryShape);
291-
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
291+
attributes.put(Attribute.ID, hashcode);
292292
}
293293
}
294294

‎src/main/java/org/opensearch/plugin/insights/core/service/grouper/MinMaxHeapQueryGrouper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ int numberOfTopGroups() {
271271
private String getGroupingId(final SearchQueryRecord searchQueryRecord) {
272272
switch (groupingType) {
273273
case SIMILARITY:
274-
return searchQueryRecord.getAttributes().get(Attribute.QUERY_HASHCODE).toString();
274+
return searchQueryRecord.getAttributes().get(Attribute.ID).toString();
275275
case NONE:
276276
throw new IllegalArgumentException("Should not try to group queries if grouping type is NONE");
277277
default:

‎src/main/java/org/opensearch/plugin/insights/rules/model/Attribute.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public enum Attribute {
5858
*/
5959
LABELS,
6060
/**
61-
* Unique hashcode used to group similar queries
61+
* Query Group hashcode or query hashcode representing a unique identifier for the query/group
6262
*/
63-
QUERY_HASHCODE,
63+
ID,
6464
/**
6565
* Grouping type of the query record (none, similarity)
6666
*/

‎src/main/java/org/opensearch/plugin/insights/rules/model/SearchQueryRecord.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
package org.opensearch.plugin.insights.rules.model;
1010

11-
import static org.opensearch.plugin.insights.rules.model.Attribute.GROUP_BY;
12-
1311
import java.io.IOException;
1412
import java.util.ArrayList;
1513
import java.util.HashMap;
@@ -96,6 +94,11 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
9694
*/
9795
public static final String GROUP_BY = "group_by";
9896

97+
/**
98+
* Query Group hashcode or query hashcode representing a unique identifier for the query/group
99+
*/
100+
public static final String ID = "id";
101+
99102
public static final String MEASUREMENTS = "measurements";
100103
private String groupingId;
101104

@@ -176,6 +179,9 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
176179
case GROUP_BY:
177180
attributes.put(Attribute.GROUP_BY, parser.text());
178181
break;
182+
case ID:
183+
attributes.put(Attribute.ID, parser.text());
184+
break;
179185
case SOURCE:
180186
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
181187
attributes.put(Attribute.SOURCE, SearchSourceBuilder.fromXContent(parser, false));

‎src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static List<SearchQueryRecord> generateQueryInsightRecords(
139139
attributes.put(Attribute.TOTAL_SHARDS, randomIntBetween(1, 100));
140140
attributes.put(Attribute.INDICES, randomArray(1, 3, Object[]::new, () -> randomAlphaOfLengthBetween(5, 10)));
141141
attributes.put(Attribute.PHASE_LATENCY_MAP, phaseLatencyMap);
142-
attributes.put(Attribute.QUERY_HASHCODE, Objects.hashCode(i));
142+
attributes.put(Attribute.ID, Objects.hashCode(i));
143143
attributes.put(Attribute.GROUP_BY, GroupingType.NONE);
144144
attributes.put(
145145
Attribute.TASK_RESOURCE_USAGES,
@@ -200,13 +200,13 @@ public static List<List<SearchQueryRecord>> generateMultipleQueryInsightsRecords
200200

201201
public static void populateSameQueryHashcodes(List<SearchQueryRecord> searchQueryRecords) {
202202
for (SearchQueryRecord record : searchQueryRecords) {
203-
record.getAttributes().put(Attribute.QUERY_HASHCODE, 1);
203+
record.getAttributes().put(Attribute.ID, 1);
204204
}
205205
}
206206

207207
public static void populateHashcode(List<SearchQueryRecord> searchQueryRecords, int hash) {
208208
for (SearchQueryRecord record : searchQueryRecords) {
209-
record.getAttributes().put(Attribute.QUERY_HASHCODE, hash);
209+
record.getAttributes().put(Attribute.ID, hash);
210210
}
211211
}
212212

‎src/test/java/org/opensearch/plugin/insights/core/service/grouper/MinMaxHeapQueryGrouperTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testWithAllDifferentHashcodes() {
4444
Set<Integer> hashcodeSet = new HashSet<>();
4545
for (SearchQueryRecord record : records) {
4646
groupedRecord = minMaxHeapQueryGrouper.add(record);
47-
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.QUERY_HASHCODE);
47+
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.ID);
4848
hashcodeSet.add(hashcode);
4949
}
5050
assertEquals(numOfRecords, hashcodeSet.size());
@@ -58,7 +58,7 @@ public void testWithAllSameHashcodes() {
5858
Set<Integer> hashcodeSet = new HashSet<>();
5959
for (SearchQueryRecord record : records) {
6060
groupedRecord = minMaxHeapQueryGrouper.add(record);
61-
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.QUERY_HASHCODE);
61+
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.ID);
6262
hashcodeSet.add(hashcode);
6363
}
6464
assertEquals(1, hashcodeSet.size());

0 commit comments

Comments
 (0)