Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure query_group_hashcode is present in top_queries response in all cases #187

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
// Add hashcode attribute when grouping is enabled
if (queryInsightsService.isGroupingEnabled()) {
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(queryShape);
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
attributes.put(Attribute.ID, hashcode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ int numberOfTopGroups() {
private String getGroupingId(final SearchQueryRecord searchQueryRecord) {
switch (groupingType) {
case SIMILARITY:
return searchQueryRecord.getAttributes().get(Attribute.QUERY_HASHCODE).toString();
return searchQueryRecord.getAttributes().get(Attribute.ID).toString();
case NONE:
throw new IllegalArgumentException("Should not try to group queries if grouping type is NONE");
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public enum Attribute {
*/
LABELS,
/**
* Unique hashcode used to group similar queries
* Query Group hashcode or query hashcode representing a unique identifier for the query/group
*/
QUERY_HASHCODE,
ID,
/**
* Grouping type of the query record (none, similarity)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

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

import static org.opensearch.plugin.insights.rules.model.Attribute.GROUP_BY;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -96,6 +94,11 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
*/
public static final String GROUP_BY = "group_by";

/**
* Query Group hashcode or query hashcode representing a unique identifier for the query/group
*/
public static final String ID = "id";

public static final String MEASUREMENTS = "measurements";
private String groupingId;

Expand Down Expand Up @@ -176,6 +179,9 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
case GROUP_BY:
attributes.put(Attribute.GROUP_BY, parser.text());
break;
case ID:
attributes.put(Attribute.ID, parser.text());
break;
case SOURCE:
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
attributes.put(Attribute.SOURCE, SearchSourceBuilder.fromXContent(parser, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static List<SearchQueryRecord> generateQueryInsightRecords(
attributes.put(Attribute.TOTAL_SHARDS, randomIntBetween(1, 100));
attributes.put(Attribute.INDICES, randomArray(1, 3, Object[]::new, () -> randomAlphaOfLengthBetween(5, 10)));
attributes.put(Attribute.PHASE_LATENCY_MAP, phaseLatencyMap);
attributes.put(Attribute.QUERY_HASHCODE, Objects.hashCode(i));
attributes.put(Attribute.ID, Objects.hashCode(i));
attributes.put(Attribute.GROUP_BY, GroupingType.NONE);
attributes.put(
Attribute.TASK_RESOURCE_USAGES,
Expand Down Expand Up @@ -200,13 +200,13 @@ public static List<List<SearchQueryRecord>> generateMultipleQueryInsightsRecords

public static void populateSameQueryHashcodes(List<SearchQueryRecord> searchQueryRecords) {
for (SearchQueryRecord record : searchQueryRecords) {
record.getAttributes().put(Attribute.QUERY_HASHCODE, 1);
record.getAttributes().put(Attribute.ID, 1);
}
}

public static void populateHashcode(List<SearchQueryRecord> searchQueryRecords, int hash) {
for (SearchQueryRecord record : searchQueryRecords) {
record.getAttributes().put(Attribute.QUERY_HASHCODE, hash);
record.getAttributes().put(Attribute.ID, hash);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testWithAllDifferentHashcodes() {
Set<Integer> hashcodeSet = new HashSet<>();
for (SearchQueryRecord record : records) {
groupedRecord = minMaxHeapQueryGrouper.add(record);
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.QUERY_HASHCODE);
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.ID);
hashcodeSet.add(hashcode);
}
assertEquals(numOfRecords, hashcodeSet.size());
Expand All @@ -58,7 +58,7 @@ public void testWithAllSameHashcodes() {
Set<Integer> hashcodeSet = new HashSet<>();
for (SearchQueryRecord record : records) {
groupedRecord = minMaxHeapQueryGrouper.add(record);
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.QUERY_HASHCODE);
int hashcode = (int) groupedRecord.getAttributes().get(Attribute.ID);
hashcodeSet.add(hashcode);
}
assertEquals(1, hashcodeSet.size());
Expand Down
Loading