Skip to content

Commit ccfdbb4

Browse files
Fix binary docvalue_fields with padding (#70826)
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.
1 parent 76448ec commit ccfdbb4

File tree

6 files changed

+59
-19
lines changed

6 files changed

+59
-19
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public void testDocValueFields() throws Exception {
810810
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
811811
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
812812
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
813-
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ"));
813+
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ="));
814814
assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
815815

816816
builder = client().prepareSearch().setQuery(matchAllQuery())
@@ -835,7 +835,7 @@ public void testDocValueFields() throws Exception {
835835
assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true));
836836
assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo"));
837837
assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo"));
838-
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ"));
838+
assertThat(searchResponse.getHits().getAt(0).getFields().get("binary_field").getValue(), equalTo("KmQ="));
839839
assertThat(searchResponse.getHits().getAt(0).getFields().get("ip_field").getValue(), equalTo("::1"));
840840

841841
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
@@ -165,7 +165,6 @@ public void writeTo(StreamOutput out) {
165165
@Override
166166
public String format(BytesRef value) {
167167
return Base64.getEncoder()
168-
.withoutPadding()
169168
.encodeToString(Arrays.copyOfRange(value.bytes, value.offset, value.offset + value.length));
170169
}
171170

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)