Skip to content

Commit

Permalink
Support arrays in fallback synthetic source implementation (#108878)
Browse files Browse the repository at this point in the history
This PR extends #108222 adding support for arrays.
  • Loading branch information
lkts committed May 23, 2024
1 parent 9d52fce commit 1ec137d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/108878.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 108878
summary: Support arrays in fallback synthetic source implementation
area: Mapping
type: feature
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,25 @@ protected boolean supportsIgnoreMalformed() {
@Override
protected SyntheticSourceSupport syntheticSourceSupport(boolean syntheticSource) {
return new SyntheticSourceSupport() {
@Override
public boolean preservesExactSource() {
return true;
}

public SyntheticSourceExample example(int maxValues) {
String value = rarely()
if (randomBoolean()) {
var value = generateValue();
return new SyntheticSourceExample(value, value, this::mapping);
}

var array = randomList(1, 5, this::generateValue);
return new SyntheticSourceExample(array, array, this::mapping);
}

private Object generateValue() {
return rarely()
? null
: randomList(0, 10, () -> randomAlphaOfLengthBetween(0, 10)).stream().collect(Collectors.joining(" "));

return new SyntheticSourceExample(value, value, this::mapping);
}

private void mapping(XContentBuilder b) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ setup:
body:
a_field: null

- do:
index:
index: test
id: "3"
body:
a_field: ["quick brown", "fox", "jumps"]

- do:
indices.refresh: {}

Expand All @@ -46,3 +53,10 @@ setup:
id: "2"

- match: { _source.a_field: null }

- do:
get:
index: test
id: "3"

- match: { _source.a_field: ["quick brown", "fox", "jumps"] }
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,10 @@ private static void parseNonDynamicArray(
) throws IOException {
// Check if we need to record the array source. This only applies to synthetic source.
if (context.mappingLookup().isSourceSynthetic() && context.getClonedSource() == false) {
if ((mapper instanceof ObjectMapper objectMapper && objectMapper.storeArraySource()) || mapper instanceof NestedObjectMapper) {
boolean storeArraySourceEnabled = mapper instanceof ObjectMapper objectMapper && objectMapper.storeArraySource();
boolean fieldWithFallbackSyntheticSource = mapper instanceof FieldMapper fieldMapper
&& fieldMapper.syntheticSourceMode() == FieldMapper.SyntheticSourceMode.FALLBACK;
if (storeArraySourceEnabled || fieldWithFallbackSyntheticSource || mapper instanceof NestedObjectMapper) {
// Clone the DocumentParserContext to parse its subtree twice.
Tuple<DocumentParserContext, XContentBuilder> tuple = XContentDataHelper.cloneSubContext(context);
context.addIgnoredField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,14 @@ private Object expectedParsedForBlockLoader() throws IOException {
public record SyntheticSourceInvalidExample(Matcher<String> error, CheckedConsumer<XContentBuilder, IOException> mapping) {}

public interface SyntheticSourceSupport {
/**
* @return True if synthetic source support is implemented to exactly store the source
* without modifications.
*/
default boolean preservesExactSource() {
return false;
}

/**
* Examples that should work when source is generated from doc values.
*/
Expand Down Expand Up @@ -1177,7 +1185,7 @@ public final void testSyntheticSourceMany() throws IOException {
) {
for (int i = 0; i < count; i++) {
if (rarely() && supportsEmptyInputArray()) {
expected[i] = "{}";
expected[i] = support.preservesExactSource() ? "{\"field\":[]}" : "{}";
iw.addDocument(mapper.parse(source(b -> b.startArray("field").endArray())).rootDoc());
continue;
}
Expand Down Expand Up @@ -1236,13 +1244,16 @@ public final void testSyntheticSourceInObject() throws IOException {
public final void testSyntheticEmptyList() throws IOException {
assumeTrue("Field does not support [] as input", supportsEmptyInputArray());
boolean ignoreMalformed = supportsIgnoreMalformed() ? rarely() : false;
SyntheticSourceExample syntheticSourceExample = syntheticSourceSupport(ignoreMalformed).example(5);
SyntheticSourceSupport support = syntheticSourceSupport(ignoreMalformed);
SyntheticSourceExample syntheticSourceExample = support.example(5);
DocumentMapper mapper = createDocumentMapper(syntheticSourceMapping(b -> {
b.startObject("field");
syntheticSourceExample.mapping().accept(b);
b.endObject();
}));
assertThat(syntheticSource(mapper, b -> b.startArray("field").endArray()), equalTo("{}"));

var expected = support.preservesExactSource() ? "{\"field\":[]}" : "{}";
assertThat(syntheticSource(mapper, b -> b.startArray("field").endArray()), equalTo(expected));
}

public final void testSyntheticEmptyListNoDocValuesLoader() throws IOException {
Expand Down

0 comments on commit 1ec137d

Please sign in to comment.