Skip to content

Commit 3779789

Browse files
committed
Added TextFamilyFieldType and undid changes to StringFieldType
1 parent 4d0f262 commit 3779789

File tree

9 files changed

+126
-93
lines changed

9 files changed

+126
-93
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
import org.elasticsearch.index.mapper.KeywordFieldMapper;
5252
import org.elasticsearch.index.mapper.MappedFieldType;
5353
import org.elasticsearch.index.mapper.MapperBuilderContext;
54+
import org.elasticsearch.index.mapper.SourceFieldMapper;
5455
import org.elasticsearch.index.mapper.SourceLoader;
5556
import org.elasticsearch.index.mapper.SourceValueFetcher;
56-
import org.elasticsearch.index.mapper.StringFieldType;
5757
import org.elasticsearch.index.mapper.TextFamilyFieldMapper;
5858
import org.elasticsearch.index.mapper.TextFieldMapper;
5959
import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType;
@@ -99,26 +99,24 @@ public static class Defaults {
9999

100100
}
101101

102-
public static class Builder extends FieldMapper.Builder {
102+
public static class Builder extends TextFamilyFieldMapper.Builder {
103103

104104
private final IndexVersion indexCreatedVersion;
105105

106106
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
107107

108108
private final TextParams.Analyzers analyzers;
109109
private final boolean storedFieldInBinaryFormat;
110-
private final boolean isWithinMultiField;
111-
112-
private boolean isSyntheticSourceEnabled;
113110

114111
public Builder(
115112
String name,
116113
IndexVersion indexCreatedVersion,
117114
IndexAnalyzers indexAnalyzers,
118115
boolean storedFieldInBinaryFormat,
116+
boolean isSyntheticSourceEnabled,
119117
boolean isWithinMultiField
120118
) {
121-
super(name);
119+
super(name, isSyntheticSourceEnabled, isWithinMultiField);
122120
this.indexCreatedVersion = indexCreatedVersion;
123121
this.analyzers = new TextParams.Analyzers(
124122
indexAnalyzers,
@@ -127,7 +125,6 @@ public Builder(
127125
indexCreatedVersion
128126
);
129127
this.storedFieldInBinaryFormat = storedFieldInBinaryFormat;
130-
this.isWithinMultiField = isWithinMultiField;
131128
}
132129

133130
@Override
@@ -164,8 +161,6 @@ private FieldType getFieldType() {
164161

165162
@Override
166163
public MatchOnlyTextFieldMapper build(MapperBuilderContext context) {
167-
this.isSyntheticSourceEnabled = context.isSourceSynthetic();
168-
169164
BuilderParams builderParams = builderParams(this, context);
170165
MatchOnlyTextFieldType tft = buildFieldType(context, builderParams.multiFields());
171166
return new MatchOnlyTextFieldMapper(leafName(), Defaults.FIELD_TYPE, tft, builderParams, this);
@@ -186,11 +181,12 @@ private static boolean isSyntheticSourceStoredFieldInBinaryFormat(IndexVersion i
186181
c.indexVersionCreated(),
187182
c.getIndexAnalyzers(),
188183
isSyntheticSourceStoredFieldInBinaryFormat(c.indexVersionCreated()),
184+
SourceFieldMapper.isSynthetic(c.getIndexSettings()),
189185
c.isWithinMultiField()
190186
)
191187
);
192188

193-
public static class MatchOnlyTextFieldType extends StringFieldType {
189+
public static class MatchOnlyTextFieldType extends TextFamilyFieldType {
194190

195191
private final Analyzer indexAnalyzer;
196192
private final TextFieldType textFieldType;
@@ -662,7 +658,14 @@ public Map<String, NamedAnalyzer> indexAnalyzers() {
662658

663659
@Override
664660
public FieldMapper.Builder getMergeBuilder() {
665-
return new Builder(leafName(), indexCreatedVersion, indexAnalyzers, storedFieldInBinaryFormat, isWithinMultiField).init(this);
661+
return new Builder(
662+
leafName(),
663+
indexCreatedVersion,
664+
indexAnalyzers,
665+
storedFieldInBinaryFormat,
666+
isSyntheticSourceEnabled,
667+
isWithinMultiField
668+
).init(this);
666669
}
667670

668671
@Override

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.index.mapper.FieldMapper;
3333
import org.elasticsearch.index.mapper.KeywordFieldMapper;
3434
import org.elasticsearch.index.mapper.MapperBuilderContext;
35+
import org.elasticsearch.index.mapper.SourceFieldMapper;
3536
import org.elasticsearch.index.mapper.SourceLoader;
3637
import org.elasticsearch.index.mapper.StringStoredFieldFieldLoader;
3738
import org.elasticsearch.index.mapper.TextFamilyFieldMapper;
@@ -81,7 +82,7 @@ private static NamedAnalyzer wrapAnalyzer(NamedAnalyzer in) {
8182
);
8283
}
8384

84-
public static class Builder extends FieldMapper.Builder {
85+
public static class Builder extends TextFamilyFieldMapper.Builder {
8586

8687
final Parameter<SimilarityProvider> similarity = TextParams.similarity(m -> builder(m).similarity.getValue());
8788
final Parameter<String> indexOptions = TextParams.textIndexOptions(m -> builder(m).indexOptions.getValue());
@@ -92,27 +93,31 @@ public static class Builder extends FieldMapper.Builder {
9293

9394
private final IndexVersion indexCreatedVersion;
9495
private final TextParams.Analyzers analyzers;
95-
private final boolean isWithinMultiField;
9696
private final Parameter<Boolean> store;
9797

98-
private boolean isSyntheticSourceEnabled;
99-
100-
public Builder(String name, IndexVersion indexCreatedVersion, IndexAnalyzers indexAnalyzers, boolean isWithinMultiField) {
101-
super(name);
98+
public Builder(
99+
String name,
100+
IndexVersion indexCreatedVersion,
101+
IndexAnalyzers indexAnalyzers,
102+
boolean isSyntheticSourceEnabled,
103+
boolean isWithinMultiField
104+
) {
105+
super(name, isSyntheticSourceEnabled, isWithinMultiField);
102106
this.indexCreatedVersion = indexCreatedVersion;
103-
this.isWithinMultiField = isWithinMultiField;
104107
this.analyzers = new TextParams.Analyzers(
105108
indexAnalyzers,
106109
m -> builder(m).analyzers.getIndexAnalyzer(),
107110
m -> builder(m).analyzers.positionIncrementGap.getValue(),
108111
indexCreatedVersion
109112
);
110-
this.store = Parameter.storeParam(m -> builder(m).store.getValue(), () -> {
111-
if (keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion)) {
112-
return false;
113-
}
114-
return isSyntheticSourceEnabled && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
115-
});
113+
this.store = Parameter.storeParam(m -> builder(m).store.getValue(), this::storeDefault);
114+
}
115+
116+
private boolean storeDefault() {
117+
if (keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion)) {
118+
return false;
119+
}
120+
return isSyntheticSourceEnabled && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
116121
}
117122

118123
@Override
@@ -162,7 +167,6 @@ public AnnotatedTextFieldMapper build(MapperBuilderContext context) {
162167
}
163168
}
164169
BuilderParams builderParams = builderParams(this, context);
165-
this.isSyntheticSourceEnabled = context.isSourceSynthetic();
166170
return new AnnotatedTextFieldMapper(
167171
leafName(),
168172
fieldType,
@@ -174,7 +178,13 @@ public AnnotatedTextFieldMapper build(MapperBuilderContext context) {
174178
}
175179

176180
public static final TypeParser PARSER = new TypeParser(
177-
(n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers(), c.isWithinMultiField())
181+
(n, c) -> new Builder(
182+
n,
183+
c.indexVersionCreated(),
184+
c.getIndexAnalyzers(),
185+
SourceFieldMapper.isSynthetic(c.getIndexSettings()),
186+
c.isWithinMultiField()
187+
)
178188
);
179189

180190
/**
@@ -591,7 +601,13 @@ protected String contentType() {
591601

592602
@Override
593603
public FieldMapper.Builder getMergeBuilder() {
594-
return new Builder(leafName(), builder.indexCreatedVersion, builder.analyzers.indexAnalyzers, isWithinMultiField).init(this);
604+
return new Builder(
605+
leafName(),
606+
builder.indexCreatedVersion,
607+
builder.analyzers.indexAnalyzers,
608+
isSyntheticSourceEnabled,
609+
isWithinMultiField
610+
).init(this);
595611
}
596612

597613
@Override

plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void testFetchSourceValue() throws IOException {
3434
"field",
3535
IndexVersion.current(),
3636
createDefaultIndexAnalyzers(),
37+
false,
3738
false
3839
).build(MapperBuilderContext.root(false, false)).fieldType();
3940

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ private static boolean indexSortConfigByHostName(final IndexSortConfig indexSort
546546

547547
public static final TypeParser PARSER = createTypeParserWithLegacySupport(Builder::new);
548548

549-
public static final class KeywordFieldType extends StringFieldType {
549+
public static final class KeywordFieldType extends TextFamilyFieldMapper.TextFamilyFieldType {
550550

551551
private final int ignoreAbove;
552552
private final String nullValue;

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,15 @@ public abstract class StringFieldType extends TermBasedFieldType {
4343

4444
private static final Pattern WILDCARD_PATTERN = Pattern.compile("(\\\\.)|([?*]+)");
4545

46-
// using Boolean instead of boolean is deliberate as not all children of this class care about these two fields
47-
protected final Boolean isSyntheticSourceEnabled;
48-
protected final Boolean isWithinMultiField;
49-
5046
public StringFieldType(
5147
String name,
5248
boolean isIndexed,
5349
boolean isStored,
5450
boolean hasDocValues,
5551
TextSearchInfo textSearchInfo,
5652
Map<String, String> meta
57-
) {
58-
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, null, null);
59-
}
60-
61-
public StringFieldType(
62-
String name,
63-
boolean isIndexed,
64-
boolean isStored,
65-
boolean hasDocValues,
66-
TextSearchInfo textSearchInfo,
67-
Map<String, String> meta,
68-
Boolean isSyntheticSourceEnabled,
69-
Boolean isWithinMultiField
7053
) {
7154
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
72-
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
73-
this.isWithinMultiField = isWithinMultiField;
7455
}
7556

7657
@Override
@@ -243,12 +224,4 @@ public Query rangeQuery(
243224
includeUpper
244225
);
245226
}
246-
247-
/**
248-
* Returns the name of the "fallback" field that can be used for synthetic source when the "main" field was not
249-
* stored for whatever reason.
250-
*/
251-
public String syntheticSourceFallbackFieldName() {
252-
return isSyntheticSourceEnabled ? name() + "._original" : null;
253-
}
254227
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.index.IndexVersion;
1313
import org.elasticsearch.index.IndexVersions;
1414

15+
import java.util.Map;
16+
1517
/**
1618
* This class is meant to contain common functionality that is needed by the Text family of field mappers. Namely
1719
* {@link TextFieldMapper} and anything strongly related to it.
@@ -70,4 +72,47 @@ protected static boolean keywordMultiFieldsNotStoredWhenIgnored_indexVersionChec
7072
return indexCreatedVersion.onOrAfter(IndexVersions.KEYWORD_MULTI_FIELDS_NOT_STORED_WHEN_IGNORED);
7173
}
7274

75+
public abstract static class Builder extends FieldMapper.Builder {
76+
77+
public final boolean isSyntheticSourceEnabled;
78+
public final boolean isWithinMultiField;
79+
80+
protected Builder(final String name, final boolean isSyntheticSourceEnabled, final boolean isWithinMultiField) {
81+
super(name);
82+
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
83+
this.isWithinMultiField = isWithinMultiField;
84+
}
85+
86+
}
87+
88+
public abstract static class TextFamilyFieldType extends StringFieldType {
89+
90+
protected final boolean isSyntheticSourceEnabled;
91+
protected final boolean isWithinMultiField;
92+
93+
public TextFamilyFieldType(
94+
String name,
95+
boolean isIndexed,
96+
boolean isStored,
97+
boolean hasDocValues,
98+
TextSearchInfo textSearchInfo,
99+
Map<String, String> meta,
100+
boolean isSyntheticSourceEnabled,
101+
boolean isWithinMultiField
102+
) {
103+
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
104+
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
105+
this.isWithinMultiField = isWithinMultiField;
106+
}
107+
108+
/**
109+
* Returns the name of the "fallback" field that can be used for synthetic source when the "main" field was not
110+
* stored for whatever reason.
111+
*/
112+
public String syntheticSourceFallbackFieldName() {
113+
return isSyntheticSourceEnabled ? name() + "._original" : null;
114+
}
115+
116+
}
117+
73118
}

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private static FielddataFrequencyFilter parseFrequencyFilter(String name, Mappin
237237
return new FielddataFrequencyFilter(minFrequency, maxFrequency, minSegmentSize);
238238
}
239239

240-
public static class Builder extends FieldMapper.Builder {
240+
public static class Builder extends TextFamilyFieldMapper.Builder {
241241

242242
private final IndexVersion indexCreatedVersion;
243243
private final Parameter<Boolean> norms;
@@ -289,9 +289,6 @@ public static class Builder extends FieldMapper.Builder {
289289

290290
final TextParams.Analyzers analyzers;
291291

292-
private boolean isSyntheticSourceEnabled;
293-
private final boolean isWithinMultiField;
294-
295292
public Builder(String name, IndexAnalyzers indexAnalyzers) {
296293
this(name, IndexVersion.current(), null, indexAnalyzers, false, false);
297294
}
@@ -304,11 +301,10 @@ public Builder(
304301
boolean isSyntheticSourceEnabled,
305302
boolean isWithinMultiField
306303
) {
307-
super(name);
304+
super(name, isSyntheticSourceEnabled, isWithinMultiField);
308305

309306
this.indexCreatedVersion = indexCreatedVersion;
310307
this.indexMode = indexMode;
311-
this.isWithinMultiField = isWithinMultiField;
312308
this.analyzers = new TextParams.Analyzers(
313309
indexAnalyzers,
314310
m -> ((TextFieldMapper) m).indexAnalyzer,
@@ -322,20 +318,23 @@ public Builder(
322318
() -> indexMode != IndexMode.LOGSDB && indexMode != IndexMode.TIME_SERIES
323319
);
324320

325-
// // backwards compatibility checks
326-
this.store = Parameter.storeParam(m -> ((TextFieldMapper) m).store, () -> {
327-
if (keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion)) {
328-
return false;
329-
}
321+
this.store = Parameter.storeParam(m -> ((TextFieldMapper) m).store, this::storeDefault);
322+
}
330323

331-
if (multiFieldsNotStoredByDefault_indexVersionCheck(indexCreatedVersion)) {
332-
return isSyntheticSourceEnabled
333-
&& isWithinMultiField == false
334-
&& multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
335-
} else {
336-
return isSyntheticSourceEnabled && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
337-
}
338-
});
324+
private boolean storeDefault() {
325+
// ideally and for simplicity, store should be set to false by default
326+
if (keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion)) {
327+
return false;
328+
}
329+
330+
// however, because historically we set store to true to support synthetic source, we must also keep that logic:
331+
if (multiFieldsNotStoredByDefault_indexVersionCheck(indexCreatedVersion)) {
332+
return isSyntheticSourceEnabled
333+
&& isWithinMultiField == false
334+
&& multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
335+
} else {
336+
return isSyntheticSourceEnabled && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
337+
}
339338
}
340339

341340
public Builder index(boolean index) {
@@ -485,8 +484,6 @@ private SubFieldInfo buildPhraseInfo(FieldType fieldType, TextFieldType parent)
485484

486485
@Override
487486
public TextFieldMapper build(MapperBuilderContext context) {
488-
this.isSyntheticSourceEnabled = context.isSourceSynthetic();
489-
490487
FieldType fieldType = TextParams.buildFieldType(
491488
index,
492489
store,
@@ -684,7 +681,7 @@ private static final class SubFieldInfo {
684681

685682
}
686683

687-
public static class TextFieldType extends StringFieldType {
684+
public static class TextFieldType extends TextFamilyFieldType {
688685

689686
private boolean fielddata;
690687
private FielddataFrequencyFilter filter;

0 commit comments

Comments
 (0)