Skip to content

Commit 682ed39

Browse files
authored
Remove IndexMode#isSyntheticSourceEnabled (#114963) (#115144)
(cherry picked from commit 16bde51) # Conflicts: # server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java
1 parent 87eef3c commit 682ed39

File tree

15 files changed

+259
-199
lines changed

15 files changed

+259
-199
lines changed

plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.index.mapper.FieldMapper;
3232
import org.elasticsearch.index.mapper.KeywordFieldMapper;
3333
import org.elasticsearch.index.mapper.MapperBuilderContext;
34+
import org.elasticsearch.index.mapper.SourceFieldMapper;
3435
import org.elasticsearch.index.mapper.StringStoredFieldFieldLoader;
3536
import org.elasticsearch.index.mapper.TextFieldMapper;
3637
import org.elasticsearch.index.mapper.TextParams;
@@ -91,15 +92,10 @@ public static class Builder extends FieldMapper.Builder {
9192

9293
private final IndexVersion indexCreatedVersion;
9394
private final TextParams.Analyzers analyzers;
94-
private final boolean isSyntheticSourceEnabledViaIndexMode;
95+
private final boolean isSyntheticSourceEnabled;
9596
private final Parameter<Boolean> store;
9697

97-
public Builder(
98-
String name,
99-
IndexVersion indexCreatedVersion,
100-
IndexAnalyzers indexAnalyzers,
101-
boolean isSyntheticSourceEnabledViaIndexMode
102-
) {
98+
public Builder(String name, IndexVersion indexCreatedVersion, IndexAnalyzers indexAnalyzers, boolean isSyntheticSourceEnabled) {
10399
super(name);
104100
this.indexCreatedVersion = indexCreatedVersion;
105101
this.analyzers = new TextParams.Analyzers(
@@ -108,10 +104,10 @@ public Builder(
108104
m -> builder(m).analyzers.positionIncrementGap.getValue(),
109105
indexCreatedVersion
110106
);
111-
this.isSyntheticSourceEnabledViaIndexMode = isSyntheticSourceEnabledViaIndexMode;
107+
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
112108
this.store = Parameter.storeParam(
113109
m -> builder(m).store.getValue(),
114-
() -> isSyntheticSourceEnabledViaIndexMode && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false
110+
() -> isSyntheticSourceEnabled && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false
115111
);
116112
}
117113

@@ -172,7 +168,7 @@ public AnnotatedTextFieldMapper build(MapperBuilderContext context) {
172168
}
173169

174170
public static TypeParser PARSER = new TypeParser(
175-
(n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers(), c.getIndexSettings().getMode().isSyntheticSourceEnabled())
171+
(n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers(), SourceFieldMapper.isSynthetic(c.getIndexSettings()))
176172
);
177173

178174
/**
@@ -560,12 +556,8 @@ protected String contentType() {
560556

561557
@Override
562558
public FieldMapper.Builder getMergeBuilder() {
563-
return new Builder(
564-
leafName(),
565-
builder.indexCreatedVersion,
566-
builder.analyzers.indexAnalyzers,
567-
builder.isSyntheticSourceEnabledViaIndexMode
568-
).init(this);
559+
return new Builder(leafName(), builder.indexCreatedVersion, builder.analyzers.indexAnalyzers, builder.isSyntheticSourceEnabled)
560+
.init(this);
569561
}
570562

571563
@Override

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/logsdb/20_source_mapping.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
---
2+
synthetic _source is default:
3+
- requires:
4+
cluster_features: ["mapper.source.remove_synthetic_source_only_validation"]
5+
reason: requires new validation logic
6+
7+
- do:
8+
indices.create:
9+
index: test-default-source
10+
body:
11+
settings:
12+
index:
13+
mode: logsdb
14+
- do:
15+
indices.get:
16+
index: test-default-source
17+
18+
- match: { test-default-source.mappings._source.mode: "synthetic" }
19+
120
---
221
stored _source mode is supported:
322
- requires:
@@ -57,3 +76,77 @@ disabled _source is not supported:
5776
- match: { error.type: "mapper_parsing_exception" }
5877
- match: { error.root_cause.0.type: "mapper_parsing_exception" }
5978
- match: { error.reason: "Failed to parse mapping: _source can not be disabled in index using [logsdb] index mode" }
79+
80+
---
81+
include/exclude is not supported with synthetic _source:
82+
- requires:
83+
cluster_features: ["mapper.source.remove_synthetic_source_only_validation"]
84+
reason: requires new validation logic
85+
86+
- do:
87+
catch: '/filtering the stored _source is incompatible with synthetic source/'
88+
indices.create:
89+
index: test-includes
90+
body:
91+
settings:
92+
index:
93+
mode: logsdb
94+
mappings:
95+
_source:
96+
includes: [a]
97+
98+
- do:
99+
catch: '/filtering the stored _source is incompatible with synthetic source/'
100+
indices.create:
101+
index: test-excludes
102+
body:
103+
settings:
104+
index:
105+
mode: logsdb
106+
mappings:
107+
_source:
108+
excludes: [b]
109+
110+
---
111+
include/exclude is supported with stored _source:
112+
- requires:
113+
cluster_features: ["mapper.source.remove_synthetic_source_only_validation"]
114+
reason: requires new validation logic
115+
116+
- do:
117+
indices.create:
118+
index: test-includes
119+
body:
120+
settings:
121+
index:
122+
mode: logsdb
123+
mappings:
124+
_source:
125+
mode: stored
126+
includes: [a]
127+
128+
- do:
129+
indices.get:
130+
index: test-includes
131+
132+
- match: { test-includes.mappings._source.mode: "stored" }
133+
- match: { test-includes.mappings._source.includes: ["a"] }
134+
135+
- do:
136+
indices.create:
137+
index: test-excludes
138+
body:
139+
settings:
140+
index:
141+
mode: logsdb
142+
mappings:
143+
_source:
144+
mode: stored
145+
excludes: [b]
146+
147+
- do:
148+
indices.get:
149+
index: test-excludes
150+
151+
- match: { test-excludes.mappings._source.mode: "stored" }
152+
- match: { test-excludes.mappings._source.excludes: ["b"] }

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/tsdb/20_mapping.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,36 @@ disabled source is not supported:
528528
- match: { error.root_cause.0.type: "mapper_parsing_exception" }
529529
- match: { error.reason: "Failed to parse mapping: _source can not be disabled in index using [time_series] index mode" }
530530

531+
- do:
532+
catch: bad_request
533+
indices.create:
534+
index: tsdb_index
535+
body:
536+
settings:
537+
index:
538+
mode: time_series
539+
routing_path: [k8s.pod.uid]
540+
time_series:
541+
start_time: 2021-04-28T00:00:00Z
542+
end_time: 2021-04-29T00:00:00Z
543+
mappings:
544+
_source:
545+
enabled: false
546+
properties:
547+
"@timestamp":
548+
type: date
549+
k8s:
550+
properties:
551+
pod:
552+
properties:
553+
uid:
554+
type: keyword
555+
time_series_dimension: true
556+
557+
- match: { error.type: "mapper_parsing_exception" }
558+
- match: { error.root_cause.0.type: "mapper_parsing_exception" }
559+
- match: { error.reason: "Failed to parse mapping: _source can not be disabled in index using [time_series] index mode" }
560+
531561
---
532562
source include/exclude:
533563
- requires:

server/src/main/java/org/elasticsearch/index/IndexMode.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public boolean shouldValidateTimestamp() {
120120
public void validateSourceFieldMapper(SourceFieldMapper sourceFieldMapper) {}
121121

122122
@Override
123-
public boolean isSyntheticSourceEnabled() {
124-
return false;
123+
public SourceFieldMapper.Mode defaultSourceMode() {
124+
return SourceFieldMapper.Mode.STORED;
125125
}
126126
},
127127
TIME_SERIES("time_series") {
@@ -223,8 +223,8 @@ public void validateSourceFieldMapper(SourceFieldMapper sourceFieldMapper) {
223223
}
224224

225225
@Override
226-
public boolean isSyntheticSourceEnabled() {
227-
return true;
226+
public SourceFieldMapper.Mode defaultSourceMode() {
227+
return SourceFieldMapper.Mode.SYNTHETIC;
228228
}
229229
},
230230
LOGSDB("logsdb") {
@@ -300,8 +300,8 @@ public void validateSourceFieldMapper(SourceFieldMapper sourceFieldMapper) {
300300
}
301301

302302
@Override
303-
public boolean isSyntheticSourceEnabled() {
304-
return true;
303+
public SourceFieldMapper.Mode defaultSourceMode() {
304+
return SourceFieldMapper.Mode.SYNTHETIC;
305305
}
306306

307307
@Override
@@ -460,9 +460,9 @@ public String getName() {
460460
public abstract void validateSourceFieldMapper(SourceFieldMapper sourceFieldMapper);
461461

462462
/**
463-
* @return whether synthetic source is the only allowed source mode.
463+
* @return default source mode for this mode
464464
*/
465-
public abstract boolean isSyntheticSourceEnabled();
465+
public abstract SourceFieldMapper.Mode defaultSourceMode();
466466

467467
public String getDefaultCodec() {
468468
return CodecService.DEFAULT_CODEC;

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public static class Builder extends FieldMapper.Builder {
4949
private final Parameter<Boolean> stored = Parameter.storeParam(m -> toType(m).stored, false);
5050
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
5151

52-
private final boolean isSyntheticSourceEnabledViaIndexMode;
52+
private final boolean isSyntheticSourceEnabled;
5353
private final Parameter<Boolean> hasDocValues;
5454

55-
public Builder(String name, boolean isSyntheticSourceEnabledViaIndexMode) {
55+
public Builder(String name, boolean isSyntheticSourceEnabled) {
5656
super(name);
57-
this.isSyntheticSourceEnabledViaIndexMode = isSyntheticSourceEnabledViaIndexMode;
58-
this.hasDocValues = Parameter.docValuesParam(m -> toType(m).hasDocValues, isSyntheticSourceEnabledViaIndexMode);
57+
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
58+
this.hasDocValues = Parameter.docValuesParam(m -> toType(m).hasDocValues, isSyntheticSourceEnabled);
5959
}
6060

6161
@Override
@@ -79,9 +79,7 @@ public BinaryFieldMapper build(MapperBuilderContext context) {
7979
}
8080
}
8181

82-
public static final TypeParser PARSER = new TypeParser(
83-
(n, c) -> new Builder(n, c.getIndexSettings().getMode().isSyntheticSourceEnabled())
84-
);
82+
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, SourceFieldMapper.isSynthetic(c.getIndexSettings())));
8583

8684
public static final class BinaryFieldType extends MappedFieldType {
8785
private BinaryFieldType(String name, boolean isStored, boolean hasDocValues, Map<String, String> meta) {
@@ -140,13 +138,13 @@ public Query termQuery(Object value, SearchExecutionContext context) {
140138

141139
private final boolean stored;
142140
private final boolean hasDocValues;
143-
private final boolean isSyntheticSourceEnabledViaIndexMode;
141+
private final boolean isSyntheticSourceEnabled;
144142

145143
protected BinaryFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams builderParams, Builder builder) {
146144
super(simpleName, mappedFieldType, builderParams);
147145
this.stored = builder.stored.getValue();
148146
this.hasDocValues = builder.hasDocValues.getValue();
149-
this.isSyntheticSourceEnabledViaIndexMode = builder.isSyntheticSourceEnabledViaIndexMode;
147+
this.isSyntheticSourceEnabled = builder.isSyntheticSourceEnabled;
150148
}
151149

152150
@Override
@@ -186,7 +184,7 @@ public void indexValue(DocumentParserContext context, byte[] value) {
186184

187185
@Override
188186
public FieldMapper.Builder getMergeBuilder() {
189-
return new BinaryFieldMapper.Builder(leafName(), isSyntheticSourceEnabledViaIndexMode).init(this);
187+
return new BinaryFieldMapper.Builder(leafName(), isSyntheticSourceEnabled).init(this);
190188
}
191189

192190
@Override

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,10 @@ public boolean newDynamicStringField(DocumentParserContext context, String name)
334334
);
335335
} else {
336336
return createDynamicField(
337-
new TextFieldMapper.Builder(
338-
name,
339-
context.indexAnalyzers(),
340-
context.indexSettings().getMode().isSyntheticSourceEnabled()
341-
).addMultiField(
342-
new KeywordFieldMapper.Builder("keyword", context.indexSettings().getIndexVersionCreated()).ignoreAbove(256)
343-
),
337+
new TextFieldMapper.Builder(name, context.indexAnalyzers(), SourceFieldMapper.isSynthetic(context.indexSettings()))
338+
.addMultiField(
339+
new KeywordFieldMapper.Builder("keyword", context.indexSettings().getIndexVersionCreated()).ignoreAbove(256)
340+
),
344341
context
345342
);
346343
}
@@ -412,10 +409,7 @@ public boolean newDynamicDateField(DocumentParserContext context, String name, D
412409
}
413410

414411
boolean newDynamicBinaryField(DocumentParserContext context, String name) throws IOException {
415-
return createDynamicField(
416-
new BinaryFieldMapper.Builder(name, context.indexSettings().getMode().isSyntheticSourceEnabled()),
417-
context
418-
);
412+
return createDynamicField(new BinaryFieldMapper.Builder(name, SourceFieldMapper.isSynthetic(context.indexSettings())), context);
419413
}
420414
}
421415

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ Mapping parse(@Nullable String type, MergeReason reason, Map<String, Object> map
124124
Map<Class<? extends MetadataFieldMapper>, MetadataFieldMapper> metadataMappers = metadataMappersSupplier.get();
125125
Map<String, Object> meta = null;
126126

127-
boolean isSourceSynthetic = mappingParserContext.getIndexSettings().getMode().isSyntheticSourceEnabled();
127+
// TODO this should be the final value once `_source.mode` mapping parameter is not used anymore
128+
// and it should not be reassigned below.
129+
// For now it is still possible to set `_source.mode` so this is correct.
130+
boolean isSourceSynthetic = SourceFieldMapper.isSynthetic(mappingParserContext.getIndexSettings());
128131
boolean isDataStream = false;
129132

130133
Iterator<Map.Entry<String, Object>> iterator = mappingSource.entrySet().iterator();

0 commit comments

Comments
 (0)