Skip to content

Commit 60d0dd9

Browse files
committed
backport #116357 to 8.15
1 parent 2f86ec4 commit 60d0dd9

File tree

12 files changed

+189
-26
lines changed

12 files changed

+189
-26
lines changed

docs/changelog/116357.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 116357
2+
summary: Add tracking for query rule types
3+
area: Relevance
4+
type: enhancement
5+
issues: []

docs/reference/query-rules/apis/list-query-rulesets.asciidoc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ PUT _query_rules/ruleset-3
124124
},
125125
{
126126
"rule_id": "rule-3",
127-
"type": "pinned",
127+
"type": "exclude",
128128
"criteria": [
129129
{
130130
"type": "fuzzy",
@@ -178,6 +178,9 @@ A sample response:
178178
"rule_total_count": 1,
179179
"rule_criteria_types_counts": {
180180
"exact": 1
181+
},
182+
"rule_type_counts": {
183+
"pinned": 1
181184
}
182185
},
183186
{
@@ -186,6 +189,9 @@ A sample response:
186189
"rule_criteria_types_counts": {
187190
"exact": 1,
188191
"fuzzy": 1
192+
},
193+
"rule_type_counts": {
194+
"pinned": 2
189195
}
190196
},
191197
{
@@ -194,6 +200,10 @@ A sample response:
194200
"rule_criteria_types_counts": {
195201
"exact": 1,
196202
"fuzzy": 2
203+
},
204+
"rule_type_counts": {
205+
"pinned": 2,
206+
"exclude": 1
197207
}
198208
}
199209
]

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ static TransportVersion def(int id) {
224224
* </ul>
225225
*/
226226
public static final TransportVersion ESQL_ATTRIBUTE_CACHED_SERIALIZATION_8_15 = def(8_702_00_3);
227+
public static final TransportVersion QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15 = def(8_702_00_4);
227228

228229
/*
229230
* STOP! READ THIS FIRST! No, really,

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/application/EnterpriseSearchFeatureSetUsage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class EnterpriseSearchFeatureSetUsage extends XPackFeatureSet.Usage {
3434
public static final String MIN_RULE_COUNT = "min_rule_count";
3535
public static final String MAX_RULE_COUNT = "max_rule_count";
3636
public static final String RULE_CRITERIA_TOTAL_COUNTS = "rule_criteria_total_counts";
37+
public static final String RULE_TYPE_TOTAL_COUNTS = "rule_type_total_counts";
3738

3839
private final Map<String, Object> searchApplicationsUsage;
3940
private final Map<String, Object> analyticsCollectionsUsage;

x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_usage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ teardown:
199199
available: true,
200200
search_applications: { count: 1 },
201201
analytics_collections: { count: 0 },
202-
query_rulesets: { total_count: 2, total_rule_count: 5, min_rule_count: 2, max_rule_count: 3, rule_criteria_total_counts: { exact: 5 } }
202+
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 } }
203203
}
204204
}
205205

@@ -216,7 +216,7 @@ teardown:
216216
available: true,
217217
search_applications: { count: 1 },
218218
analytics_collections: { count: 0 },
219-
query_rulesets: { total_count: 1, total_rule_count: 2, min_rule_count: 2, max_rule_count: 2, rule_criteria_total_counts: { exact: 2 } }
219+
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 } }
220220
}
221221
}
222222

x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/rules/20_query_ruleset_list.yml

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
setup:
2-
- requires:
3-
cluster_features: [ "gte_v8.10.0" ]
4-
reason: Introduced in 8.10.0
52
- do:
63
query_rules.put_ruleset:
74
ruleset_id: test-query-ruleset-3
@@ -297,3 +294,88 @@ teardown:
297294

298295
- match: { error.type: 'security_exception' }
299296

297+
---
298+
'List query rulesets - include rule types':
299+
- requires:
300+
cluster_features: [ "query_rule_list_types" ]
301+
reason: 'List responses updated in 8.15.5 and 8.16.1'
302+
303+
- do:
304+
query_rules.put_ruleset:
305+
ruleset_id: a-test-query-ruleset-with-lots-of-criteria
306+
body:
307+
rules:
308+
- rule_id: query-rule-id1
309+
type: pinned
310+
criteria:
311+
- type: exact
312+
metadata: query_string
313+
values: [ puggles ]
314+
- type: gt
315+
metadata: year
316+
values: [ 2023 ]
317+
actions:
318+
ids:
319+
- 'id1'
320+
- 'id2'
321+
- rule_id: query-rule-id2
322+
type: pinned
323+
criteria:
324+
- type: exact
325+
metadata: query_string
326+
values: [ pug ]
327+
actions:
328+
ids:
329+
- 'id3'
330+
- 'id4'
331+
- rule_id: query-rule-id3
332+
type: pinned
333+
criteria:
334+
- type: fuzzy
335+
metadata: query_string
336+
values: [ puggles ]
337+
actions:
338+
ids:
339+
- 'id5'
340+
- 'id6'
341+
- rule_id: query-rule-id4
342+
type: pinned
343+
criteria:
344+
- type: always
345+
actions:
346+
ids:
347+
- 'id7'
348+
- 'id8'
349+
- rule_id: query-rule-id5
350+
type: pinned
351+
criteria:
352+
- type: prefix
353+
metadata: query_string
354+
values: [ pug ]
355+
- type: suffix
356+
metadata: query_string
357+
values: [ gle ]
358+
actions:
359+
ids:
360+
- 'id9'
361+
- 'id10'
362+
363+
- do:
364+
query_rules.list_rulesets:
365+
from: 0
366+
size: 1
367+
368+
- match: { count: 4 }
369+
370+
# Alphabetical order by ruleset_id for results
371+
- match: { results.0.ruleset_id: "a-test-query-ruleset-with-lots-of-criteria" }
372+
- match: { results.0.rule_total_count: 5 }
373+
- match:
374+
results.0.rule_criteria_types_counts:
375+
exact: 2
376+
gt: 1
377+
fuzzy: 1
378+
prefix: 1
379+
suffix: 1
380+
always: 1
381+
- match: { results.0.rule_type_counts: { pinned: 5 } }

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchFeatures.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
import org.elasticsearch.features.NodeFeature;
1313
import org.elasticsearch.xpack.application.analytics.AnalyticsTemplateRegistry;
1414
import org.elasticsearch.xpack.application.connector.ConnectorTemplateRegistry;
15+
import org.elasticsearch.xpack.application.rules.action.ListQueryRulesetsAction;
1516

1617
import java.util.Map;
18+
import java.util.Set;
1719

1820
public class EnterpriseSearchFeatures implements FeatureSpecification {
21+
22+
@Override
23+
public Set<NodeFeature> getFeatures() {
24+
return Set.of(ListQueryRulesetsAction.QUERY_RULE_LIST_TYPES);
25+
}
26+
1927
@Override
2028
public Map<NodeFeature, Version> getHistoricalFeatures() {
2129
return Map.of(

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.threadpool.ThreadPool;
2828
import org.elasticsearch.transport.TransportService;
2929
import org.elasticsearch.xpack.application.analytics.action.GetAnalyticsCollectionAction;
30-
import org.elasticsearch.xpack.application.rules.QueryRuleCriteriaType;
3130
import org.elasticsearch.xpack.application.rules.QueryRulesIndexService;
3231
import org.elasticsearch.xpack.application.rules.QueryRulesetListItem;
3332
import org.elasticsearch.xpack.application.rules.action.ListQueryRulesetsAction;
@@ -41,7 +40,6 @@
4140
import org.elasticsearch.xpack.core.application.EnterpriseSearchFeatureSetUsage;
4241

4342
import java.util.Collections;
44-
import java.util.EnumMap;
4543
import java.util.HashMap;
4644
import java.util.IntSummaryStatistics;
4745
import java.util.List;
@@ -226,20 +224,29 @@ private void addQueryRulesetUsage(ListQueryRulesetsAction.Response response, Map
226224
List<QueryRulesetListItem> results = response.queryPage().results();
227225
IntSummaryStatistics ruleStats = results.stream().mapToInt(QueryRulesetListItem::ruleTotalCount).summaryStatistics();
228226

229-
Map<QueryRuleCriteriaType, Integer> criteriaTypeCountMap = new EnumMap<>(QueryRuleCriteriaType.class);
230-
results.stream()
231-
.flatMap(result -> result.criteriaTypeToCountMap().entrySet().stream())
232-
.forEach(entry -> criteriaTypeCountMap.merge(entry.getKey(), entry.getValue(), Integer::sum));
227+
Map<String, Object> ruleCriteriaTypeCountMap = new HashMap<>();
228+
Map<String, Object> ruleTypeCountMap = new HashMap<>();
233229

234-
Map<String, Object> rulesTypeCountMap = new HashMap<>();
235-
criteriaTypeCountMap.forEach((criteriaType, count) -> rulesTypeCountMap.put(criteriaType.name().toLowerCase(Locale.ROOT), count));
230+
results.forEach(result -> {
231+
populateCounts(ruleCriteriaTypeCountMap, result.criteriaTypeToCountMap());
232+
populateCounts(ruleTypeCountMap, result.ruleTypeToCountMap());
233+
});
236234

237235
queryRulesUsage.put(TOTAL_COUNT, response.queryPage().count());
238236
queryRulesUsage.put(TOTAL_RULE_COUNT, ruleStats.getSum());
239237
queryRulesUsage.put(MIN_RULE_COUNT, results.isEmpty() ? 0 : ruleStats.getMin());
240238
queryRulesUsage.put(MAX_RULE_COUNT, results.isEmpty() ? 0 : ruleStats.getMax());
241-
if (rulesTypeCountMap.isEmpty() == false) {
242-
queryRulesUsage.put(RULE_CRITERIA_TOTAL_COUNTS, rulesTypeCountMap);
239+
if (ruleCriteriaTypeCountMap.isEmpty() == false) {
240+
queryRulesUsage.put(RULE_CRITERIA_TOTAL_COUNTS, ruleCriteriaTypeCountMap);
241+
}
242+
if (ruleTypeCountMap.isEmpty() == false) {
243+
queryRulesUsage.put(EnterpriseSearchFeatureSetUsage.RULE_TYPE_TOTAL_COUNTS, ruleTypeCountMap);
243244
}
244245
}
246+
247+
private void populateCounts(Map<String, Object> targetMap, Map<? extends Enum<?>, Integer> sourceMap) {
248+
sourceMap.forEach(
249+
(key, value) -> targetMap.merge(key.name().toLowerCase(Locale.ROOT), value, (v1, v2) -> (Integer) v1 + (Integer) v2)
250+
);
251+
}
245252
}

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesIndexService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ private static QueryRulesetListItem hitToQueryRulesetListItem(SearchHit searchHi
446446
final List<LinkedHashMap<?, ?>> rules = ((List<LinkedHashMap<?, ?>>) sourceMap.get(QueryRuleset.RULES_FIELD.getPreferredName()));
447447
final int numRules = rules.size();
448448
final Map<QueryRuleCriteriaType, Integer> queryRuleCriteriaTypeToCountMap = new EnumMap<>(QueryRuleCriteriaType.class);
449+
final Map<QueryRule.QueryRuleType, Integer> ruleTypeToCountMap = new EnumMap<>(QueryRule.QueryRuleType.class);
449450
for (LinkedHashMap<?, ?> rule : rules) {
450451
@SuppressWarnings("unchecked")
451452
List<LinkedHashMap<?, ?>> criteriaList = ((List<LinkedHashMap<?, ?>>) rule.get(QueryRule.CRITERIA_FIELD.getPreferredName()));
@@ -454,9 +455,12 @@ private static QueryRulesetListItem hitToQueryRulesetListItem(SearchHit searchHi
454455
final QueryRuleCriteriaType queryRuleCriteriaType = QueryRuleCriteriaType.type(criteriaType);
455456
queryRuleCriteriaTypeToCountMap.compute(queryRuleCriteriaType, (k, v) -> v == null ? 1 : v + 1);
456457
}
458+
final String ruleType = ((String) rule.get(QueryRule.TYPE_FIELD.getPreferredName()));
459+
final QueryRule.QueryRuleType queryRuleType = QueryRule.QueryRuleType.queryRuleType(ruleType);
460+
ruleTypeToCountMap.compute(queryRuleType, (k, v) -> v == null ? 1 : v + 1);
457461
}
458462

459-
return new QueryRulesetListItem(rulesetId, numRules, queryRuleCriteriaTypeToCountMap);
463+
return new QueryRulesetListItem(rulesetId, numRules, queryRuleCriteriaTypeToCountMap, ruleTypeToCountMap);
460464
}
461465

462466
public record QueryRulesetResult(List<QueryRulesetListItem> rulesets, long totalResults) {}

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesetListItem.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public class QueryRulesetListItem implements Writeable, ToXContentObject {
3232
public static final ParseField RULESET_ID_FIELD = new ParseField("ruleset_id");
3333
public static final ParseField RULE_TOTAL_COUNT_FIELD = new ParseField("rule_total_count");
3434
public static final ParseField RULE_CRITERIA_TYPE_COUNTS_FIELD = new ParseField("rule_criteria_types_counts");
35+
public static final ParseField RULE_TYPE_COUNTS_FIELD = new ParseField("rule_type_counts");
3536

3637
private final String rulesetId;
3738
private final int ruleTotalCount;
3839
private final Map<QueryRuleCriteriaType, Integer> criteriaTypeToCountMap;
40+
private final Map<QueryRule.QueryRuleType, Integer> ruleTypeToCountMap;
3941

4042
/**
4143
* Constructs a QueryRulesetListItem.
@@ -44,21 +46,33 @@ public class QueryRulesetListItem implements Writeable, ToXContentObject {
4446
* @param ruleTotalCount The number of rules contained within the ruleset.
4547
* @param criteriaTypeToCountMap A map of criteria type to the number of rules of that type.
4648
*/
47-
public QueryRulesetListItem(String rulesetId, int ruleTotalCount, Map<QueryRuleCriteriaType, Integer> criteriaTypeToCountMap) {
49+
public QueryRulesetListItem(
50+
String rulesetId,
51+
int ruleTotalCount,
52+
Map<QueryRuleCriteriaType, Integer> criteriaTypeToCountMap,
53+
Map<QueryRule.QueryRuleType, Integer> ruleTypeToCountMap
54+
) {
4855
Objects.requireNonNull(rulesetId, "rulesetId cannot be null on a QueryRuleListItem");
4956
this.rulesetId = rulesetId;
5057
this.ruleTotalCount = ruleTotalCount;
5158
this.criteriaTypeToCountMap = criteriaTypeToCountMap;
59+
this.ruleTypeToCountMap = ruleTypeToCountMap;
5260
}
5361

5462
public QueryRulesetListItem(StreamInput in) throws IOException {
5563
this.rulesetId = in.readString();
5664
this.ruleTotalCount = in.readInt();
57-
if (in.getTransportVersion().onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) {
65+
TransportVersion streamTransportVersion = in.getTransportVersion();
66+
if (streamTransportVersion.onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) {
5867
this.criteriaTypeToCountMap = in.readMap(m -> in.readEnum(QueryRuleCriteriaType.class), StreamInput::readInt);
5968
} else {
6069
this.criteriaTypeToCountMap = Map.of();
6170
}
71+
if (streamTransportVersion.onOrAfter(TransportVersions.QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15)) {
72+
this.ruleTypeToCountMap = in.readMap(m -> in.readEnum(QueryRule.QueryRuleType.class), StreamInput::readInt);
73+
} else {
74+
this.ruleTypeToCountMap = Map.of();
75+
}
6276
}
6377

6478
@Override
@@ -71,6 +85,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7185
builder.field(criteriaType.name().toLowerCase(Locale.ROOT), criteriaTypeToCountMap.get(criteriaType));
7286
}
7387
builder.endObject();
88+
builder.startObject(RULE_TYPE_COUNTS_FIELD.getPreferredName());
89+
for (QueryRule.QueryRuleType ruleType : ruleTypeToCountMap.keySet()) {
90+
builder.field(ruleType.name().toLowerCase(Locale.ROOT), ruleTypeToCountMap.get(ruleType));
91+
}
92+
builder.endObject();
7493
builder.endObject();
7594
return builder;
7695
}
@@ -79,9 +98,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7998
public void writeTo(StreamOutput out) throws IOException {
8099
out.writeString(rulesetId);
81100
out.writeInt(ruleTotalCount);
82-
if (out.getTransportVersion().onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) {
101+
TransportVersion streamTransportVersion = out.getTransportVersion();
102+
if (streamTransportVersion.onOrAfter(EXPANDED_RULESET_COUNT_TRANSPORT_VERSION)) {
83103
out.writeMap(criteriaTypeToCountMap, StreamOutput::writeEnum, StreamOutput::writeInt);
84104
}
105+
if (streamTransportVersion.onOrAfter(TransportVersions.QUERY_RULES_LIST_INCLUDES_TYPES_BACKPORT_8_15)) {
106+
out.writeMap(ruleTypeToCountMap, StreamOutput::writeEnum, StreamOutput::writeInt);
107+
}
85108
}
86109

87110
/**
@@ -106,18 +129,23 @@ public Map<QueryRuleCriteriaType, Integer> criteriaTypeToCountMap() {
106129
return criteriaTypeToCountMap;
107130
}
108131

132+
public Map<QueryRule.QueryRuleType, Integer> ruleTypeToCountMap() {
133+
return ruleTypeToCountMap;
134+
}
135+
109136
@Override
110137
public boolean equals(Object o) {
111138
if (this == o) return true;
112139
if (o == null || getClass() != o.getClass()) return false;
113140
QueryRulesetListItem that = (QueryRulesetListItem) o;
114141
return ruleTotalCount == that.ruleTotalCount
115142
&& Objects.equals(rulesetId, that.rulesetId)
116-
&& Objects.equals(criteriaTypeToCountMap, that.criteriaTypeToCountMap);
143+
&& Objects.equals(criteriaTypeToCountMap, that.criteriaTypeToCountMap)
144+
&& Objects.equals(ruleTypeToCountMap, that.ruleTypeToCountMap);
117145
}
118146

119147
@Override
120148
public int hashCode() {
121-
return Objects.hash(rulesetId, ruleTotalCount, criteriaTypeToCountMap);
149+
return Objects.hash(rulesetId, ruleTotalCount, criteriaTypeToCountMap, ruleTypeToCountMap);
122150
}
123151
}

0 commit comments

Comments
 (0)