From 0c509b4365a62ebe9e9ede4080b4d6b9b40b4768 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Tue, 20 Apr 2021 13:44:14 +0100 Subject: [PATCH 1/3] FieldNamesFieldMapper is resposible for building its own metadata document fields --- .../index/mapper/ScaledFloatFieldMapper.java | 2 +- .../mapper/SearchAsYouTypeFieldMapper.java | 2 +- .../percolator/PercolatorFieldMapper.java | 2 +- .../ICUCollationKeywordFieldMapper.java | 2 +- .../AnnotatedTextFieldMapper.java | 2 +- .../index/mapper/BinaryFieldMapper.java | 2 +- .../index/mapper/BooleanFieldMapper.java | 2 +- .../index/mapper/CompletionFieldMapper.java | 2 +- .../index/mapper/DateFieldMapper.java | 2 +- .../index/mapper/FieldMapper.java | 15 ------- .../index/mapper/FieldNamesFieldMapper.java | 45 +++++-------------- .../index/mapper/GeoPointFieldMapper.java | 2 +- .../index/mapper/GeoShapeFieldMapper.java | 2 +- .../index/mapper/IpFieldMapper.java | 2 +- .../index/mapper/KeywordFieldMapper.java | 2 +- .../mapper/LegacyGeoShapeFieldMapper.java | 2 +- .../index/mapper/NumberFieldMapper.java | 2 +- .../index/mapper/ParseContext.java | 34 ++++++++++++++ .../index/mapper/RangeFieldMapper.java | 2 +- .../index/mapper/RoutingFieldMapper.java | 2 +- .../index/mapper/TextFieldMapper.java | 2 +- .../flattened/FlattenedFieldMapper.java | 2 +- .../mapper/FieldNamesFieldMapperTests.java | 18 -------- .../unsignedlong/UnsignedLongFieldMapper.java | 2 +- .../GeoShapeWithDocValuesFieldMapper.java | 2 +- .../index/mapper/PointFieldMapper.java | 2 +- .../index/mapper/ShapeFieldMapper.java | 2 +- 27 files changed, 67 insertions(+), 91 deletions(-) diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java index 13c6f4d6dc245..a89b896204ebc 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java @@ -365,7 +365,7 @@ protected void parseCreateField(ParseContext context) throws IOException { context.doc().addAll(fields); if (hasDocValues == false && (indexed || stored)) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java index 84c2afc9bf3c6..b1ca3824f22e0 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java @@ -571,7 +571,7 @@ protected void parseCreateField(ParseContext context) throws IOException { context.doc().add(new Field(prefixField.fieldType().name(), value, prefixField.getLuceneFieldType())); } if (fieldType().fieldType.omitNorms()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java index bd8084028ef87..3c6323214365b 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java @@ -414,7 +414,7 @@ void processQuery(Query query, ParseContext context) { doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, INDEXED_KEYWORD)); } - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); doc.add(new NumericDocValuesField(minimumShouldMatchFieldMapper.name(), result.minimumShouldMatch)); } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java index a0de295102d45..0fc6ada72351b 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java @@ -462,7 +462,7 @@ protected void parseCreateField(ParseContext context) throws IOException { if (hasDocValues) { context.doc().add(new SortedSetDocValuesField(fieldType().name(), binaryValue)); } else if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java index a6fb248584ec8..ede73d96bff59 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java @@ -532,7 +532,7 @@ protected void parseCreateField(ParseContext context) throws IOException { Field field = new Field(mappedFieldType.name(), value, fieldType); context.doc().add(field); if (fieldType.omitNorms()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java index 3c0e1b00a8cd8..59b8a42512592 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java @@ -168,7 +168,7 @@ protected void parseCreateField(ParseContext context) throws IOException { // Only add an entry to the field names field if the field is stored // but has no doc values so exists query will work on a field with // no doc values - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java index 5195830a03551..d94bac31c8bc9 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java @@ -293,7 +293,7 @@ private void indexValue(ParseContext context, Boolean value) { if (hasDocValues) { context.doc().add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0)); } else { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index fef30796d08d6..43d24f2e1849f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -376,7 +376,7 @@ public void parse(ParseContext context) throws IOException { } } - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); for (CompletionInputMetadata metadata: inputMap.values()) { ParseContext externalValueContext = context.createExternalValueContext(metadata); multiFields.parse(this, externalValueContext); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java index b213f5828aede..d8b2c59832164 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java @@ -731,7 +731,7 @@ private void indexValue(ParseContext context, long timestamp) { if (hasDocValues) { context.doc().add(new SortedNumericDocValuesField(fieldType().name(), timestamp)); } else if (store || indexed) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } if (store) { context.doc().add(new StoredField(fieldType().name(), timestamp)); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index 6d595da5123a0..7fe80c9cebae0 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -8,7 +8,6 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.document.Field; import org.apache.lucene.index.LeafReaderContext; import org.elasticsearch.Version; import org.elasticsearch.common.Explicit; @@ -24,7 +23,6 @@ import org.elasticsearch.common.xcontent.support.AbstractXContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.analysis.NamedAnalyzer; -import org.elasticsearch.index.mapper.FieldNamesFieldMapper.FieldNamesFieldType; import org.elasticsearch.index.mapper.Mapper.TypeParser.ParserContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; @@ -266,19 +264,6 @@ protected void indexScriptValues(SearchLookup searchLookup, LeafReaderContext re throw new UnsupportedOperationException("FieldMapper " + name() + " does not support [script]"); } - protected final void createFieldNamesField(ParseContext context) { - assert fieldType().hasDocValues() == false : "_field_names should only be used when doc_values are turned off"; - FieldNamesFieldMapper fieldNamesFieldMapper = (FieldNamesFieldMapper) context.getMetadataMapper(FieldNamesFieldMapper.NAME); - if (fieldNamesFieldMapper != null) { - FieldNamesFieldType fieldNamesFieldType = fieldNamesFieldMapper.fieldType(); - if (fieldNamesFieldType != null && fieldNamesFieldType.isEnabled()) { - for (String fieldName : FieldNamesFieldMapper.extractFieldNames(fieldType().name())) { - context.doc().add(new Field(FieldNamesFieldMapper.NAME, fieldName, FieldNamesFieldMapper.Defaults.FIELD_TYPE)); - } - } - } - } - @Override public Iterator iterator() { Iterator multiFieldsIterator = multiFields.iterator(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java index e228d01a37390..7ec53d5761283 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java @@ -8,6 +8,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; @@ -17,8 +18,8 @@ import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.index.query.SearchExecutionContext; +import java.io.IOException; import java.util.Collections; -import java.util.Iterator; import java.util.List; /** @@ -155,40 +156,14 @@ public FieldNamesFieldType fieldType() { return (FieldNamesFieldType) super.fieldType(); } - static Iterable extractFieldNames(final String fullPath) { - return new Iterable() { - @Override - public Iterator iterator() { - return new Iterator<>() { - int endIndex = nextEndIndex(0); - - private int nextEndIndex(int index) { - while (index < fullPath.length() && fullPath.charAt(index) != '.') { - index += 1; - } - return index; - } - - @Override - public boolean hasNext() { - return endIndex <= fullPath.length(); - } - - @Override - public String next() { - final String result = fullPath.substring(0, endIndex); - endIndex = nextEndIndex(endIndex + 1); - return result; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - - }; - } - }; + @Override + public void postParse(ParseContext context) throws IOException { + if (enabled.value() == false) { + return; + } + for (String field : context.getFieldExistsFields()) { + context.doc().add(new Field(NAME, field, Defaults.FIELD_TYPE)); + } } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java index e5d8484e8eace..0aab19b3e546b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java @@ -191,7 +191,7 @@ protected void index(ParseContext context, GeoPoint geometry) throws IOException if (fieldType().hasDocValues()) { context.doc().add(new LatLonDocValuesField(fieldType().name(), geometry.lat(), geometry.lon())); } else if (fieldType().isStored() || fieldType().isSearchable()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } if (fieldType().isStored()) { context.doc().add(new StoredField(fieldType().name(), geometry.toString())); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java index 6f714b3a9ddf5..c2a7d589aaebf 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java @@ -188,7 +188,7 @@ protected void checkIncomingMergeType(FieldMapper mergeWith) { @Override protected void index(ParseContext context, Geometry geometry) throws IOException { context.doc().addAll(indexer.indexShape(geometry)); - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java index 8b57c0b325ca9..f831d6a6b0717 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java @@ -459,7 +459,7 @@ private void indexValue(ParseContext context, InetAddress address) { if (hasDocValues) { context.doc().add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address)))); } else if (stored || indexed) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } if (stored) { context.doc().add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address)))); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index baf46aca504c9..5749966f58525 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -417,7 +417,7 @@ private void indexValue(ParseContext context, String value) { context.doc().add(field); if (fieldType().hasDocValues() == false && fieldType.omitNorms()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java index d8daf28c21e40..dd1bd49702391 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java @@ -493,7 +493,7 @@ protected void index(ParseContext context, ShapeBuilder shapeBuilder) t } } context.doc().addAll(Arrays.asList(fieldType().defaultPrefixTreeStrategy().createIndexableFields(shape))); - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index b5b426de16661..c00066346ec1b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -1155,7 +1155,7 @@ private void indexValue(ParseContext context, Number numericValue) { indexed, hasDocValues, stored)); if (hasDocValues == false && (stored || indexed)) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index 75f280e2a315e..722cd7a738432 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -292,6 +292,16 @@ public void addIgnoredField(String field) { public Collection getIgnoredFields() { return in.getIgnoredFields(); } + + @Override + public void addFieldExistsField(String field) { + in.addFieldExistsField(field); + } + + @Override + public Collection getFieldExistsFields() { + return in.getFieldExistsFields(); + } } public static class InternalParseContext extends ParseContext { @@ -307,6 +317,7 @@ public static class InternalParseContext extends ParseContext { private final Map dynamicObjectMappers = new HashMap<>(); private final List dynamicRuntimeFields = new ArrayList<>(); private final Set ignoredFields = new HashSet<>(); + private final Set fieldNameFields = new HashSet<>(); private Field version; private SeqNoFieldMapper.SequenceIDFields seqID; private long numNestedDocs; @@ -490,6 +501,16 @@ public void addIgnoredField(String field) { public Collection getIgnoredFields() { return Collections.unmodifiableCollection(ignoredFields); } + + @Override + public void addFieldExistsField(String field) { + fieldNameFields.add(field); + } + + @Override + public Collection getFieldExistsFields() { + return Collections.unmodifiableCollection(fieldNameFields); + } } /** @@ -509,6 +530,19 @@ public Collection getIgnoredFields() { */ public abstract Collection getIgnoredFields(); + /** + * Add the given {@code field} to the _fieldnames field + * + * Use this if an exists query run against the field cannot use docvalues + * or norms. + */ + public abstract void addFieldExistsField(String field); + + /** + * Return the collection of fields to be added to the _fieldnames field + */ + public abstract Collection getFieldExistsFields(); + public abstract Mapper.TypeParser.ParserContext parserContext(DateFormatter dateFormatter); /** diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java index f4a49be19a28b..0c32e9097cb4f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java @@ -359,7 +359,7 @@ protected void parseCreateField(ParseContext context) throws IOException { context.doc().addAll(fieldType().rangeType.createFields(context, name(), range, index, hasDocValues, store)); if (hasDocValues == false && (index || store)) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java index 6e5f7681ab06d..94d7402b54884 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java @@ -104,7 +104,7 @@ public void preParse(ParseContext context) { String routing = context.sourceToParse().routing(); if (routing != null) { context.doc().add(new Field(fieldType().name(), routing, Defaults.FIELD_TYPE)); - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java index 8d3340a770589..53ed5ae7850b9 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java @@ -826,7 +826,7 @@ protected void parseCreateField(ParseContext context) throws IOException { Field field = new Field(fieldType().name(), value, fieldType); context.doc().add(field); if (fieldType.omitNorms()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } if (prefixFieldInfo != null) { context.doc().add(new Field(prefixFieldInfo.field, value, prefixFieldInfo.fieldType)); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java index 25b5665c5da56..249cac5c5f406 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapper.java @@ -473,7 +473,7 @@ protected void parseCreateField(ParseContext context) throws IOException { context.doc().addAll(fieldParser.parse(xContentParser)); if (mappedFieldType.hasDocValues() == false) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java index 4925aedda8231..f7e1d4754033e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java @@ -24,14 +24,6 @@ public class FieldNamesFieldMapperTests extends MapperServiceTestCase { - private static SortedSet extract(String path) { - SortedSet set = new TreeSet<>(); - for (String fieldName : FieldNamesFieldMapper.extractFieldNames(path)) { - set.add(fieldName); - } - return set; - } - private static SortedSet set(String... values) { return new TreeSet<>(Arrays.asList(values)); } @@ -41,16 +33,6 @@ void assertFieldNames(Set expected, ParsedDocument doc) { assertEquals(expected, set(got)); } - public void testExtractFieldNames() { - assertEquals(set("abc"), extract("abc")); - assertEquals(set("a", "a.b"), extract("a.b")); - assertEquals(set("a", "a.b", "a.b.c"), extract("a.b.c")); - // and now corner cases - assertEquals(set("", ".a"), extract(".a")); - assertEquals(set("a", "a."), extract("a.")); - assertEquals(set("", ".", ".."), extract("..")); - } - public void testFieldType() throws Exception { DocumentMapper docMapper = createDocumentMapper(mapping(b -> {})); FieldNamesFieldMapper fieldNamesMapper = docMapper.metadataMapper(FieldNamesFieldMapper.class); diff --git a/x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongFieldMapper.java b/x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongFieldMapper.java index 2c39719eac6bc..16409cc4e7629 100644 --- a/x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongFieldMapper.java +++ b/x-pack/plugin/mapper-unsigned-long/src/main/java/org/elasticsearch/xpack/unsignedlong/UnsignedLongFieldMapper.java @@ -488,7 +488,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } context.doc().addAll(fields); if (hasDocValues == false && (stored || indexed)) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java index c9d3b6ee3c633..55ecb869f2b7d 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java @@ -208,7 +208,7 @@ protected void index(ParseContext context, Geometry geometry) throws IOException } docValuesField.add(fields, geometry); } else if (fieldType().isSearchable()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java index d2eb7e6ef51b4..eace22a31a995 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java @@ -122,7 +122,7 @@ protected void index(ParseContext context, ParsedCartesianPoint point) throws IO if (fieldType().hasDocValues()) { context.doc().add(new XYDocValuesField(fieldType().name(), (float) point.getX(), (float) point.getY())); } else if (fieldType().isStored() || fieldType().isSearchable()) { - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } if (fieldType().isStored()) { context.doc().add(new StoredField(fieldType().name(), point.toString())); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java index 9ae24708e685e..ebb8bd5d0e119 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java @@ -126,7 +126,7 @@ public ShapeFieldMapper(String simpleName, MappedFieldType mappedFieldType, @Override protected void index(ParseContext context, Geometry geometry) throws IOException { context.doc().addAll(indexer.indexShape(geometry)); - createFieldNamesField(context); + context.addFieldExistsField(fieldType().name()); } @Override From b589ee42f4490490ac499cb1cf8948c70e81b007 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Tue, 20 Apr 2021 14:20:12 +0100 Subject: [PATCH 2/3] percolator tests --- .../percolator/PercolatorFieldMapperTests.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java index deab3b445561c..cc4925e69908b 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java @@ -287,7 +287,7 @@ public void testExtractTermsAndRanges_partial() throws Exception { ParseContext.Document document = parseContext.doc(); PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); - assertThat(document.getFields().size(), equalTo(4)); + assertThat(document.getFields().size(), equalTo(3)); assertThat(document.getFields().get(0).binaryValue().utf8ToString(), equalTo("field\u0000term")); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL)); } @@ -609,7 +609,7 @@ public void testNestedPercolatorField() throws Exception { .field("query_field", queryBuilder) .endObject().endObject()), XContentType.JSON)); - assertThat(doc.rootDoc().getFields().size(), equalTo(12)); // also includes all other meta fields + assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields IndexableField queryBuilderField = doc.rootDoc().getField("object_field.query_field.query_builder_field"); assertTrue(queryBuilderField.fieldType().omitNorms()); IndexableField extractionResultField = doc.rootDoc().getField("object_field.query_field.extraction_result"); @@ -624,7 +624,7 @@ public void testNestedPercolatorField() throws Exception { .endArray() .endObject()), XContentType.JSON)); - assertThat(doc.rootDoc().getFields().size(), equalTo(12)); // also includes all other meta fields + assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); From 95df369190f3740548d4df5a1a99a64ff876b283 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Tue, 27 Apr 2021 15:05:54 +0100 Subject: [PATCH 3/3] precommit --- .../elasticsearch/index/mapper/MatchOnlyTextFieldMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cb081ca05ee13..9b6d26abc4292 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 @@ -331,7 +331,7 @@ protected void parseCreateField(ParseContext context) throws IOException { Field field = new Field(fieldType().name(), value, fieldType); context.doc().add(field); - createFieldNamesField(context); + context.addToFieldNames(fieldType().name()); } @Override