Skip to content

Commit 44c3418

Browse files
committed
Simplify handling of keyword field normalizers (#42002)
We have a number of places in analysis-handling code where we check if a field type is a keyword field, and if so then extract the normalizer rather than pulling the index-time analyzer. However, a keyword normalizer is really just a special case of an analyzer, so we should be able to simplify this by setting the normalizer as the index-time analyzer at construction time.
1 parent 809ed3b commit 44c3418

File tree

8 files changed

+13
-35
lines changed

8 files changed

+13
-35
lines changed

plugins/mapper-annotated-text/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/AnnotatedTextHighlighter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class AnnotatedTextHighlighter extends UnifiedHighlighter {
3939
public static final String NAME = "annotated";
4040

4141
@Override
42-
protected Analyzer getAnalyzer(DocumentMapper docMapper, MappedFieldType type, HitContext hitContext) {
43-
return new AnnotatedHighlighterAnalyzer(super.getAnalyzer(docMapper, type, hitContext), hitContext);
42+
protected Analyzer getAnalyzer(DocumentMapper docMapper, HitContext hitContext) {
43+
return new AnnotatedHighlighterAnalyzer(super.getAnalyzer(docMapper, hitContext), hitContext);
4444
}
4545

4646
// Convert the marked-up values held on-disk to plain-text versions for highlighting

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
import org.elasticsearch.index.analysis.CharFilterFactory;
5151
import org.elasticsearch.index.analysis.CustomAnalyzer;
5252
import org.elasticsearch.index.analysis.IndexAnalyzers;
53+
import org.elasticsearch.index.analysis.NamedAnalyzer;
5354
import org.elasticsearch.index.analysis.NormalizingCharFilterFactory;
5455
import org.elasticsearch.index.analysis.NormalizingTokenFilterFactory;
55-
import org.elasticsearch.index.analysis.NamedAnalyzer;
5656
import org.elasticsearch.index.analysis.TokenFilterFactory;
5757
import org.elasticsearch.index.analysis.TokenizerFactory;
5858
import org.elasticsearch.index.mapper.KeywordFieldMapper;
@@ -141,14 +141,8 @@ protected AnalyzeResponse shardOperation(AnalyzeRequest request, ShardId shardId
141141
}
142142
MappedFieldType fieldType = indexService.mapperService().fullName(request.field());
143143
if (fieldType != null) {
144-
if (fieldType.tokenized()) {
144+
if (fieldType.tokenized() || fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
145145
analyzer = fieldType.indexAnalyzer();
146-
} else if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
147-
analyzer = ((KeywordFieldMapper.KeywordFieldType) fieldType).normalizer();
148-
if (analyzer == null) {
149-
// this will be KeywordAnalyzer
150-
analyzer = fieldType.indexAnalyzer();
151-
}
152146
} else {
153147
throw new IllegalArgumentException("Can't process field [" + request.field() +
154148
"], Analysis requests are only supported on tokenized fields");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,14 @@ public String typeName() {
235235
return CONTENT_TYPE;
236236
}
237237

238-
public NamedAnalyzer normalizer() {
238+
private NamedAnalyzer normalizer() {
239239
return normalizer;
240240
}
241241

242242
public void setNormalizer(NamedAnalyzer normalizer) {
243243
checkIfFrozen();
244244
this.normalizer = normalizer;
245+
setIndexAnalyzer(normalizer);
245246
}
246247

247248
public boolean splitQueriesOnWhitespace() {

server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.elasticsearch.index.get.GetResult;
4545
import org.elasticsearch.index.mapper.DocumentMapperForType;
4646
import org.elasticsearch.index.mapper.IdFieldMapper;
47-
import org.elasticsearch.index.mapper.KeywordFieldMapper;
4847
import org.elasticsearch.index.mapper.MappedFieldType;
4948
import org.elasticsearch.index.mapper.MapperService;
5049
import org.elasticsearch.index.mapper.ParseContext;
@@ -235,12 +234,7 @@ private static Analyzer getAnalyzerAtField(IndexShard indexShard, String field,
235234
analyzer = mapperService.getIndexAnalyzers().get(perFieldAnalyzer.get(field).toString());
236235
} else {
237236
MappedFieldType fieldType = mapperService.fullName(field);
238-
if (fieldType instanceof KeywordFieldMapper.KeywordFieldType) {
239-
KeywordFieldMapper.KeywordFieldType keywordFieldType = (KeywordFieldMapper.KeywordFieldType) fieldType;
240-
analyzer = keywordFieldType.normalizer() == null ? keywordFieldType.indexAnalyzer() : keywordFieldType.normalizer();
241-
} else {
242-
analyzer = fieldType.indexAnalyzer();
243-
}
237+
analyzer = fieldType.indexAnalyzer();
244238
}
245239
if (analyzer == null) {
246240
analyzer = mapperService.getIndexAnalyzers().getDefaultIndexAnalyzer();

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightUtils.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@
1818
*/
1919
package org.elasticsearch.search.fetch.subphase.highlight;
2020

21-
import org.apache.lucene.analysis.Analyzer;
2221
import org.apache.lucene.search.highlight.DefaultEncoder;
2322
import org.apache.lucene.search.highlight.Encoder;
2423
import org.apache.lucene.search.highlight.SimpleHTMLEncoder;
2524
import org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor;
26-
import org.elasticsearch.index.mapper.DocumentMapper;
27-
import org.elasticsearch.index.mapper.KeywordFieldMapper;
2825
import org.elasticsearch.index.mapper.MappedFieldType;
2926
import org.elasticsearch.search.fetch.FetchSubPhase;
3027
import org.elasticsearch.search.internal.SearchContext;
@@ -78,13 +75,4 @@ public static class Encoders {
7875
public static final Encoder HTML = new SimpleHTMLEncoder();
7976
}
8077

81-
static Analyzer getAnalyzer(DocumentMapper docMapper, MappedFieldType type) {
82-
if (type instanceof KeywordFieldMapper.KeywordFieldType) {
83-
KeywordFieldMapper.KeywordFieldType keywordFieldType = (KeywordFieldMapper.KeywordFieldType) type;
84-
if (keywordFieldType.normalizer() != null) {
85-
return keywordFieldType.normalizer();
86-
}
87-
}
88-
return docMapper.mappers().indexAnalyzer();
89-
}
9078
}

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
101101
int numberOfFragments = field.fieldOptions().numberOfFragments() == 0 ? 1 : field.fieldOptions().numberOfFragments();
102102
ArrayList<TextFragment> fragsList = new ArrayList<>();
103103
List<Object> textsToHighlight;
104-
Analyzer analyzer = HighlightUtils.getAnalyzer(context.mapperService().documentMapper(hitContext.hit().getType()), fieldType);
104+
Analyzer analyzer = context.mapperService().documentMapper(hitContext.hit().getType()).mappers().indexAnalyzer();
105105
final int maxAnalyzedOffset = context.indexShard().indexSettings().getHighlightMaxAnalyzedOffset();
106106

107107
try {

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
7070
int numberOfFragments;
7171
try {
7272

73-
final Analyzer analyzer = getAnalyzer(context.mapperService().documentMapper(hitContext.hit().getType()), fieldType,
73+
final Analyzer analyzer = getAnalyzer(context.mapperService().documentMapper(hitContext.hit().getType()),
7474
hitContext);
7575
List<Object> fieldValues = loadFieldValues(fieldType, field, context, hitContext);
7676
if (fieldValues.size() == 0) {
@@ -150,8 +150,8 @@ protected PassageFormatter getPassageFormatter(HitContext hitContext, SearchCont
150150
}
151151

152152

153-
protected Analyzer getAnalyzer(DocumentMapper docMapper, MappedFieldType type, HitContext hitContext) {
154-
return HighlightUtils.getAnalyzer(docMapper, type);
153+
protected Analyzer getAnalyzer(DocumentMapper docMapper, HitContext hitContext) {
154+
return docMapper.mappers().indexAnalyzer();
155155
}
156156

157157
protected List<Object> loadFieldValues(MappedFieldType fieldType, SearchContextHighlight.Field field, SearchContext context,

server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ public void testUpdateNormalizer() throws IOException {
401401
() -> indexService.mapperService().merge("type",
402402
new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE));
403403
assertEquals(
404-
"Mapper for [field] conflicts with existing mapping:\n[mapper [field] has different [normalizer]]",
404+
"Mapper for [field] conflicts with existing mapping:\n" +
405+
"[mapper [field] has different [analyzer], mapper [field] has different [normalizer]]",
405406
e.getMessage());
406407
}
407408

0 commit comments

Comments
 (0)