Skip to content

Commit 4afbf45

Browse files
Fix binary docvalue_fields with padding (#70936)
Previously docvalue_fields for binary values with paddings did not output padding. We consider it to be a bug because: 1) es would not be able parse these values 2) output from source filtering and fields API is different and does output padding. This patches fixes this by outputing padding for binary docvalue_fields where it is present. Closes #70244 Backport for #70826
1 parent ea47587 commit 4afbf45

File tree

6 files changed

+60
-20
lines changed

6 files changed

+60
-20
lines changed

modules/lang-painless/src/yamlRestTest/resources/rest-api-spec/test/painless/60_script_doc_values_binary.yml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,13 @@
2727

2828
- do:
2929
search:
30-
rest_total_hits_as_int: true
3130
body:
3231
script_fields:
33-
field:
32+
field1:
3433
script:
3534
source: "doc['binary'].get(0).utf8ToString()"
36-
- match: { hits.hits.0.fields.field.0: "Some binary blob" }
37-
38-
- do:
39-
search:
40-
rest_total_hits_as_int: true
41-
body:
42-
script_fields:
43-
field:
35+
field2:
4436
script:
4537
source: "doc['binary'].value.utf8ToString()"
46-
- match: { hits.hits.0.fields.field.0: "Some binary blob" }
38+
- match: { hits.hits.0.fields.field1.0: "Some binary blob" }
39+
- match: { hits.hits.0.fields.field2.0: "Some binary blob" }
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
"binary":
3+
- skip:
4+
features: ["headers"]
5+
version: " - 7.99.99"
6+
reason: "docvalues_fields on binary field were corrected in 8.0"
7+
- do:
8+
indices.create:
9+
index: test
10+
body:
11+
mappings:
12+
properties:
13+
binary:
14+
type: binary
15+
doc_values: true
16+
17+
- do:
18+
#other formats (e.g. cbor) may not support parsing of binary
19+
headers:
20+
Content-Type: application/json
21+
index:
22+
index: test
23+
refresh: true
24+
id: 1
25+
body:
26+
binary: U29tZSBiaW5hcnkgYmxvYg==
27+
28+
- do:
29+
search:
30+
index: test
31+
body:
32+
docvalue_fields: [ "binary" ]
33+
- match: { hits.hits.0.fields.binary.0: "U29tZSBiaW5hcnkgYmxvYg==" }
34+
35+
- do:
36+
search:
37+
index: test
38+
body:
39+
fields: [ "binary" ]
40+
- match: { hits.hits.0.fields.binary.0: "U29tZSBiaW5hcnkgYmxvYg==" }
41+
42+
- do:
43+
search:
44+
index: test
45+
body:
46+
_source: ["binary"]
47+
- match: { hits.hits.0._source.binary: "U29tZSBiaW5hcnkgYmxvYg==" }

server/src/internalClusterTest/java/org/elasticsearch/search/fields/SearchFieldsIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ public void testDocValueFields() throws Exception {
848848
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
849849
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
850850
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
851-
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ"));
851+
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ="));
852852
assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
853853

854854
builder = client().prepareSearch().setQuery(matchAllQuery())
@@ -873,7 +873,7 @@ public void testDocValueFields() throws Exception {
873873
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
874874
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
875875
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
876-
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ"));
876+
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ="));
877877
assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
878878

879879
builder = client().prepareSearch().setQuery(matchAllQuery())
@@ -909,7 +909,7 @@ public void testDocValueFields() throws Exception {
909909
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
910910
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
911911
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
912-
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ"));
912+
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ="));
913913
assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
914914

915915
builder = client().prepareSearch().setQuery(matchAllQuery())

server/src/main/java/org/elasticsearch/search/DocValueFormat.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ public void writeTo(StreamOutput out) {
168168
@Override
169169
public String format(BytesRef value) {
170170
return Base64.getEncoder()
171-
.withoutPadding()
172171
.encodeToString(Arrays.copyOfRange(value.bytes, value.offset, value.offset + value.length));
173172
}
174173

server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldMapperTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.io.OutputStream;
2222
import java.util.Arrays;
23+
import java.util.Base64;
2324

2425
import static org.hamcrest.Matchers.instanceOf;
2526

@@ -121,9 +122,9 @@ public void testStoredValue() throws IOException {
121122

122123
@Override
123124
protected Object generateRandomInputValue(MappedFieldType ft) {
124-
assumeFalse("We can't parse the binary doc values we send", true);
125-
// AwaitsFix https://github.com/elastic/elasticsearch/issues/70244
126-
return null;
125+
if (rarely()) return null;
126+
byte[] value = randomByteArrayOfLength(randomIntBetween(1, 50));
127+
return Base64.getEncoder().encodeToString(value);
127128
}
128129

129130
@Override

server/src/test/java/org/elasticsearch/search/DocValueFormatTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ public void testRawFormat() {
126126

127127
public void testBinaryFormat() {
128128
assertEquals("", DocValueFormat.BINARY.format(new BytesRef()));
129-
assertEquals("KmQ", DocValueFormat.BINARY.format(new BytesRef(new byte[] {42, 100})));
129+
assertEquals("KmQ=", DocValueFormat.BINARY.format(new BytesRef(new byte[] {42, 100})));
130130

131131
assertEquals(new BytesRef(), DocValueFormat.BINARY.parseBytesRef(""));
132-
assertEquals(new BytesRef(new byte[] {42, 100}), DocValueFormat.BINARY.parseBytesRef("KmQ"));
132+
assertEquals(new BytesRef(new byte[] {42, 100}), DocValueFormat.BINARY.parseBytesRef("KmQ="));
133133
}
134134

135135
public void testBooleanFormat() {

0 commit comments

Comments
 (0)