From 60d0dd904d9f77afa097b84e7b65f43c361167bc Mon Sep 17 00:00:00 2001 From: "Mark J. Hoy" Date: Thu, 14 Nov 2024 16:50:25 -0500 Subject: [PATCH 1/2] backport elastic#116357 to 8.15 --- docs/changelog/116357.yaml | 5 ++ .../apis/list-query-rulesets.asciidoc | 12 ++- .../org/elasticsearch/TransportVersions.java | 1 + .../EnterpriseSearchFeatureSetUsage.java | 1 + .../rest-api-spec/test/entsearch/20_usage.yml | 4 +- .../entsearch/rules/20_query_ruleset_list.yml | 88 ++++++++++++++++++- .../application/EnterpriseSearchFeatures.java | 8 ++ .../EnterpriseSearchUsageTransportAction.java | 27 +++--- .../rules/QueryRulesIndexService.java | 6 +- .../rules/QueryRulesetListItem.java | 38 ++++++-- .../rules/action/ListQueryRulesetsAction.java | 3 + ...setsActionResponseBWCSerializingTests.java | 22 ++++- 12 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 docs/changelog/116357.yaml diff --git a/docs/changelog/116357.yaml b/docs/changelog/116357.yaml new file mode 100644 index 0000000000000..a1a7831eab9ca --- /dev/null +++ b/docs/changelog/116357.yaml @@ -0,0 +1,5 @@ +pr: 116357 +summary: Add tracking for query rule types +area: Relevance +type: enhancement +issues: [] diff --git a/docs/reference/query-rules/apis/list-query-rulesets.asciidoc b/docs/reference/query-rules/apis/list-query-rulesets.asciidoc index 6832934f6985c..304b8c7745007 100644 --- a/docs/reference/query-rules/apis/list-query-rulesets.asciidoc +++ b/docs/reference/query-rules/apis/list-query-rulesets.asciidoc @@ -124,7 +124,7 @@ PUT _query_rules/ruleset-3 }, { "rule_id": "rule-3", - "type": "pinned", + "type": "exclude", "criteria": [ { "type": "fuzzy", @@ -178,6 +178,9 @@ A sample response: "rule_total_count": 1, "rule_criteria_types_counts": { "exact": 1 + }, + "rule_type_counts": { + "pinned": 1 } }, { @@ -186,6 +189,9 @@ A sample response: "rule_criteria_types_counts": { "exact": 1, "fuzzy": 1 + }, + "rule_type_counts": { + "pinned": 2 } }, { @@ -194,6 +200,10 @@ A sample response: "rule_criteria_types_counts": { "exact": 1, "fuzzy": 2 + }, + "rule_type_counts": { + "pinned": 2, + "exclude": 1 } } ] diff --git a/server/src/main/java/org/elasticsearch/TransportVersions.java b/server/src/main/java/org/elasticsearch/TransportVersions.java index 93fa590ebe97f..b450787b57535 100644 --- a/server/src/main/java/org/elasticsearch/TransportVersions.java +++ b/server/src/main/java/org/elasticsearch/TransportVersions.java @@ -224,6 +224,7 @@ static TransportVersion def(int id) { * */ public static final TransportVersion ESQL_ATTRIBUTE_CACHED_SERIALIZATION_8_15 = def(8_702_00_3); + public static final TransportVersion QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15 = def(8_702_00_4); /* * STOP! READ THIS FIRST! No, really, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/application/EnterpriseSearchFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/application/EnterpriseSearchFeatureSetUsage.java index 45b9d557b72b3..506dadcac3fa2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/application/EnterpriseSearchFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/application/EnterpriseSearchFeatureSetUsage.java @@ -34,6 +34,7 @@ public class EnterpriseSearchFeatureSetUsage extends XPackFeatureSet.Usage { public static final String MIN_RULE_COUNT = "min_rule_count"; public static final String MAX_RULE_COUNT = "max_rule_count"; public static final String RULE_CRITERIA_TOTAL_COUNTS = "rule_criteria_total_counts"; + public static final String RULE_TYPE_TOTAL_COUNTS = "rule_type_total_counts"; private final Map searchApplicationsUsage; private final Map analyticsCollectionsUsage; diff --git a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_usage.yml b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_usage.yml index 81eaa24fd6f5d..2599d70ffef30 100644 --- a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_usage.yml +++ b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_usage.yml @@ -199,7 +199,7 @@ teardown: available: true, search_applications: { count: 1 }, analytics_collections: { count: 0 }, - query_rulesets: { total_count: 2, total_rule_count: 5, min_rule_count: 2, max_rule_count: 3, rule_criteria_total_counts: { exact: 5 } } + query_rulesets: { total_count: 2, total_rule_count: 5, min_rule_count: 2, max_rule_count: 3, rule_criteria_total_counts: { exact: 5 }, rule_type_total_counts: { pinned: 5 } } } } @@ -216,7 +216,7 @@ teardown: available: true, search_applications: { count: 1 }, analytics_collections: { count: 0 }, - query_rulesets: { total_count: 1, total_rule_count: 2, min_rule_count: 2, max_rule_count: 2, rule_criteria_total_counts: { exact: 2 } } + query_rulesets: { total_count: 1, total_rule_count: 2, min_rule_count: 2, max_rule_count: 2, rule_criteria_total_counts: { exact: 2 }, rule_type_total_counts: { pinned: 2 } } } } diff --git a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/rules/20_query_ruleset_list.yml b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/rules/20_query_ruleset_list.yml index f2ced956b5369..54bd093375515 100644 --- a/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/rules/20_query_ruleset_list.yml +++ b/x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/rules/20_query_ruleset_list.yml @@ -1,7 +1,4 @@ setup: - - requires: - cluster_features: [ "gte_v8.10.0" ] - reason: Introduced in 8.10.0 - do: query_rules.put_ruleset: ruleset_id: test-query-ruleset-3 @@ -297,3 +294,88 @@ teardown: - match: { error.type: 'security_exception' } +--- +'List query rulesets - include rule types': + - requires: + cluster_features: [ "query_rule_list_types" ] + reason: 'List responses updated in 8.15.5 and 8.16.1' + + - do: + query_rules.put_ruleset: + ruleset_id: a-test-query-ruleset-with-lots-of-criteria + body: + rules: + - rule_id: query-rule-id1 + type: pinned + criteria: + - type: exact + metadata: query_string + values: [ puggles ] + - type: gt + metadata: year + values: [ 2023 ] + actions: + ids: + - 'id1' + - 'id2' + - rule_id: query-rule-id2 + type: pinned + criteria: + - type: exact + metadata: query_string + values: [ pug ] + actions: + ids: + - 'id3' + - 'id4' + - rule_id: query-rule-id3 + type: pinned + criteria: + - type: fuzzy + metadata: query_string + values: [ puggles ] + actions: + ids: + - 'id5' + - 'id6' + - rule_id: query-rule-id4 + type: pinned + criteria: + - type: always + actions: + ids: + - 'id7' + - 'id8' + - rule_id: query-rule-id5 + type: pinned + criteria: + - type: prefix + metadata: query_string + values: [ pug ] + - type: suffix + metadata: query_string + values: [ gle ] + actions: + ids: + - 'id9' + - 'id10' + + - do: + query_rules.list_rulesets: + from: 0 + size: 1 + + - match: { count: 4 } + + # Alphabetical order by ruleset_id for results + - match: { results.0.ruleset_id: "a-test-query-ruleset-with-lots-of-criteria" } + - match: { results.0.rule_total_count: 5 } + - match: + results.0.rule_criteria_types_counts: + exact: 2 + gt: 1 + fuzzy: 1 + prefix: 1 + suffix: 1 + always: 1 + - match: { results.0.rule_type_counts: { pinned: 5 } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchFeatures.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchFeatures.java index 81e072479d402..6a5eade45af61 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchFeatures.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchFeatures.java @@ -12,10 +12,18 @@ import org.elasticsearch.features.NodeFeature; import org.elasticsearch.xpack.application.analytics.AnalyticsTemplateRegistry; import org.elasticsearch.xpack.application.connector.ConnectorTemplateRegistry; +import org.elasticsearch.xpack.application.rules.action.ListQueryRulesetsAction; import java.util.Map; +import java.util.Set; public class EnterpriseSearchFeatures implements FeatureSpecification { + + @Override + public Set getFeatures() { + return Set.of(ListQueryRulesetsAction.QUERY_RULE_LIST_TYPES); + } + @Override public Map getHistoricalFeatures() { return Map.of( diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java index 4a6a2a3590b3d..c79f6a0d03260 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java @@ -27,7 +27,6 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.application.analytics.action.GetAnalyticsCollectionAction; -import org.elasticsearch.xpack.application.rules.QueryRuleCriteriaType; import org.elasticsearch.xpack.application.rules.QueryRulesIndexService; import org.elasticsearch.xpack.application.rules.QueryRulesetListItem; import org.elasticsearch.xpack.application.rules.action.ListQueryRulesetsAction; @@ -41,7 +40,6 @@ import org.elasticsearch.xpack.core.application.EnterpriseSearchFeatureSetUsage; import java.util.Collections; -import java.util.EnumMap; import java.util.HashMap; import java.util.IntSummaryStatistics; import java.util.List; @@ -226,20 +224,29 @@ private void addQueryRulesetUsage(ListQueryRulesetsAction.Response response, Map List results = response.queryPage().results(); IntSummaryStatistics ruleStats = results.stream().mapToInt(QueryRulesetListItem::ruleTotalCount).summaryStatistics(); - Map criteriaTypeCountMap = new EnumMap<>(QueryRuleCriteriaType.class); - results.stream() - .flatMap(result -> result.criteriaTypeToCountMap().entrySet().stream()) - .forEach(entry -> criteriaTypeCountMap.merge(entry.getKey(), entry.getValue(), Integer::sum)); + Map ruleCriteriaTypeCountMap = new HashMap<>(); + Map ruleTypeCountMap = new HashMap<>(); - Map rulesTypeCountMap = new HashMap<>(); - criteriaTypeCountMap.forEach((criteriaType, count) -> rulesTypeCountMap.put(criteriaType.name().toLowerCase(Locale.ROOT), count)); + results.forEach(result -> { + populateCounts(ruleCriteriaTypeCountMap, result.criteriaTypeToCountMap()); + populateCounts(ruleTypeCountMap, result.ruleTypeToCountMap()); + }); queryRulesUsage.put(TOTAL_COUNT, response.queryPage().count()); queryRulesUsage.put(TOTAL_RULE_COUNT, ruleStats.getSum()); queryRulesUsage.put(MIN_RULE_COUNT, results.isEmpty() ? 0 : ruleStats.getMin()); queryRulesUsage.put(MAX_RULE_COUNT, results.isEmpty() ? 0 : ruleStats.getMax()); - if (rulesTypeCountMap.isEmpty() == false) { - queryRulesUsage.put(RULE_CRITERIA_TOTAL_COUNTS, rulesTypeCountMap); + if (ruleCriteriaTypeCountMap.isEmpty() == false) { + queryRulesUsage.put(RULE_CRITERIA_TOTAL_COUNTS, ruleCriteriaTypeCountMap); + } + if (ruleTypeCountMap.isEmpty() == false) { + queryRulesUsage.put(EnterpriseSearchFeatureSetUsage.RULE_TYPE_TOTAL_COUNTS, ruleTypeCountMap); } } + + private void populateCounts(Map targetMap, Map, Integer> sourceMap) { + sourceMap.forEach( + (key, value) -> targetMap.merge(key.name().toLowerCase(Locale.ROOT), value, (v1, v2) -> (Integer) v1 + (Integer) v2) + ); + } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesIndexService.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesIndexService.java index 2eec155ae8ea2..43f76dcb9cf61 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesIndexService.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesIndexService.java @@ -446,6 +446,7 @@ private static QueryRulesetListItem hitToQueryRulesetListItem(SearchHit searchHi final List> rules = ((List>) sourceMap.get(QueryRuleset.RULES_FIELD.getPreferredName())); final int numRules = rules.size(); final Map queryRuleCriteriaTypeToCountMap = new EnumMap<>(QueryRuleCriteriaType.class); + final Map ruleTypeToCountMap = new EnumMap<>(QueryRule.QueryRuleType.class); for (LinkedHashMap rule : rules) { @SuppressWarnings("unchecked") List> criteriaList = ((List>) rule.get(QueryRule.CRITERIA_FIELD.getPreferredName())); @@ -454,9 +455,12 @@ private static QueryRulesetListItem hitToQueryRulesetListItem(SearchHit searchHi final QueryRuleCriteriaType queryRuleCriteriaType = QueryRuleCriteriaType.type(criteriaType); queryRuleCriteriaTypeToCountMap.compute(queryRuleCriteriaType, (k, v) -> v == null ? 1 : v + 1); } + final String ruleType = ((String) rule.get(QueryRule.TYPE_FIELD.getPreferredName())); + final QueryRule.QueryRuleType queryRuleType = QueryRule.QueryRuleType.queryRuleType(ruleType); + ruleTypeToCountMap.compute(queryRuleType, (k, v) -> v == null ? 1 : v + 1); } - return new QueryRulesetListItem(rulesetId, numRules, queryRuleCriteriaTypeToCountMap); + return new QueryRulesetListItem(rulesetId, numRules, queryRuleCriteriaTypeToCountMap, ruleTypeToCountMap); } public record QueryRulesetResult(List rulesets, long totalResults) {} diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesetListItem.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesetListItem.java index f3bc07387512f..f40a69f6fa7cd 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesetListItem.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesetListItem.java @@ -32,10 +32,12 @@ public class QueryRulesetListItem implements Writeable, ToXContentObject { public static final ParseField RULESET_ID_FIELD = new ParseField("ruleset_id"); public static final ParseField RULE_TOTAL_COUNT_FIELD = new ParseField("rule_total_count"); public static final ParseField RULE_CRITERIA_TYPE_COUNTS_FIELD = new ParseField("rule_criteria_types_counts"); + public static final ParseField RULE_TYPE_COUNTS_FIELD = new ParseField("rule_type_counts"); private final String rulesetId; private final int ruleTotalCount; private final Map criteriaTypeToCountMap; + private final Map ruleTypeToCountMap; /** * Constructs a QueryRulesetListItem. @@ -44,21 +46,33 @@ public class QueryRulesetListItem implements Writeable, ToXContentObject { * @param ruleTotalCount The number of rules contained within the ruleset. * @param criteriaTypeToCountMap A map of criteria type to the number of rules of that type. */ - public QueryRulesetListItem(String rulesetId, int ruleTotalCount, Map criteriaTypeToCountMap) { + public QueryRulesetListItem( + String rulesetId, + int ruleTotalCount, + Map criteriaTypeToCountMap, + Map ruleTypeToCountMap + ) { Objects.requireNonNull(rulesetId, "rulesetId cannot be null on a QueryRuleListItem"); this.rulesetId = rulesetId; this.ruleTotalCount = ruleTotalCount; this.criteriaTypeToCountMap = criteriaTypeToCountMap; + this.ruleTypeToCountMap = ruleTypeToCountMap; } public QueryRulesetListItem(StreamInput in) throws IOException { this.rulesetId = in.readString(); this.ruleTotalCount = in.readInt(); - if (in.getTransportVersion().onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { + TransportVersion streamTransportVersion = in.getTransportVersion(); + if (streamTransportVersion.onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { this.criteriaTypeToCountMap = in.readMap(m -> in.readEnum(QueryRuleCriteriaType.class), StreamInput::readInt); } else { this.criteriaTypeToCountMap = Map.of(); } + if (streamTransportVersion.onOrAfter(TransportVersions.QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15)) { + this.ruleTypeToCountMap = in.readMap(m -> in.readEnum(QueryRule.QueryRuleType.class), StreamInput::readInt); + } else { + this.ruleTypeToCountMap = Map.of(); + } } @Override @@ -71,6 +85,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(criteriaType.name().toLowerCase(Locale.ROOT), criteriaTypeToCountMap.get(criteriaType)); } builder.endObject(); + builder.startObject(RULE_TYPE_COUNTS_FIELD.getPreferredName()); + for (QueryRule.QueryRuleType ruleType : ruleTypeToCountMap.keySet()) { + builder.field(ruleType.name().toLowerCase(Locale.ROOT), ruleTypeToCountMap.get(ruleType)); + } + builder.endObject(); builder.endObject(); return builder; } @@ -79,9 +98,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public void writeTo(StreamOutput out) throws IOException { out.writeString(rulesetId); out.writeInt(ruleTotalCount); - if (out.getTransportVersion().onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { + TransportVersion streamTransportVersion = out.getTransportVersion(); + if (streamTransportVersion.onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { out.writeMap(criteriaTypeToCountMap, StreamOutput::writeEnum, StreamOutput::writeInt); } + if (streamTransportVersion.onOrAfter(TransportVersions.QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15)) { + out.writeMap(ruleTypeToCountMap, StreamOutput::writeEnum, StreamOutput::writeInt); + } } /** @@ -106,6 +129,10 @@ public Map criteriaTypeToCountMap() { return criteriaTypeToCountMap; } + public Map ruleTypeToCountMap() { + return ruleTypeToCountMap; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -113,11 +140,12 @@ public boolean equals(Object o) { QueryRulesetListItem that = (QueryRulesetListItem) o; return ruleTotalCount == that.ruleTotalCount && Objects.equals(rulesetId, that.rulesetId) - && Objects.equals(criteriaTypeToCountMap, that.criteriaTypeToCountMap); + && Objects.equals(criteriaTypeToCountMap, that.criteriaTypeToCountMap) + && Objects.equals(ruleTypeToCountMap, that.ruleTypeToCountMap); } @Override public int hashCode() { - return Objects.hash(rulesetId, ruleTotalCount, criteriaTypeToCountMap); + return Objects.hash(rulesetId, ruleTotalCount, criteriaTypeToCountMap, ruleTypeToCountMap); } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java index 11397583ce5b9..62f9f3fd46cc4 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java @@ -13,6 +13,7 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.features.NodeFeature; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -33,6 +34,8 @@ public class ListQueryRulesetsAction { public static final String NAME = "cluster:admin/xpack/query_rules/list"; public static final ActionType INSTANCE = new ActionType<>(NAME); + public static final NodeFeature QUERY_RULE_LIST_TYPES = new NodeFeature("query_rule_list_types"); + private ListQueryRulesetsAction() {/* no instances */} public static class Request extends ActionRequest implements ToXContentObject { diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsActionResponseBWCSerializingTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsActionResponseBWCSerializingTests.java index 5ae0f51cb6112..adf349379de20 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsActionResponseBWCSerializingTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsActionResponseBWCSerializingTests.java @@ -8,8 +8,10 @@ package org.elasticsearch.xpack.application.rules.action; import org.elasticsearch.TransportVersion; +import org.elasticsearch.TransportVersions; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xpack.application.EnterpriseSearchModuleTestUtils; +import org.elasticsearch.xpack.application.rules.QueryRule; import org.elasticsearch.xpack.application.rules.QueryRuleCriteriaType; import org.elasticsearch.xpack.application.rules.QueryRuleset; import org.elasticsearch.xpack.application.rules.QueryRulesetListItem; @@ -32,9 +34,13 @@ private static ListQueryRulesetsAction.Response randomQueryRulesetListItem() { QueryRuleset queryRuleset = EnterpriseSearchModuleTestUtils.randomQueryRuleset(); Map criteriaTypeToCountMap = Map.of( randomFrom(QueryRuleCriteriaType.values()), - randomIntBetween(0, 10) + randomIntBetween(1, 10) ); - return new QueryRulesetListItem(queryRuleset.id(), queryRuleset.rules().size(), criteriaTypeToCountMap); + Map ruleTypeToCountMap = Map.of( + randomFrom(QueryRule.QueryRuleType.values()), + randomIntBetween(1, 10) + ); + return new QueryRulesetListItem(queryRuleset.id(), queryRuleset.rules().size(), criteriaTypeToCountMap, ruleTypeToCountMap); }), randomLongBetween(0, 1000)); } @@ -53,12 +59,20 @@ protected ListQueryRulesetsAction.Response mutateInstanceForVersion( ListQueryRulesetsAction.Response instance, TransportVersion version ) { - if (version.onOrAfter(QueryRulesetListItem.EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { + if (version.onOrAfter(TransportVersions.QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15)) { return instance; + } else if (version.onOrAfter(QueryRulesetListItem.EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) { + List updatedResults = new ArrayList<>(); + for (QueryRulesetListItem listItem : instance.queryPage.results()) { + updatedResults.add( + new QueryRulesetListItem(listItem.rulesetId(), listItem.ruleTotalCount(), listItem.criteriaTypeToCountMap(), Map.of()) + ); + } + return new ListQueryRulesetsAction.Response(updatedResults, instance.queryPage.count()); } else { List updatedResults = new ArrayList<>(); for (QueryRulesetListItem listItem : instance.queryPage.results()) { - updatedResults.add(new QueryRulesetListItem(listItem.rulesetId(), listItem.ruleTotalCount(), Map.of())); + updatedResults.add(new QueryRulesetListItem(listItem.rulesetId(), listItem.ruleTotalCount(), Map.of(), Map.of())); } return new ListQueryRulesetsAction.Response(updatedResults, instance.queryPage.count()); } From 705a7074d06b93f6ddaf12fa7d78e5467bdeab1b Mon Sep 17 00:00:00 2001 From: "Mark J. Hoy" Date: Thu, 14 Nov 2024 17:00:10 -0500 Subject: [PATCH 2/2] remove exclude clause from docs --- docs/reference/query-rules/apis/list-query-rulesets.asciidoc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/reference/query-rules/apis/list-query-rulesets.asciidoc b/docs/reference/query-rules/apis/list-query-rulesets.asciidoc index 304b8c7745007..c044fa7b216ff 100644 --- a/docs/reference/query-rules/apis/list-query-rulesets.asciidoc +++ b/docs/reference/query-rules/apis/list-query-rulesets.asciidoc @@ -124,7 +124,7 @@ PUT _query_rules/ruleset-3 }, { "rule_id": "rule-3", - "type": "exclude", + "type": "pinned", "criteria": [ { "type": "fuzzy", @@ -202,8 +202,7 @@ A sample response: "fuzzy": 2 }, "rule_type_counts": { - "pinned": 2, - "exclude": 1 + "pinned": 3 } } ]