Skip to content

Commit 9cc7573

Browse files
lktsjeffganmr
andauthored
fix stale data in synthetic source for string stored field (#123105) (#123277)
Co-authored-by: jeffganmr <[email protected]>
1 parent 4a22613 commit 9cc7573

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

docs/changelog/123105.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 123105
2+
summary: fix stale data in synthetic source for string stored field
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 123110

server/src/main/java/org/elasticsearch/index/mapper/StringStoredFieldFieldLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final void write(XContentBuilder b) throws IOException {
5454
case 1:
5555
b.field(simpleName);
5656
write(b, values.get(0));
57-
return;
57+
break;
5858
default:
5959
b.startArray(simpleName);
6060
for (Object value : values) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index.mapper;
11+
12+
import org.elasticsearch.common.Strings;
13+
import org.elasticsearch.test.ESTestCase;
14+
import org.elasticsearch.xcontent.XContentBuilder;
15+
import org.elasticsearch.xcontent.XContentType;
16+
17+
import java.io.IOException;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.stream.Collectors;
21+
22+
public class StringStoredFieldFieldLoaderTests extends ESTestCase {
23+
24+
public void testLoadStoredFieldAndReset() throws IOException {
25+
var sfl = new StringStoredFieldFieldLoader("foo", "foo") {
26+
@Override
27+
protected void write(XContentBuilder b, Object value) throws IOException {
28+
b.value((String) value);
29+
}
30+
};
31+
32+
var storedFieldLoaders = sfl.storedFieldLoaders().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
33+
storedFieldLoaders.get("foo").load(List.of("one"));
34+
35+
var result = XContentBuilder.builder(XContentType.JSON.xContent());
36+
result.startObject();
37+
sfl.write(result);
38+
result.endObject();
39+
40+
assertEquals("""
41+
{"foo":"one"}""", Strings.toString(result));
42+
43+
var empty = XContentBuilder.builder(XContentType.JSON.xContent());
44+
empty.startObject();
45+
// reset() should have been called after previous write
46+
sfl.write(empty);
47+
empty.endObject();
48+
49+
assertEquals("{}", Strings.toString(empty));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)