Skip to content

Commit d23b7e7

Browse files
committed
Added TextFamilyFieldType + Builder
1 parent 5540f13 commit d23b7e7

File tree

14 files changed

+154
-228
lines changed

14 files changed

+154
-228
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
import org.elasticsearch.index.mapper.SourceFieldMapper;
5555
import org.elasticsearch.index.mapper.SourceLoader;
5656
import org.elasticsearch.index.mapper.SourceValueFetcher;
57-
import org.elasticsearch.index.mapper.StringFieldType;
57+
import org.elasticsearch.index.mapper.TextFamilyFieldMapper;
58+
import org.elasticsearch.index.mapper.TextFamilyFieldType;
5859
import org.elasticsearch.index.mapper.TextFieldMapper;
5960
import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType;
6061
import org.elasticsearch.index.mapper.TextParams;
@@ -100,9 +101,7 @@ public static class Defaults {
100101

101102
}
102103

103-
public static class Builder extends FieldMapper.Builder {
104-
105-
private final IndexVersion indexCreatedVersion;
104+
public static class Builder extends TextFamilyFieldMapper.Builder {
106105

107106
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
108107

@@ -117,8 +116,7 @@ public Builder(
117116
boolean isSyntheticSourceEnabled,
118117
boolean isWithinMultiField
119118
) {
120-
super(name, isSyntheticSourceEnabled, isWithinMultiField);
121-
this.indexCreatedVersion = indexCreatedVersion;
119+
super(name, indexCreatedVersion, isSyntheticSourceEnabled, isWithinMultiField);
122120
this.analyzers = new TextParams.Analyzers(
123121
indexAnalyzers,
124122
m -> ((MatchOnlyTextFieldMapper) m).indexAnalyzer,
@@ -187,7 +185,7 @@ private static boolean isSyntheticSourceStoredFieldInBinaryFormat(IndexVersion i
187185
)
188186
);
189187

190-
public static class MatchOnlyTextFieldType extends StringFieldType {
188+
public static class MatchOnlyTextFieldType extends TextFamilyFieldType {
191189

192190
private final Analyzer indexAnalyzer;
193191
private final TextFieldType textFieldType;
@@ -586,7 +584,7 @@ protected BytesRef toBytesRef(Object v) {
586584

587585
@Override
588586
public BlockLoader blockLoader(BlockLoaderContext blContext) {
589-
if (textFieldType.isSyntheticSourceEnabled()) {
587+
if (isSyntheticSourceEnabled()) {
590588
final String fieldName = syntheticSourceFallbackFieldName();
591589
if (storedFieldInBinaryFormat) {
592590
return new BlockStoredFieldsReader.BytesFromBytesRefsBlockLoader(fieldName);
@@ -605,7 +603,7 @@ public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext
605603
if (fieldDataContext.fielddataOperation() != FielddataOperation.SCRIPT) {
606604
throw new IllegalArgumentException(CONTENT_TYPE + " fields do not support sorting and aggregations");
607605
}
608-
if (textFieldType.isSyntheticSourceEnabled()) {
606+
if (isSyntheticSourceEnabled()) {
609607
return (cache, breaker) -> new StoredFieldSortedBinaryIndexFieldData(
610608
syntheticSourceFallbackFieldName(),
611609
CoreValuesSourceType.KEYWORD,
@@ -652,7 +650,7 @@ private MatchOnlyTextFieldMapper(
652650
assert mappedFieldType.hasDocValues() == false;
653651

654652
this.fieldType = freezeAndDeduplicateFieldType(fieldType);
655-
this.indexCreatedVersion = builder.indexCreatedVersion;
653+
this.indexCreatedVersion = builder.indexCreatedVersion();
656654
this.indexAnalyzers = builder.analyzers.indexAnalyzers;
657655
this.indexAnalyzer = builder.analyzers.getIndexAnalyzer();
658656
this.positionIncrementGap = builder.analyzers.positionIncrementGap.getValue();

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.index.mapper.SourceFieldMapper;
3636
import org.elasticsearch.index.mapper.SourceLoader;
3737
import org.elasticsearch.index.mapper.StringStoredFieldFieldLoader;
38+
import org.elasticsearch.index.mapper.TextFamilyFieldMapper;
3839
import org.elasticsearch.index.mapper.TextFieldMapper;
3940
import org.elasticsearch.index.mapper.TextParams;
4041
import org.elasticsearch.index.mapper.TextSearchInfo;
@@ -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());
@@ -90,7 +91,6 @@ public static class Builder extends FieldMapper.Builder {
9091

9192
private final Parameter<Map<String, String>> meta = Parameter.metaParam();
9293

93-
private final IndexVersion indexCreatedVersion;
9494
private final TextParams.Analyzers analyzers;
9595
private final Parameter<Boolean> store;
9696

@@ -101,8 +101,7 @@ public Builder(
101101
boolean isSyntheticSourceEnabled,
102102
boolean isWithinMultiField
103103
) {
104-
super(name, isSyntheticSourceEnabled, isWithinMultiField);
105-
this.indexCreatedVersion = indexCreatedVersion;
104+
super(name, indexCreatedVersion, isSyntheticSourceEnabled, isWithinMultiField);
106105
this.analyzers = new TextParams.Analyzers(
107106
indexAnalyzers,
108107
m -> builder(m).analyzers.getIndexAnalyzer(),
@@ -113,7 +112,7 @@ public Builder(
113112
}
114113

115114
private boolean storeDefault() {
116-
if (TextFieldMapper.keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion)) {
115+
if (TextFieldMapper.keywordMultiFieldsNotStoredWhenIgnored_indexVersionCheck(indexCreatedVersion())) {
117116
return false;
118117
}
119118
return isSyntheticSourceEnabled() && multiFieldsBuilder.hasSyntheticSourceCompatibleKeywordField() == false;
@@ -544,7 +543,7 @@ protected AnnotatedTextFieldMapper(
544543
this.fieldType = freezeAndDeduplicateFieldType(fieldType);
545544
this.builder = builder;
546545
this.indexAnalyzer = wrapAnalyzer(builder.analyzers.getIndexAnalyzer());
547-
this.indexCreatedVersion = builder.indexCreatedVersion;
546+
this.indexCreatedVersion = builder.indexCreatedVersion();
548547
}
549548

550549
@Override
@@ -603,7 +602,7 @@ protected String contentType() {
603602
public FieldMapper.Builder getMergeBuilder() {
604603
return new Builder(
605604
leafName(),
606-
builder.indexCreatedVersion,
605+
builder.indexCreatedVersion(),
607606
builder.analyzers.indexAnalyzers,
608607
fieldType().isSyntheticSourceEnabled(),
609608
fieldType().isWithinMultiField()

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ public Optional<SourceKeepMode> sourceKeepMode() {
149149
return builderParams.sourceKeepMode;
150150
}
151151

152-
public boolean isSyntheticSourceEnabled() {
153-
return fieldType().isSyntheticSourceEnabled();
154-
}
155-
156-
public boolean isWithinMultiField() {
157-
return fieldType().isWithinMultiField();
158-
}
159-
160152
/**
161153
* Will this field ignore malformed values for this field and accept the
162154
* document ({@code true}) or will it reject documents with malformed
@@ -1416,22 +1408,11 @@ public abstract static class Builder extends Mapper.Builder implements ToXConten
14161408
protected boolean hasScript = false;
14171409
protected OnScriptError onScriptError = null;
14181410

1419-
// we're using Boolean here instead of boolean by design - to throw a NPE when users attempt to use these fields without
1420-
// initializing them first
1421-
private final Boolean isSyntheticSourceEnabled;
1422-
private final Boolean isWithinMultiField;
1423-
14241411
/**
14251412
* Creates a new Builder with a field name
14261413
*/
14271414
protected Builder(String name) {
1428-
this(name, null, null);
1429-
}
1430-
1431-
protected Builder(String name, Boolean isSyntheticSourceEnabled, Boolean isWithinMultiField) {
14321415
super(name);
1433-
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
1434-
this.isWithinMultiField = isWithinMultiField;
14351416
}
14361417

14371418
/**
@@ -1447,16 +1428,6 @@ public Builder init(FieldMapper initializer) {
14471428
return this;
14481429
}
14491430

1450-
public boolean isSyntheticSourceEnabled() {
1451-
Objects.requireNonNull(isSyntheticSourceEnabled, "You're attempting to use isSyntheticSourceEnabled without setting it first!");
1452-
return isSyntheticSourceEnabled;
1453-
}
1454-
1455-
public boolean isWithinMultiField() {
1456-
Objects.requireNonNull(isWithinMultiField, "You're attempting to use isWithinMultiField without setting it first!");
1457-
return isWithinMultiField;
1458-
}
1459-
14601431
public Builder addMultiField(FieldMapper.Builder builder) {
14611432
this.multiFieldsBuilder.add(builder);
14621433
return this;

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 TextFamilyFieldType {
550550

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

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,13 @@ public abstract class MappedFieldType {
7272
private final TextSearchInfo textSearchInfo;
7373
private final Map<String, String> meta;
7474

75-
// we're using Boolean here instead of boolean by design - to throw a NPE when users attempt to use these fields without
76-
// initializing them first
77-
private final Boolean isSyntheticSourceEnabled;
78-
private final Boolean isWithinMultiField;
79-
8075
public MappedFieldType(
8176
String name,
8277
boolean isIndexed,
8378
boolean isStored,
8479
boolean hasDocValues,
8580
TextSearchInfo textSearchInfo,
8681
Map<String, String> meta
87-
) {
88-
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, null, null);
89-
}
90-
91-
public MappedFieldType(
92-
String name,
93-
boolean isIndexed,
94-
boolean isStored,
95-
boolean hasDocValues,
96-
TextSearchInfo textSearchInfo,
97-
Map<String, String> meta,
98-
Boolean isSyntheticSourceEnabled,
99-
Boolean isWithinMultiField
10082
) {
10183
this.name = Mapper.internFieldName(name);
10284
this.isIndexed = isIndexed;
@@ -106,26 +88,6 @@ public MappedFieldType(
10688
// meta should be sorted but for the one item or empty case we can fall back to immutable maps to save some memory since order is
10789
// irrelevant
10890
this.meta = meta.size() <= 1 ? Map.copyOf(meta) : meta;
109-
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
110-
this.isWithinMultiField = isWithinMultiField;
111-
}
112-
113-
public boolean isSyntheticSourceEnabled() {
114-
Objects.requireNonNull(isSyntheticSourceEnabled, "You're attempting to use isSyntheticSourceEnabled without setting it first!");
115-
return isSyntheticSourceEnabled;
116-
}
117-
118-
public boolean isWithinMultiField() {
119-
Objects.requireNonNull(isWithinMultiField, "You're attempting to use isWithinMultiField without setting it first!");
120-
return isWithinMultiField;
121-
}
122-
123-
/**
124-
* Returns the name of the "fallback" field that can be used for synthetic source when the "main" field was not
125-
* stored for whatever reason.
126-
*/
127-
public String syntheticSourceFallbackFieldName() {
128-
return isSyntheticSourceEnabled ? name() + "._original" : null;
12991
}
13092

13193
/**

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,7 @@ protected SimpleMappedFieldType(
3030
TextSearchInfo textSearchInfo,
3131
Map<String, String> meta
3232
) {
33-
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, null, null);
34-
}
35-
36-
protected SimpleMappedFieldType(
37-
String name,
38-
boolean isIndexed,
39-
boolean isStored,
40-
boolean hasDocValues,
41-
TextSearchInfo textSearchInfo,
42-
Map<String, String> meta,
43-
Boolean isSyntheticSourceEnabled,
44-
Boolean isWithinMultiField
45-
) {
46-
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, isSyntheticSourceEnabled, isWithinMultiField);
33+
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
4734
}
4835

4936
@Override

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,7 @@ public StringFieldType(
5151
TextSearchInfo textSearchInfo,
5252
Map<String, String> meta
5353
) {
54-
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, null, null);
55-
}
56-
57-
public StringFieldType(
58-
String name,
59-
boolean isIndexed,
60-
boolean isStored,
61-
boolean hasDocValues,
62-
TextSearchInfo textSearchInfo,
63-
Map<String, String> meta,
64-
Boolean isSyntheticSourceEnabled,
65-
Boolean isWithinMultiField
66-
) {
67-
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, isSyntheticSourceEnabled, isWithinMultiField);
54+
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
6855
}
6956

7057
@Override

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,7 @@ public TermBasedFieldType(
3434
TextSearchInfo textSearchInfo,
3535
Map<String, String> meta
3636
) {
37-
this(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, null, null);
38-
}
39-
40-
public TermBasedFieldType(
41-
String name,
42-
boolean isIndexed,
43-
boolean isStored,
44-
boolean hasDocValues,
45-
TextSearchInfo textSearchInfo,
46-
Map<String, String> meta,
47-
Boolean isSyntheticSourceEnabled,
48-
Boolean isWithinMultiField
49-
) {
50-
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta, isSyntheticSourceEnabled, isWithinMultiField);
37+
super(name, isIndexed, isStored, hasDocValues, textSearchInfo, meta);
5138
}
5239

5340
/** Returns the indexed value used to construct search "values".
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.index.mapper;
11+
12+
import org.elasticsearch.index.IndexVersion;
13+
14+
public abstract class TextFamilyFieldMapper extends FieldMapper {
15+
16+
protected TextFamilyFieldMapper(String simpleName, MappedFieldType mappedFieldType, BuilderParams params) {
17+
super(simpleName, mappedFieldType, params);
18+
}
19+
20+
public abstract static class Builder extends FieldMapper.Builder {
21+
22+
private final IndexVersion indexCreatedVersion;
23+
private final boolean isSyntheticSourceEnabled;
24+
private final boolean isWithinMultiField;
25+
26+
protected Builder(String name, IndexVersion indexCreatedVersion, boolean isSyntheticSourceEnabled, boolean isWithinMultiField) {
27+
super(name);
28+
this.indexCreatedVersion = indexCreatedVersion;
29+
this.isSyntheticSourceEnabled = isSyntheticSourceEnabled;
30+
this.isWithinMultiField = isWithinMultiField;
31+
}
32+
33+
public IndexVersion indexCreatedVersion() {
34+
return indexCreatedVersion;
35+
}
36+
37+
public boolean isSyntheticSourceEnabled() {
38+
return isSyntheticSourceEnabled;
39+
}
40+
41+
public boolean isWithinMultiField() {
42+
return isWithinMultiField;
43+
}
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)