From 3f6a27969e12956bb40fd89e8709de98790e8624 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Thu, 2 Sep 2021 17:25:33 +0200 Subject: [PATCH] Fix MatchOnlyTextFieldMapper Retaining a Reference to its Builder Just like #77131 but for the `MatchOnlyTextFieldMapper`. Also, cleaned up a few other minor things in it to make the constructor code for this class easier to follow. --- .../mapper/MatchOnlyTextFieldMapper.java | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapper.java index fff7ac2a86ce5..9f87a83fde2ec 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapper.java @@ -41,7 +41,6 @@ import java.io.IOException; import java.io.UncheckedIOException; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -72,10 +71,6 @@ public static class Defaults { } - private static Builder builder(FieldMapper in) { - return ((MatchOnlyTextFieldMapper) in).builder; - } - public static class Builder extends FieldMapper.Builder { private final Version indexCreatedVersion; @@ -91,37 +86,30 @@ public Builder(String name, IndexAnalyzers indexAnalyzers) { public Builder(String name, Version indexCreatedVersion, IndexAnalyzers indexAnalyzers) { super(name); this.indexCreatedVersion = indexCreatedVersion; - this.analyzers = new TextParams.Analyzers(indexAnalyzers, m -> builder(m).analyzers); - } - - public Builder addMultiField(FieldMapper.Builder builder) { - this.multiFieldsBuilder.add(builder); - return this; + this.analyzers = new TextParams.Analyzers(indexAnalyzers, m -> ((MatchOnlyTextFieldMapper) m).analyzers); } @Override protected List> getParameters() { - return Arrays.asList(meta); + return List.of(meta); } - private MatchOnlyTextFieldType buildFieldType(FieldType fieldType, ContentPath contentPath) { + private MatchOnlyTextFieldType buildFieldType(ContentPath contentPath) { NamedAnalyzer searchAnalyzer = analyzers.getSearchAnalyzer(); NamedAnalyzer searchQuoteAnalyzer = analyzers.getSearchQuoteAnalyzer(); NamedAnalyzer indexAnalyzer = analyzers.getIndexAnalyzer(); - TextSearchInfo tsi = new TextSearchInfo(fieldType, null, searchAnalyzer, searchQuoteAnalyzer); - MatchOnlyTextFieldType ft = new MatchOnlyTextFieldType(buildFullName(contentPath), tsi, indexAnalyzer, meta.getValue()); - return ft; + TextSearchInfo tsi = new TextSearchInfo(Defaults.FIELD_TYPE, null, searchAnalyzer, searchQuoteAnalyzer); + return new MatchOnlyTextFieldType(buildFullName(contentPath), tsi, indexAnalyzer, meta.getValue()); } @Override public MatchOnlyTextFieldMapper build(ContentPath contentPath) { - MatchOnlyTextFieldType tft = buildFieldType(Defaults.FIELD_TYPE, contentPath); + MatchOnlyTextFieldType tft = buildFieldType(contentPath); MultiFields multiFields = multiFieldsBuilder.build(this, contentPath); return new MatchOnlyTextFieldMapper( name, Defaults.FIELD_TYPE, tft, - analyzers.getIndexAnalyzer(), multiFields, copyTo.build(), this @@ -142,19 +130,6 @@ public MatchOnlyTextFieldType(String name, TextSearchInfo tsi, Analyzer indexAna this.textFieldType = new TextFieldType(name); } - public MatchOnlyTextFieldType(String name, boolean stored, Map meta) { - super( - name, - true, - stored, - false, - new TextSearchInfo(Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER), - meta - ); - this.indexAnalyzer = Lucene.STANDARD_ANALYZER; - this.textFieldType = new TextFieldType(name); - } - public MatchOnlyTextFieldType(String name) { this( name, @@ -292,28 +267,29 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S } - private final Builder builder; + private final Version indexCreatedVersion; + private final TextParams.Analyzers analyzers; private final FieldType fieldType; private MatchOnlyTextFieldMapper( String simpleName, FieldType fieldType, MatchOnlyTextFieldType mappedFieldType, - NamedAnalyzer indexAnalyzer, MultiFields multiFields, CopyTo copyTo, Builder builder ) { - super(simpleName, mappedFieldType, indexAnalyzer, multiFields, copyTo); + super(simpleName, mappedFieldType, builder.analyzers.getIndexAnalyzer(), multiFields, copyTo); assert mappedFieldType.getTextSearchInfo().isTokenized(); assert mappedFieldType.hasDocValues() == false; this.fieldType = fieldType; - this.builder = builder; + this.indexCreatedVersion = builder.indexCreatedVersion; + this.analyzers = builder.analyzers; } @Override public FieldMapper.Builder getMergeBuilder() { - return new Builder(simpleName(), builder.indexCreatedVersion, builder.analyzers.indexAnalyzers).init(this); + return new Builder(simpleName(), indexCreatedVersion, analyzers.indexAnalyzers).init(this); } @Override