Skip to content

Commit 2084a48

Browse files
nik9000mark-vieira
authored andcommitted
Add integration tests for filters (#69439)
Revamps the integration tests for the `filter` agg to be more clear and builds integration tests for the `fitlers` agg. Both of these integration tests are fairly basic but they do assert that the aggs work.
1 parent 280325f commit 2084a48

File tree

8 files changed

+285
-36
lines changed

8 files changed

+285
-36
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/README.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ match all of those selectors. The following selectors are supported:
395395
396396
- `version`: Only nodes who's version is within the range will receive the
397397
request. The syntax for the pattern is the same as when `version` is within
398-
`skip`.
398+
`skip` but also supports `current` which selects nodes of the current version.
399+
`current` is useful when running mixed version tests if the results vary based
400+
on the version of the node that received the request.
399401
- `attribute`: Only nodes that have an attribute matching the name and value
400402
of the provided attribute match.
401403
Looks like:

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/220_filters_bucket.yml

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,26 @@ setup:
275275

276276

277277
---
278-
"cache":
278+
"cache busting":
279279
- skip:
280280
version: " - 7.10.99"
281281
reason: cache fixed in 7.11.0
282+
features: node_selector
282283

283284
- do:
284-
bulk:
285-
refresh: true
286-
body:
287-
- index:
288-
_index: test_1
289-
_id: 100
290-
- int_field: 1
291-
double_field: 1.0
292-
string_field: foo bar
285+
bulk:
286+
refresh: true
287+
body:
288+
- index:
289+
_index: test_1
290+
_id: 100
291+
- int_field: 1
292+
double_field: 1.0
293+
string_field: foo bar
293294

294295
- do:
296+
node_selector:
297+
version: current # the version of the node that parsed the request is part of the cache key.
295298
search:
296299
index: test_1
297300
body:
@@ -323,6 +326,8 @@ setup:
323326

324327
# This should be entirely fresh because updating the mapping busted the cache.
325328
- do:
329+
node_selector:
330+
version: current # the version of the node that parsed the request is part of the cache key.
326331
search:
327332
index: test_1
328333
body:
@@ -392,3 +397,110 @@ nested:
392397
- match: { hits.total.value: 1 }
393398
- length: { aggregations.f.buckets: 1 }
394399
- match: { aggregations.f.buckets.foo.doc_count: 1 }
400+
401+
---
402+
"cache hits":
403+
- skip:
404+
features: node_selector
405+
406+
- do:
407+
indices.create:
408+
index: test
409+
body:
410+
settings:
411+
number_of_shards: 1
412+
number_of_replicas: 0
413+
mappings:
414+
properties:
415+
mentions:
416+
type: keyword
417+
notifications:
418+
type: keyword
419+
420+
- do:
421+
bulk:
422+
refresh: true
423+
index: test
424+
body: |
425+
{"index":{"_id": "foo|bar|baz0"}}
426+
{"notifications" : ["abc"]}
427+
{"index":{"_id": "foo|bar|baz1"}}
428+
{"mentions" : ["abc"]}
429+
430+
- do:
431+
node_selector:
432+
version: current # the version of the node that parsed the request is part of the cache key.
433+
search:
434+
rest_total_hits_as_int: true
435+
size: 0
436+
request_cache: true
437+
index: test
438+
body:
439+
aggs:
440+
itemsNotify:
441+
filters:
442+
filters:
443+
lookup:
444+
terms:
445+
mentions:
446+
index: test
447+
id: foo|bar|baz0
448+
path: notifications
449+
std:
450+
terms:
451+
mentions: ["abc"]
452+
aggs:
453+
mentions:
454+
terms:
455+
field: mentions
456+
457+
# validate result
458+
- match: { hits.total: 2 }
459+
- match: { aggregations.itemsNotify.buckets.lookup.doc_count: 1 }
460+
- match: { aggregations.itemsNotify.buckets.std.doc_count: 1 }
461+
462+
# The first request will miss the cache
463+
- do:
464+
indices.stats: { index: test, metric: request_cache}
465+
- match: { _shards.total: 1 }
466+
- match: { indices.test.total.request_cache.hit_count: 0 }
467+
- match: { indices.test.total.request_cache.miss_count: 1 }
468+
469+
- do:
470+
node_selector:
471+
version: current # the version of the node that parsed the request is part of the cache key.
472+
search:
473+
rest_total_hits_as_int: true
474+
size: 0
475+
request_cache: true
476+
index: test
477+
body:
478+
aggs:
479+
itemsNotify:
480+
filters:
481+
filters:
482+
lookup:
483+
terms:
484+
mentions:
485+
index: test
486+
id: foo|bar|baz0
487+
path: notifications
488+
std:
489+
terms:
490+
mentions: ["abc"]
491+
aggs:
492+
mentions:
493+
terms:
494+
field: mentions
495+
496+
# validate result
497+
- match: { hits.total: 2 }
498+
- match: { aggregations.itemsNotify.buckets.lookup.doc_count: 1 }
499+
- match: { aggregations.itemsNotify.buckets.std.doc_count: 1 }
500+
501+
# The second result with hit the cache
502+
- do:
503+
indices.stats: { index: test, metric: request_cache}
504+
- match: { _shards.total: 1 }
505+
- match: { indices.test.total.request_cache.hit_count: 1 }
506+
- match: { indices.test.total.request_cache.miss_count: 1 }

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/50_filter.yml

Lines changed: 113 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,157 @@ setup:
1414
type: keyword
1515

1616
- do:
17-
index:
17+
bulk:
18+
refresh: true
1819
index: test
19-
id: foo|bar|baz0
20-
body: { "notifications" : ["abc"] }
20+
body: |
21+
{"index":{"_id": "foo|bar|baz0"}}
22+
{"notifications" : ["abc"]}
23+
{"index":{"_id": "foo|bar|baz1"}}
24+
{"mentions" : ["abc"]}
2125
22-
- do:
23-
index:
24-
index: test
25-
id: foo|bar|baz1
26-
body: { "mentions" : ["abc"] }
26+
---
27+
"Terms lookup gets cached":
28+
- skip:
29+
features: node_selector
2730

2831
- do:
29-
indices.refresh: {}
32+
node_selector:
33+
version: current # the version of the node that parsed the request is part of the cache key.
34+
search:
35+
rest_total_hits_as_int: true
36+
size: 0
37+
request_cache: true
38+
body:
39+
aggs:
40+
itemsNotify:
41+
filter:
42+
terms:
43+
mentions:
44+
index: test
45+
id: foo|bar|baz0
46+
path: notifications
47+
aggs:
48+
mentions:
49+
terms:
50+
field: mentions
3051

31-
---
32-
"Filter aggs with terms lookup and ensure it's cached":
33-
# Because the filter agg rewrites the terms lookup in the rewrite phase the request can be cached
52+
# validate result
53+
- match: { hits.total: 2 }
54+
- match: { aggregations.itemsNotify.doc_count: 1 }
55+
- length: { aggregations.itemsNotify.mentions.buckets: 1 }
56+
- match: { aggregations.itemsNotify.mentions.buckets.0.key: "abc" }
3457

58+
# The first request will miss the cache
59+
- do:
60+
indices.stats: { index: test, metric: request_cache}
61+
- match: { _shards.total: 1 }
62+
- match: { indices.test.total.request_cache.hit_count: 0 }
63+
- match: { indices.test.total.request_cache.miss_count: 1 }
3564

3665
- do:
66+
node_selector:
67+
version: current # the version of the node that parsed the request is part of the cache key.
3768
search:
3869
rest_total_hits_as_int: true
3970
size: 0
4071
request_cache: true
41-
body: {"aggs": { "itemsNotify": { "filter": { "terms": { "mentions": { "index": "test", "id": "foo|bar|baz0", "path": "notifications"}}}, "aggs": { "mentions" : {"terms" : { "field" : "mentions" }}}}}}
72+
body:
73+
aggs:
74+
itemsNotify:
75+
filter:
76+
terms:
77+
mentions:
78+
index: test
79+
id: foo|bar|baz0
80+
path: notifications
81+
aggs:
82+
mentions:
83+
terms:
84+
field: mentions
4285

4386
# validate result
4487
- match: { hits.total: 2 }
4588
- match: { aggregations.itemsNotify.doc_count: 1 }
4689
- length: { aggregations.itemsNotify.mentions.buckets: 1 }
4790
- match: { aggregations.itemsNotify.mentions.buckets.0.key: "abc" }
48-
# we are using a lookup - this should not cache
91+
92+
# The second result with hit the cache
4993
- do:
5094
indices.stats: { index: test, metric: request_cache}
5195
- match: { _shards.total: 1 }
52-
- match: { _all.total.request_cache.hit_count: 0 }
53-
- match: { _all.total.request_cache.miss_count: 1 }
54-
- is_true: indices.test
96+
- match: { indices.test.total.request_cache.hit_count: 1 }
97+
- match: { indices.test.total.request_cache.miss_count: 1 }
5598

5699
---
57-
"Filter aggs no lookup and ensure it's cached":
58-
# now run without lookup and ensure we get cached or at least do the lookup
100+
"Standard queries get cached":
101+
- skip:
102+
features: node_selector
103+
59104
- do:
105+
node_selector:
106+
version: current # the version of the node that parsed the request is part of the cache key.
60107
search:
61108
rest_total_hits_as_int: true
62109
size: 0
63110
request_cache: true
64-
body: {"aggs": { "itemsNotify": { "filter": { "terms": { "mentions": ["abc"]}}, "aggs": { "mentions" : {"terms" : { "field" : "mentions" }}}}}}
111+
body:
112+
aggs:
113+
itemsNotify:
114+
filter:
115+
terms:
116+
mentions: ["abc"]
117+
aggs:
118+
mentions:
119+
terms:
120+
field: mentions
65121

122+
# Validate result
66123
- match: { hits.total: 2 }
67124
- match: { aggregations.itemsNotify.doc_count: 1 }
68125
- length: { aggregations.itemsNotify.mentions.buckets: 1 }
69126
- match: { aggregations.itemsNotify.mentions.buckets.0.key: "abc" }
127+
128+
# The first request will miss the cache
70129
- do:
71130
indices.stats: { index: test, metric: request_cache}
72131
- match: { _shards.total: 1 }
73-
- match: { _all.total.request_cache.hit_count: 0 }
74-
- match: { _all.total.request_cache.miss_count: 1 }
132+
- match: { indices.test.total.request_cache.hit_count: 0 }
133+
- match: { indices.test.total.request_cache.miss_count: 1 }
75134
- is_true: indices.test
76135

136+
# Try again - it'll cache
137+
- do:
138+
node_selector:
139+
version: current # the version of the node that parsed the request is part of the cache key.
140+
search:
141+
rest_total_hits_as_int: true
142+
size: 0
143+
request_cache: true
144+
body:
145+
aggs:
146+
itemsNotify:
147+
filter:
148+
terms:
149+
mentions: ["abc"]
150+
aggs:
151+
mentions:
152+
terms:
153+
field: mentions
154+
155+
# Validate result
156+
- match: { hits.total: 2 }
157+
- match: { aggregations.itemsNotify.doc_count: 1 }
158+
- length: { aggregations.itemsNotify.mentions.buckets: 1 }
159+
- match: { aggregations.itemsNotify.mentions.buckets.0.key: "abc" }
160+
161+
# The first request will miss the cache
162+
- do:
163+
indices.stats: { index: test, metric: request_cache}
164+
- match: { _shards.total: 1 }
165+
- match: { indices.test.total.request_cache.hit_count: 1 }
166+
- match: { indices.test.total.request_cache.miss_count: 1 }
167+
77168
---
78169
"As a child of terms":
79170
- do:

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/70_adjacency_matrix.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ setup:
6767
- skip:
6868
version: " - 7.8.99"
6969
reason: fixed in 7.9.0
70+
features: node_selector
71+
7072
- do:
7173
indices.create:
7274
index: lookup
@@ -91,6 +93,8 @@ setup:
9193
{ "num": [4] }
9294
9395
- do:
96+
node_selector:
97+
version: current # the version of the node that parsed the request is part of the cache key.
9498
search:
9599
index: test
96100
body:
@@ -142,6 +146,8 @@ setup:
142146

143147
# The second request should hit the cache
144148
- do:
149+
node_selector:
150+
version: current # the version of the node that parsed the request is part of the cache key.
145151
search:
146152
index: test
147153
body:

server/src/main/java/org/elasticsearch/indices/IndicesRequestCache.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ public int hashCode() {
252252
result = 31 * result + value.hashCode();
253253
return result;
254254
}
255+
256+
@Override
257+
public String toString() {
258+
return "Key(mappingKey=["
259+
+ mappingCacheKey
260+
+ "],readerKey=["
261+
+ readerCacheKey
262+
+ "],entityKey=["
263+
+ entity.getCacheIdentity()
264+
+ ",value=" // BytesRef's toString already has [] so we don't add it here
265+
+ value.toBytesRef() // BytesRef has a readable toString
266+
+ ")";
267+
}
255268
}
256269

257270
private class CleanupKey implements IndexReader.ClosedListener {

0 commit comments

Comments
 (0)