Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/123105.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 123105
summary: fix stale data in synthetic source for string stored field
area: Mapping
type: bug
issues:
- 123110
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final void write(XContentBuilder b) throws IOException {
case 1:
b.field(simpleName);
write(b, values.get(0));
return;
break;
default:
b.startArray(simpleName);
for (Object value : values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.index.mapper;

import org.elasticsearch.common.Strings;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StringStoredFieldFieldLoaderTests extends ESTestCase {

public void testLoadStoredFieldAndReset() throws IOException {
var sfl = new StringStoredFieldFieldLoader("foo", "foo") {
@Override
protected void write(XContentBuilder b, Object value) throws IOException {
b.value((String) value);
}
};

var storedFieldLoaders = sfl.storedFieldLoaders().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
storedFieldLoaders.get("foo").load(List.of("one"));

var result = XContentBuilder.builder(XContentType.JSON.xContent());
result.startObject();
sfl.write(result);
result.endObject();

assertEquals("""
{"foo":"one"}""", Strings.toString(result));

var empty = XContentBuilder.builder(XContentType.JSON.xContent());
empty.startObject();
// reset() should have been called after previous write
sfl.write(empty);
empty.endObject();

assertEquals("{}", Strings.toString(empty));
}

}