Skip to content

Commit 7e937ac

Browse files
authored
Fix _terms_enum display values (#94080)
A recent change (#93839) introduced `_terms_enum` support for fields that need to convert their internal bytes representation to thr proper string representation for display purposes. The `constant_keyword` and `flattened` field types didn't implement a 'valueForDisplay' method yet, so the underlying `BytesRef` was printed directly in the response. This change fixes that and adds tests to ensure human readable response values for those field types. Closes #94041
1 parent fbb7913 commit 7e937ac

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

docs/changelog/94080.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 94080
2+
summary: Fix `_terms_enum` display values
3+
area: Search
4+
type: bug
5+
issues:
6+
- 94041

server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
365365
}
366366
return SourceValueFetcher.identity(rootName + "." + key, context, null);
367367
}
368+
369+
@Override
370+
public Object valueForDisplay(Object value) {
371+
if (value == null) {
372+
return null;
373+
}
374+
BytesRef binaryValue = (BytesRef) value;
375+
return binaryValue.utf8ToString();
376+
}
368377
}
369378

370379
// Wraps a raw Lucene TermsEnum to strip values of fieldnames

x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
151151
return value == null ? (lookup, doc, ignoredValues) -> List.of() : (lookup, doc, ignoredValues) -> List.of(value);
152152
}
153153

154+
@Override
155+
public Object valueForDisplay(Object value) {
156+
if (value == null) {
157+
return null;
158+
}
159+
BytesRef binaryValue = (BytesRef) value;
160+
return binaryValue.utf8ToString();
161+
}
162+
154163
@Override
155164
public TermsEnum getTerms(boolean caseInsensitive, String string, SearchExecutionContext queryShardContext, String searchAfter) {
156165
if (value == null) {
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
setup:
3+
- skip:
4+
version: " - 7.15.0"
5+
reason: original indices are propagated correctly in 7.15.1
6+
features: headers
7+
- do:
8+
cluster.health:
9+
wait_for_status: yellow
10+
11+
- do:
12+
security.put_role:
13+
name: "test_admin_role"
14+
body: >
15+
{
16+
"indices": [
17+
{ "names": ["*"], "privileges": ["all"] }
18+
]
19+
}
20+
21+
- do:
22+
security.put_user:
23+
username: "test_admin"
24+
body: >
25+
{
26+
"password" : "x-pack-test-password",
27+
"roles" : [ "test_admin_role" ],
28+
"full_name" : "user with full privileges to multiple indices"
29+
}
30+
31+
- do:
32+
indices.create:
33+
index: test
34+
body:
35+
settings:
36+
index:
37+
number_of_shards: 1
38+
number_of_replicas: 0
39+
mappings:
40+
properties:
41+
foo_kw:
42+
type : keyword
43+
foo_ck:
44+
type: constant_keyword
45+
foo_version:
46+
type: version
47+
foo_flattened:
48+
type : flattened
49+
50+
- do:
51+
index:
52+
index: test
53+
id: "1"
54+
body: { foo_kw: "foo_kw", foo_ck: "foo_ck", foo_version: "foo_version", foo_flattened.f1: "foo_flattened.f1" }
55+
56+
- do:
57+
index:
58+
index: test
59+
id: "2"
60+
body: { foo_flattened.f2: "1234" }
61+
62+
- do: #superuser
63+
headers: { Authorization: "Basic dGVzdF9hZG1pbjp4LXBhY2stdGVzdC1wYXNzd29yZA==" } # admin
64+
indices.refresh: {}
65+
66+
- do: #superuser
67+
cluster.health:
68+
index: test
69+
wait_for_status: green
70+
71+
---
72+
"Test terms enumeration keyword field":
73+
74+
- do:
75+
terms_enum:
76+
index: test
77+
body: {"field": "foo_kw", "string":"fo"}
78+
- length: {terms: 1}
79+
- match: { terms: [ "foo_kw"] }
80+
81+
---
82+
"Test terms enumeration constant keyword field":
83+
84+
- do:
85+
terms_enum:
86+
index: test
87+
body: {"field": "foo_ck", "string":"fo"}
88+
- length: {terms: 1}
89+
- match: { terms: [ "foo_ck"] }
90+
91+
---
92+
"Test terms enumeration version field":
93+
94+
- do:
95+
terms_enum:
96+
index: test
97+
body: {"field": "foo_version", "string":"fo"}
98+
- length: {terms: 1}
99+
- match: { terms: [ "foo_version"] }
100+
101+
---
102+
"Test terms enumeration flattened field":
103+
104+
- do:
105+
terms_enum:
106+
index: test
107+
body: { "field": "foo_flattened.f1", "string": "fo" }
108+
- length: { terms: 1 }
109+
- match: { terms: [ "foo_flattened.f1" ] }
110+
111+
- do:
112+
terms_enum:
113+
index: test
114+
body: { "field": "foo_flattened.f2", "string": "1" }
115+
- length: { terms: 1 }
116+
- match: { terms: [ "1234" ] }

0 commit comments

Comments
 (0)