From 44e815635e2565c0b042cfe558a7451226c89488 Mon Sep 17 00:00:00 2001 From: Kostas Krikellas Date: Tue, 10 Oct 2023 15:06:09 +0300 Subject: [PATCH] Don't print synthetic source in mapping for bwc tests #2 --- .../index/mapper/SourceFieldMapper.java | 38 +++++++++++++++---- .../index/mapper/SourceFieldMapperTests.java | 5 +-- .../query/SearchExecutionContextTests.java | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java index aeab22a6f5f35..a98c78409d951 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java @@ -114,17 +114,17 @@ public static class Builder extends MetadataFieldMapper.Builder { ); private final IndexMode indexMode; + private final boolean setDefaultSytheticMode; - public Builder(IndexMode indexMode, IndexVersion indexVersion) { + public Builder(IndexMode indexMode, boolean setDefaultSytheticMode) { super(Defaults.NAME); this.indexMode = indexMode; + this.setDefaultSytheticMode = setDefaultSytheticMode; this.mode = new Parameter<>( "mode", true, // The default mode for TimeSeries is left empty on purpose, so that mapping printings include the synthetic source mode. - () -> getIndexMode() == IndexMode.TIME_SERIES && indexVersion.between(IndexVersion.V_8_7_0, IndexVersion.V_8_10_0) - ? Mode.SYNTHETIC - : null, + () -> getIndexMode() == IndexMode.TIME_SERIES && setDefaultSytheticMode ? Mode.SYNTHETIC : null, (n, c, o) -> Mode.valueOf(o.toString().toUpperCase(Locale.ROOT)), m -> toType(m).enabled.explicit() ? null : toType(m).mode, (b, n, v) -> b.field(n, v.toString().toLowerCase(Locale.ROOT)), @@ -171,7 +171,8 @@ public SourceFieldMapper build() { enabled.get(), includes.getValue().toArray(String[]::new), excludes.getValue().toArray(String[]::new), - indexMode + indexMode, + setDefaultSytheticMode ); if (indexMode != null) { indexMode.validateSourceFieldMapper(sourceFieldMapper); @@ -188,7 +189,10 @@ private IndexMode getIndexMode() { c -> c.getIndexSettings().getMode() == IndexMode.TIME_SERIES ? c.getIndexSettings().getIndexVersionCreated().onOrAfter(IndexVersion.V_8_7_0) ? TSDB_DEFAULT : TSDB_LEGACY_DEFAULT : DEFAULT, - c -> new Builder(c.getIndexSettings().getMode(), c.getIndexSettings().getIndexVersionCreated()) + c -> new Builder( + c.getIndexSettings().getMode(), + c.getIndexSettings().getIndexVersionCreated().between(IndexVersion.V_8_7_0, IndexVersion.V_8_10_0) + ) ); static final class SourceFieldType extends MappedFieldType { @@ -230,8 +234,25 @@ public Query termQuery(Object value, SearchExecutionContext context) { private final SourceFilter sourceFilter; private final IndexMode indexMode; + private final boolean setDefaultSytheticMode; + + private SourceFieldMapper( + Mode mode, + Explicit enabled, + String[] includes, + String[] excludes, + IndexMode indexMode) { + this(mode, enabled, includes, excludes, indexMode, false); + } - private SourceFieldMapper(Mode mode, Explicit enabled, String[] includes, String[] excludes, IndexMode indexMode) { + private SourceFieldMapper( + Mode mode, + Explicit enabled, + String[] includes, + String[] excludes, + IndexMode indexMode, + boolean setDefaultSytheticMode + ) { super(new SourceFieldType((enabled.explicit() && enabled.value()) || (enabled.explicit() == false && mode != Mode.DISABLED))); assert enabled.explicit() == false || mode == null; this.mode = mode; @@ -244,6 +265,7 @@ private SourceFieldMapper(Mode mode, Explicit enabled, String[] include } this.complete = stored() && sourceFilter == null; this.indexMode = indexMode; + this.setDefaultSytheticMode = setDefaultSytheticMode; } private static SourceFilter buildSourceFilter(String[] includes, String[] excludes) { @@ -313,7 +335,7 @@ protected String contentType() { @Override public FieldMapper.Builder getMergeBuilder() { - return new Builder(indexMode, IndexVersion.current()).init(this); + return new Builder(indexMode, setDefaultSytheticMode).init(this); } /** diff --git a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java index 433ebc467483d..2655e252aebfa 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.IndexMode; -import org.elasticsearch.index.IndexVersion; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; @@ -242,8 +241,8 @@ public void testSyntheticSourceInTimeSeries() throws IOException { } public void testSyntheticSourceInTimeSeriesBwc() throws IOException { - SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(IndexMode.TIME_SERIES, IndexVersion.V_8_8_0).build(); + SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(IndexMode.TIME_SERIES, true).build(); assertTrue(sourceMapper.isSynthetic()); - assertEquals("{\"_source\":{\"mode\":\"synthetic\"}}", sourceMapper.toString()); + assertEquals("{}", sourceMapper.toString()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java b/server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java index 9df1dc24c2793..253045fd15098 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SearchExecutionContextTests.java @@ -381,7 +381,7 @@ public void testSearchRequestRuntimeFieldsAndMultifieldDetection() { public void testSyntheticSourceSearchLookup() throws IOException { // Build a mapping using synthetic source - SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, IndexVersion.current()).setSynthetic().build(); + SourceFieldMapper sourceMapper = new SourceFieldMapper.Builder(null, false).setSynthetic().build(); RootObjectMapper root = new RootObjectMapper.Builder("_doc", Explicit.IMPLICIT_TRUE).add( new KeywordFieldMapper.Builder("cat", IndexVersion.current()).ignoreAbove(100) ).build(MapperBuilderContext.root(true, false));