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

Add type attribute to search query record #157

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -51,6 +51,7 @@
*/
public final class QueryInsightsListener extends SearchRequestOperationsListener {
private static final ToXContent.Params FORMAT_PARAMS = new ToXContent.MapParams(Collections.singletonMap("pretty", "false"));
private static final String DEFAULT_TYPE = "query";
deshsidd marked this conversation as resolved.
Show resolved Hide resolved

private static final Logger log = LogManager.getLogger(QueryInsightsListener.class);

Expand Down Expand Up @@ -271,6 +272,7 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
attributes.put(Attribute.INDICES, request.indices());
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
attributes.put(Attribute.TASK_RESOURCE_USAGES, tasksResourceUsages);
attributes.put(Attribute.TYPE, DEFAULT_TYPE);

if (queryInsightsService.isGroupingEnabled() || log.isTraceEnabled()) {
// Generate the query shape only if grouping is enabled or trace logging is enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
public class MinMaxHeapQueryGrouper implements QueryGrouper {

private static final String GROUP_TYPE = "group";

/**
* Logger
*/
Expand Down Expand Up @@ -127,6 +129,7 @@ public SearchQueryRecord add(final SearchQueryRecord searchQueryRecord) {
aggregateSearchQueryRecord = searchQueryRecord;
aggregateSearchQueryRecord.setGroupingId(groupId);
aggregateSearchQueryRecord.setMeasurementAggregation(metricType, aggregationType);
aggregateSearchQueryRecord.addAttribute(Attribute.TYPE, GROUP_TYPE);
addToMinPQ(aggregateSearchQueryRecord, groupId);
} else {
aggregateSearchQueryRecord = groupIdToAggSearchQueryRecord.get(groupId).v1();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public enum Attribute {
/**
* Unique hashcode used to group similar queries
*/
QUERY_HASHCODE;
QUERY_HASHCODE,
/**
* Type of the query record (either 'query' or 'group')
*/
TYPE;

/**
* Read an Attribute from a StreamInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public class SearchQueryRecord implements ToXContentObject, Writeable {
* Custom search request labels
*/
public static final String LABELS = "labels";
/**
* Type of the query record (either 'query' or 'group')
*/
public static final String TYPE = "type";

public static final String MEASUREMENTS = "measurements";
private String groupingId;
Expand Down Expand Up @@ -167,6 +171,8 @@ public static SearchQueryRecord fromXContent(XContentParser parser) throws IOExc
case SEARCH_TYPE:
attributes.put(Attribute.SEARCH_TYPE, parser.text());
break;
case TYPE:
attributes.put(Attribute.TYPE, parser.text());
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,6 +139,7 @@ public static List<SearchQueryRecord> generateQueryInsightRecords(
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.TYPE, "query");
attributes.put(
Attribute.TASK_RESOURCE_USAGES,
List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,18 @@ public void testGetHealthStatsWithGroups() {
private MinMaxHeapQueryGrouper getQueryGroupingService(AggregationType aggregationType, int topNSize) {
return new MinMaxHeapQueryGrouper(MetricType.LATENCY, GroupingType.SIMILARITY, aggregationType, topQueriesStore, topNSize);
}

public void testAttributeTypeSetToGroup() {
int numOfRecords = 10;
final List<SearchQueryRecord> records = QueryInsightsTestUtils.generateQueryInsightRecords(numOfRecords);

for (SearchQueryRecord record : records) {
assertEquals("query", record.getAttributes().get(Attribute.TYPE));
}
SearchQueryRecord groupedRecord;
for (SearchQueryRecord record : records) {
groupedRecord = minMaxHeapQueryGrouper.add(record);
assertEquals("group", groupedRecord.getAttributes().get(Attribute.TYPE));
}
}
}
Loading