From f9d40e47ff199e3fca04702454e6c225cf451327 Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 16 Mar 2020 15:23:30 +0000 Subject: [PATCH 1/2] Remove TypeFieldMapper --- .../index/mapper/DocumentMapper.java | 2 +- .../index/mapper/MappedFieldType.java | 4 - .../index/mapper/TypeFieldMapper.java | 260 ------------------ .../index/mapper/TypeFieldType.java | 44 +++ .../index/query/QueryShardContext.java | 6 +- .../elasticsearch/indices/IndicesModule.java | 2 - .../search/lookup/LeafFieldsLookup.java | 24 +- .../index/get/DocumentFieldTests.java | 4 +- .../index/mapper/NestedObjectMapperTests.java | 2 +- .../index/mapper/TypeFieldMapperTests.java | 90 ------ .../index/mapper/TypeFieldTypeTests.java | 45 --- .../indices/IndicesModuleTests.java | 3 +- .../support/ValuesSourceConfigTests.java | 4 +- 13 files changed, 61 insertions(+), 429 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java create mode 100644 server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java delete mode 100644 server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java delete mode 100644 server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index db9b4306f2f5c..8f4f8c9c98675 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -184,7 +184,7 @@ public DocumentMapper(MapperService mapperService, Mapping mapping) { } final Collection deleteTombstoneMetadataFields = Arrays.asList(VersionFieldMapper.NAME, IdFieldMapper.NAME, - TypeFieldMapper.NAME, SeqNoFieldMapper.NAME, SeqNoFieldMapper.PRIMARY_TERM_NAME, SeqNoFieldMapper.TOMBSTONE_NAME); + SeqNoFieldMapper.NAME, SeqNoFieldMapper.PRIMARY_TERM_NAME, SeqNoFieldMapper.TOMBSTONE_NAME); this.deleteTombstoneMetadataFieldMappers = Stream.of(mapping.metadataMappers) .filter(field -> deleteTombstoneMetadataFields.contains(field.name())).toArray(MetadataFieldMapper[]::new); final Collection noopTombstoneMetadataFields = Arrays.asList( diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 67bbdaf79ce76..110632e875d2b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -488,10 +488,6 @@ public static Term extractTerm(Query termQuery) { while (termQuery instanceof BoostQuery) { termQuery = ((BoostQuery) termQuery).getQuery(); } - if (termQuery instanceof TypeFieldMapper.TypesQuery) { - assert ((TypeFieldMapper.TypesQuery) termQuery).getTerms().length == 1; - return new Term(TypeFieldMapper.NAME, ((TypeFieldMapper.TypesQuery) termQuery).getTerms()[0]); - } if (termQuery instanceof TermInSetQuery) { TermInSetQuery tisQuery = (TermInSetQuery) termQuery; PrefixCodedTerms terms = tisQuery.getTermData(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java deleted file mode 100644 index d32d4fa9d3f11..0000000000000 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java +++ /dev/null @@ -1,260 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -import org.apache.lucene.document.Field; -import org.apache.lucene.document.SortedSetDocValuesField; -import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexableField; -import org.apache.lucene.index.Term; -import org.apache.lucene.index.TermStates; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermInSetQuery; -import org.apache.lucene.search.TermQuery; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.index.IndexSettings; -import org.elasticsearch.index.fielddata.IndexFieldData; -import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; -import org.elasticsearch.index.query.QueryShardContext; -import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; -import org.elasticsearch.search.aggregations.support.ValuesSourceType; - -import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; - -public class TypeFieldMapper extends MetadataFieldMapper { - - public static final String NAME = "_type"; - - public static final String CONTENT_TYPE = "_type"; - - public static class Defaults { - public static final String NAME = TypeFieldMapper.NAME; - - public static final MappedFieldType FIELD_TYPE = new TypeFieldType(); - - static { - FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); - FIELD_TYPE.setTokenized(false); - FIELD_TYPE.setStored(false); - FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); - FIELD_TYPE.freeze(); - } - } - - public static class TypeParser implements MetadataFieldMapper.TypeParser { - @Override - public MetadataFieldMapper.Builder parse(String name, Map node, - ParserContext parserContext) throws MapperParsingException { - throw new MapperParsingException(NAME + " is not configurable"); - } - - @Override - public MetadataFieldMapper getDefault(ParserContext context) { - final IndexSettings indexSettings = context.mapperService().getIndexSettings(); - return new TypeFieldMapper(indexSettings, defaultFieldType(indexSettings)); - } - } - - public static final class TypeFieldType extends ConstantFieldType { - - TypeFieldType() { - } - - protected TypeFieldType(TypeFieldType ref) { - super(ref); - } - - @Override - public MappedFieldType clone() { - return new TypeFieldType(this); - } - - @Override - public String typeName() { - return CONTENT_TYPE; - } - - @Override - public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { - Function typeFunction = mapperService -> mapperService.documentMapper().type(); - return new ConstantIndexFieldData.Builder(typeFunction); - } - - @Override - public ValuesSourceType getValuesSourceType() { - return CoreValuesSourceType.BYTES; - } - - @Override - protected boolean matches(String pattern, QueryShardContext context) { - return pattern.equals(MapperService.SINGLE_MAPPING_NAME); - } - } - - /** - * Specialization for a disjunction over many _type - */ - public static class TypesQuery extends Query { - // Same threshold as TermInSetQuery - private static final int BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD = 16; - - private final BytesRef[] types; - - public TypesQuery(BytesRef... types) { - if (types == null) { - throw new NullPointerException("types cannot be null."); - } - if (types.length == 0) { - throw new IllegalArgumentException("types must contains at least one value."); - } - this.types = types; - } - - public BytesRef[] getTerms() { - return types; - } - - @Override - public Query rewrite(IndexReader reader) throws IOException { - final int threshold = Math.min(BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD, BooleanQuery.getMaxClauseCount()); - if (types.length <= threshold) { - Set uniqueTypes = new HashSet<>(); - BooleanQuery.Builder bq = new BooleanQuery.Builder(); - int totalDocFreq = 0; - for (BytesRef type : types) { - if (uniqueTypes.add(type)) { - Term term = new Term(CONTENT_TYPE, type); - TermStates context = TermStates.build(reader.getContext(), term, true); - if (context.docFreq() == 0) { - // this _type is not present in the reader - continue; - } - totalDocFreq += context.docFreq(); - // strict equality should be enough ? - if (totalDocFreq >= reader.maxDoc()) { - assert totalDocFreq == reader.maxDoc(); - // Matches all docs since _type is a single value field - // Using a match_all query will help Lucene perform some optimizations - // For instance, match_all queries as filter clauses are automatically removed - return new MatchAllDocsQuery(); - } - bq.add(new TermQuery(term, context), BooleanClause.Occur.SHOULD); - } - } - return new ConstantScoreQuery(bq.build()); - } - return new TermInSetQuery(CONTENT_TYPE, types); - } - - @Override - public boolean equals(Object obj) { - if (sameClassAs(obj) == false) { - return false; - } - TypesQuery that = (TypesQuery) obj; - return Arrays.equals(types, that.types); - } - - @Override - public int hashCode() { - return 31 * classHash() + Arrays.hashCode(types); - } - - @Override - public String toString(String field) { - StringBuilder builder = new StringBuilder(); - for (BytesRef type : types) { - if (builder.length() > 0) { - builder.append(' '); - } - builder.append(new Term(CONTENT_TYPE, type).toString()); - } - return builder.toString(); - } - } - - private TypeFieldMapper(IndexSettings indexSettings, MappedFieldType existing) { - this(existing == null ? defaultFieldType(indexSettings) : existing.clone(), - indexSettings); - } - - private TypeFieldMapper(MappedFieldType fieldType, IndexSettings indexSettings) { - super(NAME, fieldType, defaultFieldType(indexSettings), indexSettings.getSettings()); - } - - private static MappedFieldType defaultFieldType(IndexSettings indexSettings) { - MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone(); - defaultFieldType.setIndexOptions(IndexOptions.NONE); - defaultFieldType.setHasDocValues(false); - return defaultFieldType; - } - - @Override - public void preParse(ParseContext context) throws IOException { - super.parse(context); - } - - @Override - public void parse(ParseContext context) throws IOException { - // we parse in pre parse - } - - @Override - protected void parseCreateField(ParseContext context, List fields) throws IOException { - if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored()) { - return; - } - fields.add(new Field(fieldType().name(), MapperService.SINGLE_MAPPING_NAME, fieldType())); - if (fieldType().hasDocValues()) { - fields.add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(MapperService.SINGLE_MAPPING_NAME))); - } - } - - @Override - protected String contentType() { - return CONTENT_TYPE; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder; - } - - @Override - protected void doMerge(Mapper mergeWith) { - // do nothing here, no merging, but also no exception - } -} diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java new file mode 100644 index 0000000000000..396cdef0ca58a --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java @@ -0,0 +1,44 @@ +package org.elasticsearch.index.mapper; + +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; +import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; +import org.elasticsearch.search.aggregations.support.ValuesSourceType; + +@Deprecated +public final class TypeFieldType extends ConstantFieldType { + + public static final String NAME = "_type"; + + public static final TypeFieldType INSTANCE = new TypeFieldType(); + + private TypeFieldType() { + freeze(); + } + + @Override + public MappedFieldType clone() { + return this; + } + + @Override + public String typeName() { + return NAME; + } + + @Override + public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { + return new ConstantIndexFieldData.Builder(s -> MapperService.SINGLE_MAPPING_NAME); + } + + @Override + public ValuesSourceType getValuesSourceType() { + return CoreValuesSourceType.BYTES; + } + + @Override + protected boolean matches(String pattern, QueryShardContext context) { + return pattern.equals(MapperService.SINGLE_MAPPING_NAME); + } +} diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 611a6fbe6d509..c6b42710ec843 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -49,7 +49,7 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; +import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.index.query.support.NestedScope; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.Script; @@ -226,8 +226,10 @@ public Set simpleMatchToIndexNames(String pattern) { } public MappedFieldType fieldMapper(String name) { - if (name.equals(TypeFieldMapper.NAME)) { + // TODO wrap this with v7 compatibility + if (name.equals(TypeFieldType.NAME)) { deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE); + return TypeFieldType.INSTANCE; } return failIfFieldMappingNotFound(name, mapperService.fieldType(name)); } diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesModule.java b/server/src/main/java/org/elasticsearch/indices/IndicesModule.java index ea880110d52c1..aa3f9d63e8517 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesModule.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesModule.java @@ -55,7 +55,6 @@ import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.index.seqno.RetentionLeaseBackgroundSyncAction; import org.elasticsearch.index.seqno.RetentionLeaseSyncAction; @@ -156,7 +155,6 @@ private static Map initBuiltInMetadataMa builtInMetadataMappers.put(RoutingFieldMapper.NAME, new RoutingFieldMapper.TypeParser()); builtInMetadataMappers.put(IndexFieldMapper.NAME, new IndexFieldMapper.TypeParser()); builtInMetadataMappers.put(SourceFieldMapper.NAME, new SourceFieldMapper.TypeParser()); - builtInMetadataMappers.put(TypeFieldMapper.NAME, new TypeFieldMapper.TypeParser()); builtInMetadataMappers.put(NestedPathFieldMapper.NAME, new NestedPathFieldMapper.TypeParser()); builtInMetadataMappers.put(VersionFieldMapper.NAME, new VersionFieldMapper.TypeParser()); builtInMetadataMappers.put(SeqNoFieldMapper.NAME, new SeqNoFieldMapper.TypeParser()); diff --git a/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java b/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java index c64d0b9445b69..901c406aadb8d 100644 --- a/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java +++ b/server/src/main/java/org/elasticsearch/search/lookup/LeafFieldsLookup.java @@ -21,10 +21,8 @@ import org.apache.lucene.index.LeafReader; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.index.fieldvisitor.SingleFieldsVisitor; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.TypeFieldMapper; import java.io.IOException; import java.util.ArrayList; @@ -137,22 +135,14 @@ private FieldLookup loadFieldData(String name) { cachedFieldData.put(name, data); } if (data.fields() == null) { - List values; - if (TypeFieldMapper.NAME.equals(data.fieldType().name())) { - values = new ArrayList<>(1); - final DocumentMapper mapper = mapperService.documentMapper(); - if (mapper != null) { - values.add(mapper.type()); - } - } else { - values = new ArrayList<>(2); - SingleFieldsVisitor visitor = new SingleFieldsVisitor(data.fieldType(), values); - try { - reader.document(docId, visitor); - } catch (IOException e) { - throw new ElasticsearchParseException("failed to load field [{}]", e, name); - } + List values = new ArrayList<>(2); + SingleFieldsVisitor visitor = new SingleFieldsVisitor(data.fieldType(), values); + try { + reader.document(docId, visitor); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to load field [{}]", e, name); } + data.fields(singletonMap(data.fieldType().name(), values)); } return data; diff --git a/server/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java b/server/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java index e2e3d4df67cf7..ccd5f66405803 100644 --- a/server/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java +++ b/server/src/test/java/org/elasticsearch/index/get/DocumentFieldTests.java @@ -30,7 +30,6 @@ import org.elasticsearch.index.mapper.IgnoredFieldMapper; import org.elasticsearch.index.mapper.IndexFieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RandomObjects; @@ -102,8 +101,7 @@ private static DocumentField mutateDocumentField(DocumentField documentField) { public static Tuple randomDocumentField(XContentType xContentType) { if (randomBoolean()) { - String metaField = randomValueOtherThanMany(field -> field.equals(TypeFieldMapper.NAME) - || field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME), + String metaField = randomValueOtherThanMany(field -> field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME), () -> randomFrom(MapperService.getAllMetaFields())); DocumentField documentField; if (metaField.equals(IgnoredFieldMapper.NAME)) { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java index 93b235f5e0bfe..39ad6b0471dba 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java @@ -732,7 +732,7 @@ public void testReorderParent() throws IOException { assertThat(doc.docs().size(), equalTo(3)); if (Version.indexCreated(settings).before(Version.V_8_0_0)) { - assertThat(doc.docs().get(0).get(TypeFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); + assertThat(doc.docs().get(0).get(TypeFieldType.NAME), equalTo(nested1Mapper.nestedTypePath())); } else { assertThat(doc.docs().get(0).get(NestedPathFieldMapper.NAME), equalTo(nested1Mapper.nestedTypePath())); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java deleted file mode 100644 index 39366b5bc4d14..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldMapperTests.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexableField; -import org.apache.lucene.index.SortedSetDocValues; -import org.apache.lucene.store.Directory; -import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData; -import org.elasticsearch.index.fielddata.IndexFieldDataCache; -import org.elasticsearch.index.fielddata.IndexOrdinalsFieldData; -import org.elasticsearch.index.mapper.MapperService.MergeReason; -import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; -import org.elasticsearch.test.InternalSettingsPlugin; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.function.Function; - -public class TypeFieldMapperTests extends ESSingleNodeTestCase { - - @Override - protected Collection> getPlugins() { - return pluginList(InternalSettingsPlugin.class); - } - - public void testDocValuesSingleType() throws Exception { - testDocValues(this::createIndex); - } - - public static void testDocValues(Function createIndex) throws IOException { - MapperService mapperService = createIndex.apply("test").mapperService(); - DocumentMapper mapper = mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE); - ParsedDocument document = mapper.parse(new SourceToParse("index", "id", new BytesArray("{}"), XContentType.JSON)); - - Directory dir = newDirectory(); - IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); - w.addDocument(document.rootDoc()); - DirectoryReader r = DirectoryReader.open(w); - w.close(); - - MappedFieldType ft = mapperService.fieldType(TypeFieldMapper.NAME); - IndexOrdinalsFieldData fd = (IndexOrdinalsFieldData) ft.fielddataBuilder("test").build(mapperService.getIndexSettings(), - ft, new IndexFieldDataCache.None(), new NoneCircuitBreakerService(), mapperService); - AtomicOrdinalsFieldData afd = fd.load(r.leaves().get(0)); - SortedSetDocValues values = afd.getOrdinalsValues(); - assertTrue(values.advanceExact(0)); - assertEquals(0, values.nextOrd()); - assertEquals(SortedSetDocValues.NO_MORE_ORDS, values.nextOrd()); - assertEquals(new BytesRef("type"), values.lookupOrd(0)); - r.close(); - dir.close(); - } - - public void testDefaults() throws IOException { - Settings indexSettings = Settings.EMPTY; - MapperService mapperService = createIndex("test", indexSettings).mapperService(); - DocumentMapper mapper = mapperService.merge("type", new CompressedXContent("{\"type\":{}}"), MergeReason.MAPPING_UPDATE); - ParsedDocument document = mapper.parse(new SourceToParse("index", "id", new BytesArray("{}"), XContentType.JSON)); - assertEquals(Collections.emptyList(), Arrays.asList(document.rootDoc().getFields(TypeFieldMapper.NAME))); - } -} diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java deleted file mode 100644 index 0e00bf6677c8f..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.mapper; - -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.MatchNoDocsQuery; -import org.apache.lucene.search.Query; -import org.elasticsearch.index.query.QueryShardContext; -import org.mockito.Mockito; - -public class TypeFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new TypeFieldMapper.TypeFieldType(); - } - - public void testTermsQuery() throws Exception { - QueryShardContext context = Mockito.mock(QueryShardContext.class); - - TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType(); - ft.setName(TypeFieldMapper.NAME); - - Query query = ft.termQuery("_doc", context); - assertEquals(new MatchAllDocsQuery(), query); - - query = ft.termQuery("other_type", context); - assertEquals(new MatchNoDocsQuery(), query); - } -} diff --git a/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java b/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java index 48d710e2ac62b..65b9d8aaae3e2 100644 --- a/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java +++ b/server/src/test/java/org/elasticsearch/indices/IndicesModuleTests.java @@ -32,7 +32,6 @@ import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldMapper; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.indices.mapper.MapperRegistry; import org.elasticsearch.plugins.MapperPlugin; @@ -86,7 +85,7 @@ public Map getMetadataMappers() { }); private static final String[] EXPECTED_METADATA_FIELDS = new String[]{ IgnoredFieldMapper.NAME, IdFieldMapper.NAME, - RoutingFieldMapper.NAME, IndexFieldMapper.NAME, SourceFieldMapper.NAME, TypeFieldMapper.NAME, + RoutingFieldMapper.NAME, IndexFieldMapper.NAME, SourceFieldMapper.NAME, NestedPathFieldMapper.NAME, VersionFieldMapper.NAME, SeqNoFieldMapper.NAME, FieldNamesFieldMapper.NAME }; public void testBuiltinMappers() { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java index db208044df1a4..222745368687d 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfigTests.java @@ -27,7 +27,7 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.SortedBinaryDocValues; -import org.elasticsearch.index.mapper.TypeFieldMapper; +import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.test.ESSingleNodeTestCase; @@ -266,7 +266,7 @@ public void testTypeFieldDeprecation() { QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); ValuesSourceConfig config = ValuesSourceConfig.resolve( - context, null, TypeFieldMapper.NAME, null, null, null, null); + context, null, TypeFieldType.NAME, null, null, null, null); assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); } } From a7590c2660ebd15946ec7a6389e7d85ef76c8ada Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 16 Mar 2020 17:42:49 +0000 Subject: [PATCH 2/2] Push _type check into MapperService; remove _type field docs --- docs/reference/mapping/fields.asciidoc | 9 +-- .../mapping/fields/type-field.asciidoc | 66 ------------------- .../index/mapper/MapperService.java | 12 ++++ .../index/mapper/TypeFieldType.java | 19 ++++++ .../index/query/QueryShardContext.java | 12 ---- 5 files changed, 32 insertions(+), 86 deletions(-) delete mode 100644 docs/reference/mapping/fields/type-field.asciidoc diff --git a/docs/reference/mapping/fields.asciidoc b/docs/reference/mapping/fields.asciidoc index f6d5f00a9b5e0..fc8d8eacb8018 100644 --- a/docs/reference/mapping/fields.asciidoc +++ b/docs/reference/mapping/fields.asciidoc @@ -1,8 +1,7 @@ [[mapping-fields]] == Meta-Fields -Each document has metadata associated with it, such as the `_index`, mapping -<>, and `_id` meta-fields. The behaviour of some of these meta-fields +Each document has metadata associated with it, such as the `_index` and `_id` meta-fields. The behaviour of some of these meta-fields can be customised when a mapping type is created. [float] @@ -13,10 +12,6 @@ can be customised when a mapping type is created. The index to which the document belongs. -<>:: - - The document's <>. - <>:: The document's ID. @@ -74,5 +69,3 @@ include::fields/routing-field.asciidoc[] include::fields/source-field.asciidoc[] -include::fields/type-field.asciidoc[] - diff --git a/docs/reference/mapping/fields/type-field.asciidoc b/docs/reference/mapping/fields/type-field.asciidoc deleted file mode 100644 index 2c5dc7195d643..0000000000000 --- a/docs/reference/mapping/fields/type-field.asciidoc +++ /dev/null @@ -1,66 +0,0 @@ -[[mapping-type-field]] -=== `_type` field - -deprecated[6.0.0,See <>] - -Each document indexed is associated with a <> (see -<>) and an <>. The `_type` field is -indexed in order to make searching by type name fast. - -The value of the `_type` field is accessible in queries, aggregations, -scripts, and when sorting: - -[source,console] --------------------------- -# Example documents - -PUT my_index/_doc/1?refresh=true -{ - "text": "Document with type 'doc'" -} --------------------------- -// TESTSETUP - -[source,console] --------------------------- -GET my_index/_search -{ - "query": { - "term": { - "_type": "_doc" <1> - } - }, - "aggs": { - "types": { - "terms": { - "field": "_type", <2> - "size": 10 - } - } - }, - "sort": [ - { - "_type": { <3> - "order": "desc" - } - } - ], - "script_fields": { - "type": { - "script": { - "lang": "painless", - "source": "doc['_type']" <4> - } - } - } -} - --------------------------- -// TEST[warning:[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead.] -// TEST[warning:[types removal] Looking up doc types [_type] in scripts is deprecated.] - -<1> Querying on the `_type` field -<2> Aggregating on the `_type` field -<3> Sorting on the `_type` field -<4> Accessing the `_type` field in scripts - diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 1ae43c9c1e7a7..f73e15cf89545 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import com.carrotsearch.hppc.ObjectHashSet; +import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.DelegatingAnalyzerWrapper; @@ -28,6 +29,7 @@ import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; @@ -74,6 +76,11 @@ public class MapperService extends AbstractIndexComponent implements Closeable { + private static final DeprecationLogger deprecationLogger = new DeprecationLogger( + LogManager.getLogger(QueryShardContext.class)); + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using the _type field " + + "in queries and aggregations is deprecated, prefer to use a field instead."; + /** * The reason why a mapping is being merged. */ @@ -587,6 +594,11 @@ public DocumentMapperForType documentMapperWithAutoCreate() { * Given the full name of a field, returns its {@link MappedFieldType}. */ public MappedFieldType fieldType(String fullName) { + // TODO wrap this with v7 compatibility + if (fullName.equals(TypeFieldType.NAME)) { + deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE); + return TypeFieldType.INSTANCE; + } return fieldTypes.get(fullName); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java index 396cdef0ca58a..868508920262f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldType.java @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.elasticsearch.index.mapper; import org.elasticsearch.index.fielddata.IndexFieldData; diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index c6b42710ec843..44e7f57f1c168 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.query; -import org.apache.logging.log4j.LogManager; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.IndexSearcher; @@ -33,7 +32,6 @@ import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -49,7 +47,6 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.TypeFieldType; import org.elasticsearch.index.query.support.NestedScope; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.Script; @@ -74,10 +71,6 @@ * Context object used to create lucene queries on the shard level. */ public class QueryShardContext extends QueryRewriteContext { - private static final DeprecationLogger deprecationLogger = new DeprecationLogger( - LogManager.getLogger(QueryShardContext.class)); - public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using the _type field " + - "in queries and aggregations is deprecated, prefer to use a field instead."; private final ScriptService scriptService; private final IndexSettings indexSettings; @@ -226,11 +219,6 @@ public Set simpleMatchToIndexNames(String pattern) { } public MappedFieldType fieldMapper(String name) { - // TODO wrap this with v7 compatibility - if (name.equals(TypeFieldType.NAME)) { - deprecationLogger.deprecatedAndMaybeLog("query_with_types", TYPES_DEPRECATION_MESSAGE); - return TypeFieldType.INSTANCE; - } return failIfFieldMappingNotFound(name, mapperService.fieldType(name)); }