Skip to content

Commit 42f9c8a

Browse files
committed
Make FieldNamesFieldMapper responsible for adding its own doc fields (#71929)
The FieldNamesFieldMapper is a metadata mapper defining a field that can be used for exists queries if a mapper does not use doc values or norms. Currently, data is added to it via a special method on FieldMapper that pulls the metadata mapper from a mapping lookup, checks to see if it is enabled, and then adds the relevant value to a lucene document. This is one of only two places that pulls a metadata mapper from the MappingLookup, and it would be nice to remove this method. This commit refactors field name handling by instead storing the names of fields to index in the fieldnames field in a set on the ParseContext, and then building the field itself in FieldNamesFieldMapper.postParse(). This means that all of the responsibility for enabling indexing, etc, is handled within the metadata mapper itself.
1 parent c57fe85 commit 42f9c8a

File tree

28 files changed

+72
-38
lines changed

28 files changed

+72
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
331331

332332
Field field = new Field(fieldType().name(), value, fieldType);
333333
context.doc().add(field);
334-
createFieldNamesField(context);
334+
context.addToFieldNames(fieldType().name());
335335
}
336336

337337
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
379379
context.doc().addAll(fields);
380380

381381
if (hasDocValues == false && (indexed || stored)) {
382-
createFieldNamesField(context);
382+
context.addToFieldNames(fieldType().name());
383383
}
384384
}
385385

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
588588
context.doc().add(new Field(prefixField.fieldType().name(), value, prefixField.getLuceneFieldType()));
589589
}
590590
if (fieldType().fieldType.omitNorms()) {
591-
createFieldNamesField(context);
591+
context.addToFieldNames(fieldType().name());
592592
}
593593
}
594594

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void processQuery(Query query, ParseContext context) {
433433
doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, INDEXED_KEYWORD));
434434
}
435435

436-
createFieldNamesField(context);
436+
context.addToFieldNames(fieldType().name());
437437
if (indexVersionCreated.onOrAfter(Version.V_6_1_0)) {
438438
doc.add(new NumericDocValuesField(minimumShouldMatchFieldMapper.name(), result.minimumShouldMatch));
439439
}

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void testExtractTermsAndRanges_partial() throws Exception {
287287
ParseContext.Document document = parseContext.doc();
288288

289289
PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
290-
assertThat(document.getFields().size(), equalTo(4));
290+
assertThat(document.getFields().size(), equalTo(3));
291291
assertThat(document.getFields().get(0).binaryValue().utf8ToString(), equalTo("field\u0000term"));
292292
assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL));
293293
}

plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
464464
if (hasDocValues) {
465465
context.doc().add(new SortedSetDocValuesField(fieldType().name(), binaryValue));
466466
} else if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) {
467-
createFieldNamesField(context);
467+
context.addToFieldNames(fieldType().name());
468468
}
469469
}
470470

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
539539
Field field = new Field(mappedFieldType.name(), value, fieldType);
540540
context.doc().add(field);
541541
if (fieldType.omitNorms()) {
542-
createFieldNamesField(context);
542+
context.addToFieldNames(fieldType().name());
543543
}
544544
}
545545
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void indexValue(ParseContext context, byte[] value) {
173173
// Only add an entry to the field names field if the field is stored
174174
// but has no doc values so exists query will work on a field with
175175
// no doc values
176-
createFieldNamesField(context);
176+
context.addToFieldNames(fieldType().name());
177177
}
178178
}
179179

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private void indexValue(ParseContext context, Boolean value) {
295295
if (hasDocValues) {
296296
context.doc().add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0));
297297
} else {
298-
createFieldNamesField(context);
298+
context.addToFieldNames(fieldType().name());
299299
}
300300
}
301301

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public void parse(ParseContext context) throws IOException {
372372
}
373373
}
374374

375-
createFieldNamesField(context);
375+
context.addToFieldNames(fieldType().name());
376376
for (CompletionInputMetadata metadata: inputMap.values()) {
377377
ParseContext externalValueContext = context.createExternalValueContext(metadata);
378378
multiFields.parse(this, externalValueContext);

0 commit comments

Comments
 (0)