From dbdee30a376b2dcbb938790ad99959338bfa1c8e Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Wed, 20 Apr 2022 20:22:11 -0700 Subject: [PATCH] [Type Removal] Remove TypeFieldMapper usage, remove support of `_type` in searches and from LeafFieldsLookup (#3016) Removes TypeFieldMapper and _type support from searches Signed-off-by: Suraj Singh --- .../PercolatorFieldMapperTests.java | 4 +- .../test/search/160_exists_query.yml | 13 --- .../document/DocumentActionsIT.java | 9 +- .../aggregations/metrics/TopHitsIT.java | 4 +- .../search/fields/SearchFieldsIT.java | 36 ------ .../index/mapper/DocumentMapper.java | 1 - .../index/mapper/MapperService.java | 5 - .../index/mapper/TypeFieldMapper.java | 15 +-- .../org/opensearch/indices/IndicesModule.java | 2 - .../search/lookup/LeafFieldsLookup.java | 24 +--- .../index/engine/InternalEngineTests.java | 2 +- .../opensearch/index/get/GetResultTests.java | 6 +- .../index/mapper/TypeFieldMapperTests.java | 106 ------------------ .../index/mapper/TypeFieldTypeTests.java | 66 ----------- .../index/query/TermQueryBuilderTests.java | 7 -- .../index/query/TermsQueryBuilderTests.java | 7 -- .../query/WildcardQueryBuilderTests.java | 7 -- .../indices/IndicesModuleTests.java | 2 - .../support/ValuesSourceConfigTests.java | 20 ---- .../fetch/subphase/FieldFetcherTests.java | 5 + 20 files changed, 21 insertions(+), 320 deletions(-) delete mode 100644 server/src/test/java/org/opensearch/index/mapper/TypeFieldMapperTests.java delete mode 100644 server/src/test/java/org/opensearch/index/mapper/TypeFieldTypeTests.java diff --git a/modules/percolator/src/test/java/org/opensearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/opensearch/percolator/PercolatorFieldMapperTests.java index ca6f3a78b27..fe9c486b681 100644 --- a/modules/percolator/src/test/java/org/opensearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/opensearch/percolator/PercolatorFieldMapperTests.java @@ -862,7 +862,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase { PercolatorFieldMapper.verifyQuery(rangeQuery1); PercolatorFieldMapper.verifyQuery(rangeQuery2); - HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("_type", new MatchAllQueryBuilder(), ScoreMode.None); + HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("parent", new MatchAllQueryBuilder(), ScoreMode.None); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery))); PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1))); @@ -881,7 +881,7 @@ public class PercolatorFieldMapperTests extends OpenSearchSingleNodeTestCase { expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); - HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("_type", new MatchAllQueryBuilder(), false); + HasParentQueryBuilder hasParentQuery = new HasParentQueryBuilder("parent", new MatchAllQueryBuilder(), false); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasParentQuery)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasParentQuery))); } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml index 201e456be2c..be97930d41e 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/160_exists_query.yml @@ -561,19 +561,6 @@ setup: - match: {hits.total: 4} ---- -"Test exists query on _type field": - - do: - search: - rest_total_hits_as_int: true - index: test - body: - query: - exists: - field: _type - - - match: {hits.total: 4} - --- "Test exists query on _routing field": - do: diff --git a/server/src/internalClusterTest/java/org/opensearch/document/DocumentActionsIT.java b/server/src/internalClusterTest/java/org/opensearch/document/DocumentActionsIT.java index 1e40cc14bbb..fa94d5c1c50 100644 --- a/server/src/internalClusterTest/java/org/opensearch/document/DocumentActionsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/document/DocumentActionsIT.java @@ -47,7 +47,6 @@ import org.opensearch.common.Strings; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentType; -import org.opensearch.index.mapper.MapperService; import org.opensearch.test.OpenSearchIntegTestCase; import org.opensearch.test.hamcrest.OpenSearchAssertions; @@ -58,7 +57,7 @@ import static org.opensearch.client.Requests.clearIndicesCacheRequest; import static org.opensearch.client.Requests.getRequest; import static org.opensearch.client.Requests.indexRequest; import static org.opensearch.client.Requests.refreshRequest; -import static org.opensearch.index.query.QueryBuilders.termQuery; +import static org.opensearch.index.query.QueryBuilders.matchAllQuery; import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures; import static org.hamcrest.Matchers.equalTo; @@ -181,11 +180,7 @@ public class DocumentActionsIT extends OpenSearchIntegTestCase { // check count for (int i = 0; i < 5; i++) { // test successful - SearchResponse countResponse = client().prepareSearch("test") - .setSize(0) - .setQuery(termQuery("_type", MapperService.SINGLE_MAPPING_NAME)) - .execute() - .actionGet(); + SearchResponse countResponse = client().prepareSearch("test").setSize(0).setQuery(matchAllQuery()).execute().actionGet(); assertNoFailures(countResponse); assertThat(countResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(countResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries)); diff --git a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/metrics/TopHitsIT.java b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/metrics/TopHitsIT.java index c3240c5eef7..4c5c42b773e 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/aggregations/metrics/TopHitsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/aggregations/metrics/TopHitsIT.java @@ -1386,7 +1386,7 @@ public class TopHitsIT extends OpenSearchIntegTestCase { SearchResponse response = client().prepareSearch("idx") .addRescorer(new QueryRescorerBuilder(new MatchAllQueryBuilder().boost(3.0f))) .addAggregation( - terms("terms").field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").sort(SortBuilders.fieldSort("_type"))) + terms("terms").field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").sort(SortBuilders.fieldSort("_index"))) ) .get(); Terms terms = response.getAggregations().get("terms"); @@ -1403,7 +1403,7 @@ public class TopHitsIT extends OpenSearchIntegTestCase { .addRescorer(new QueryRescorerBuilder(new MatchAllQueryBuilder().boost(3.0f))) .addAggregation( terms("terms").field(TERMS_AGGS_FIELD) - .subAggregation(topHits("hits").sort(SortBuilders.scoreSort()).sort(SortBuilders.fieldSort("_type"))) + .subAggregation(topHits("hits").sort(SortBuilders.scoreSort()).sort(SortBuilders.fieldSort("_index"))) ) .get(); Terms terms = response.getAggregations().get("terms"); diff --git a/server/src/internalClusterTest/java/org/opensearch/search/fields/SearchFieldsIT.java b/server/src/internalClusterTest/java/org/opensearch/search/fields/SearchFieldsIT.java index 25782f8dc18..941f4982af9 100644 --- a/server/src/internalClusterTest/java/org/opensearch/search/fields/SearchFieldsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/search/fields/SearchFieldsIT.java @@ -471,42 +471,6 @@ public class SearchFieldsIT extends OpenSearchIntegTestCase { assertThat(fields, equalTo(singleton("id"))); assertThat(response.getHits().getAt(i).getFields().get("id").getValue(), equalTo(Integer.toString(i))); } - - response = client().prepareSearch() - .setQuery(matchAllQuery()) - .addSort("num1", SortOrder.ASC) - .setSize(numDocs) - .addScriptField("type", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_fields._type.value", Collections.emptyMap())) - .get(); - - assertNoFailures(response); - - assertThat(response.getHits().getTotalHits().value, equalTo((long) numDocs)); - for (int i = 0; i < numDocs; i++) { - assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i))); - Set fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet()); - assertThat(fields, equalTo(singleton("type"))); - assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME)); - } - - response = client().prepareSearch() - .setQuery(matchAllQuery()) - .addSort("num1", SortOrder.ASC) - .setSize(numDocs) - .addScriptField("id", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_fields._id.value", Collections.emptyMap())) - .addScriptField("type", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_fields._type.value", Collections.emptyMap())) - .get(); - - assertNoFailures(response); - - assertThat(response.getHits().getTotalHits().value, equalTo((long) numDocs)); - for (int i = 0; i < numDocs; i++) { - assertThat(response.getHits().getAt(i).getId(), equalTo(Integer.toString(i))); - Set fields = new HashSet<>(response.getHits().getAt(i).getFields().keySet()); - assertThat(fields, equalTo(newHashSet("type", "id"))); - assertThat(response.getHits().getAt(i).getFields().get("type").getValue(), equalTo(MapperService.SINGLE_MAPPING_NAME)); - assertThat(response.getHits().getAt(i).getFields().get("id").getValue(), equalTo(Integer.toString(i))); - } } public void testScriptFieldUsingSource() throws Exception { diff --git a/server/src/main/java/org/opensearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/opensearch/index/mapper/DocumentMapper.java index 0ee0a3cb9a1..0bebfa024e1 100644 --- a/server/src/main/java/org/opensearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/DocumentMapper.java @@ -161,7 +161,6 @@ public class DocumentMapper implements ToXContentFragment { final Collection deleteTombstoneMetadataFields = Arrays.asList( VersionFieldMapper.NAME, IdFieldMapper.NAME, - TypeFieldMapper.NAME, SeqNoFieldMapper.NAME, SeqNoFieldMapper.PRIMARY_TERM_NAME, SeqNoFieldMapper.TOMBSTONE_NAME diff --git a/server/src/main/java/org/opensearch/index/mapper/MapperService.java b/server/src/main/java/org/opensearch/index/mapper/MapperService.java index a92647929ff..819df4a6f39 100644 --- a/server/src/main/java/org/opensearch/index/mapper/MapperService.java +++ b/server/src/main/java/org/opensearch/index/mapper/MapperService.java @@ -576,11 +576,6 @@ public class MapperService extends AbstractIndexComponent implements Closeable { * Given the full name of a field, returns its {@link MappedFieldType}. */ public MappedFieldType fieldType(String fullName) { - if (fullName.equals(TypeFieldMapper.NAME)) { - String type = mapper == null ? null : mapper.type(); - return new TypeFieldMapper.TypeFieldType(type); - } - return this.mapper == null ? null : this.mapper.fieldTypes().get(fullName); } diff --git a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java index 9adb1430b3d..8d3f1df6770 100644 --- a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java +++ b/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java @@ -41,7 +41,6 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; import org.opensearch.common.geo.ShapeRelation; -import org.opensearch.common.logging.DeprecationLogger; import org.opensearch.common.regex.Regex; import org.opensearch.common.time.DateMathParser; import org.opensearch.index.fielddata.IndexFieldData; @@ -55,17 +54,9 @@ import java.util.Collections; import java.util.Objects; import java.util.function.Supplier; +// Todo: Remove TypeFieldMapper once we have NestedFieldMapper implementation public class TypeFieldMapper extends MetadataFieldMapper { - private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(TypeFieldType.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."; - - public static void emitTypesDeprecationWarning() { - deprecationLogger.deprecate("query_with_types", TYPES_DEPRECATION_MESSAGE); - } - public static final String NAME = "_type"; public static final String CONTENT_TYPE = "_type"; @@ -101,7 +92,6 @@ public class TypeFieldMapper extends MetadataFieldMapper { @Override public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { - emitTypesDeprecationWarning(); return new ConstantIndexFieldData.Builder(type, name(), CoreValuesSourceType.BYTES); } @@ -112,13 +102,11 @@ public class TypeFieldMapper extends MetadataFieldMapper { @Override public Query existsQuery(QueryShardContext context) { - emitTypesDeprecationWarning(); return new MatchAllDocsQuery(); } @Override protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { - emitTypesDeprecationWarning(); if (type == null) { return false; } @@ -136,7 +124,6 @@ public class TypeFieldMapper extends MetadataFieldMapper { DateMathParser parser, QueryShardContext context ) { - emitTypesDeprecationWarning(); BytesRef lower = (BytesRef) lowerTerm; BytesRef upper = (BytesRef) upperTerm; if (includeLower) { diff --git a/server/src/main/java/org/opensearch/indices/IndicesModule.java b/server/src/main/java/org/opensearch/indices/IndicesModule.java index e685ea52aa5..9a7b91f020e 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesModule.java +++ b/server/src/main/java/org/opensearch/indices/IndicesModule.java @@ -64,7 +64,6 @@ import org.opensearch.index.mapper.RoutingFieldMapper; import org.opensearch.index.mapper.SeqNoFieldMapper; import org.opensearch.index.mapper.SourceFieldMapper; import org.opensearch.index.mapper.TextFieldMapper; -import org.opensearch.index.mapper.TypeFieldMapper; import org.opensearch.index.mapper.VersionFieldMapper; import org.opensearch.index.seqno.RetentionLeaseBackgroundSyncAction; import org.opensearch.index.seqno.RetentionLeaseSyncAction; @@ -185,7 +184,6 @@ public class IndicesModule extends AbstractModule { builtInMetadataMappers.put(IndexFieldMapper.NAME, IndexFieldMapper.PARSER); builtInMetadataMappers.put(DataStreamFieldMapper.NAME, DataStreamFieldMapper.PARSER); builtInMetadataMappers.put(SourceFieldMapper.NAME, SourceFieldMapper.PARSER); - builtInMetadataMappers.put(TypeFieldMapper.NAME, TypeFieldMapper.PARSER); builtInMetadataMappers.put(VersionFieldMapper.NAME, VersionFieldMapper.PARSER); builtInMetadataMappers.put(SeqNoFieldMapper.NAME, SeqNoFieldMapper.PARSER); // _field_names must be added last so that it has a chance to see all the other mappers diff --git a/server/src/main/java/org/opensearch/search/lookup/LeafFieldsLookup.java b/server/src/main/java/org/opensearch/search/lookup/LeafFieldsLookup.java index 14c5dade52c..62b040dfdc8 100644 --- a/server/src/main/java/org/opensearch/search/lookup/LeafFieldsLookup.java +++ b/server/src/main/java/org/opensearch/search/lookup/LeafFieldsLookup.java @@ -34,10 +34,8 @@ package org.opensearch.search.lookup; import org.apache.lucene.index.LeafReader; import org.opensearch.OpenSearchParseException; import org.opensearch.index.fieldvisitor.SingleFieldsVisitor; -import org.opensearch.index.mapper.DocumentMapper; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.index.mapper.MapperService; -import org.opensearch.index.mapper.TypeFieldMapper; import java.io.IOException; import java.util.ArrayList; @@ -147,22 +145,12 @@ public class LeafFieldsLookup implements Map { cachedFieldData.put(name, data); } if (data.fields() == null) { - List values; - if (TypeFieldMapper.NAME.equals(data.fieldType().name())) { - TypeFieldMapper.emitTypesDeprecationWarning(); - 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 OpenSearchParseException("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 OpenSearchParseException("failed to load field [{}]", e, name); } data.fields(singletonMap(data.fieldType().name(), values)); } diff --git a/server/src/test/java/org/opensearch/index/engine/InternalEngineTests.java b/server/src/test/java/org/opensearch/index/engine/InternalEngineTests.java index c33adf3bcb5..cbae55a047a 100644 --- a/server/src/test/java/org/opensearch/index/engine/InternalEngineTests.java +++ b/server/src/test/java/org/opensearch/index/engine/InternalEngineTests.java @@ -381,7 +381,7 @@ public class InternalEngineTests extends EngineTestCase { } public void testSegmentsWithIndexSort() throws Exception { - Sort indexSort = new Sort(new SortedSetSortField("_type", false)); + Sort indexSort = new Sort(new SortedSetSortField("field", false)); try ( Store store = createStore(); Engine engine = createEngine(defaultSettings, store, createTempDir(), NoMergePolicy.INSTANCE, null, null, null, indexSort, null) diff --git a/server/src/test/java/org/opensearch/index/get/GetResultTests.java b/server/src/test/java/org/opensearch/index/get/GetResultTests.java index 9519b83fa54..03621f83e8a 100644 --- a/server/src/test/java/org/opensearch/index/get/GetResultTests.java +++ b/server/src/test/java/org/opensearch/index/get/GetResultTests.java @@ -46,7 +46,6 @@ import org.opensearch.index.mapper.IdFieldMapper; import org.opensearch.index.mapper.IndexFieldMapper; import org.opensearch.index.mapper.SeqNoFieldMapper; import org.opensearch.index.mapper.SourceFieldMapper; -import org.opensearch.index.mapper.TypeFieldMapper; import org.opensearch.index.mapper.VersionFieldMapper; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.test.RandomObjects; @@ -372,9 +371,8 @@ public class GetResultTests extends OpenSearchTestCase { Map fields = new HashMap<>(numFields); Map expectedFields = new HashMap<>(numFields); // As we are using this to construct a GetResult object that already contains - // index, type, id, version, seqNo, and source fields, we need to exclude them from random fields - Predicate excludeMetaFieldFilter = field -> field.equals(TypeFieldMapper.NAME) - || field.equals(IndexFieldMapper.NAME) + // index, id, version, seqNo, and source fields, we need to exclude them from random fields + Predicate excludeMetaFieldFilter = field -> field.equals(IndexFieldMapper.NAME) || field.equals(IdFieldMapper.NAME) || field.equals(VersionFieldMapper.NAME) || field.equals(SourceFieldMapper.NAME) diff --git a/server/src/test/java/org/opensearch/index/mapper/TypeFieldMapperTests.java b/server/src/test/java/org/opensearch/index/mapper/TypeFieldMapperTests.java deleted file mode 100644 index 89eee655ca9..00000000000 --- a/server/src/test/java/org/opensearch/index/mapper/TypeFieldMapperTests.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * 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. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.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.opensearch.common.bytes.BytesArray; -import org.opensearch.common.compress.CompressedXContent; -import org.opensearch.common.settings.Settings; -import org.opensearch.common.xcontent.XContentType; -import org.opensearch.index.IndexService; -import org.opensearch.index.fielddata.IndexFieldDataCache; -import org.opensearch.index.fielddata.IndexOrdinalsFieldData; -import org.opensearch.index.fielddata.LeafOrdinalsFieldData; -import org.opensearch.index.mapper.MapperService.MergeReason; -import org.opensearch.indices.breaker.NoneCircuitBreakerService; -import org.opensearch.plugins.Plugin; -import org.opensearch.test.OpenSearchSingleNodeTestCase; -import org.opensearch.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 OpenSearchSingleNodeTestCase { - - @Override - protected Collection> getPlugins() { - return pluginList(InternalSettingsPlugin.class); - } - - public void testDocValuesSingleType() throws Exception { - testDocValues(this::createIndex); - assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead."); - } - - 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", - () -> { throw new UnsupportedOperationException(); } - ).build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService()); - LeafOrdinalsFieldData 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/opensearch/index/mapper/TypeFieldTypeTests.java b/server/src/test/java/org/opensearch/index/mapper/TypeFieldTypeTests.java deleted file mode 100644 index 66377a16b90..00000000000 --- a/server/src/test/java/org/opensearch/index/mapper/TypeFieldTypeTests.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * 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. - */ -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.index.mapper; - -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.MatchNoDocsQuery; -import org.apache.lucene.search.Query; -import org.opensearch.index.query.QueryShardContext; -import org.opensearch.test.OpenSearchTestCase; -import org.mockito.Mockito; - -import java.util.Arrays; - -public class TypeFieldTypeTests extends OpenSearchTestCase { - - public void testTermsQuery() { - QueryShardContext context = Mockito.mock(QueryShardContext.class); - - TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType("_doc"); - Query query = ft.termQuery("my_type", context); - assertEquals(new MatchNoDocsQuery(), query); - - query = ft.termQuery("_doc", context); - assertEquals(new MatchAllDocsQuery(), query); - - query = ft.termsQuery(Arrays.asList("_doc", "type", "foo"), context); - assertEquals(new MatchAllDocsQuery(), query); - - query = ft.termsQuery(Arrays.asList("type", "foo"), context); - assertEquals(new MatchNoDocsQuery(), query); - - query = ft.termQueryCaseInsensitive("_DOC", context); - assertEquals(new MatchAllDocsQuery(), query); - - assertWarnings("[types removal] Using the _type field in queries and aggregations is deprecated, prefer to use a field instead."); - } -} diff --git a/server/src/test/java/org/opensearch/index/query/TermQueryBuilderTests.java b/server/src/test/java/org/opensearch/index/query/TermQueryBuilderTests.java index 9cac88a256a..cc877c7590c 100644 --- a/server/src/test/java/org/opensearch/index/query/TermQueryBuilderTests.java +++ b/server/src/test/java/org/opensearch/index/query/TermQueryBuilderTests.java @@ -41,7 +41,6 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.opensearch.common.ParsingException; import org.opensearch.index.mapper.MappedFieldType; -import org.opensearch.index.mapper.TypeFieldMapper; import java.io.IOException; @@ -198,12 +197,6 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase 42L, null); - - ValuesSourceConfig config = ValuesSourceConfig.resolve( - context, - null, - TypeFieldMapper.NAME, - null, - null, - null, - null, - CoreValuesSourceType.BYTES - ); - assertWarnings(TypeFieldMapper.TYPES_DEPRECATION_MESSAGE); - } - } - public void testFieldAlias() throws Exception { IndexService indexService = createIndex("index", Settings.EMPTY, "type", "field", "type=keyword", "alias", "type=alias,path=field"); client().prepareIndex("index").setId("1").setSource("field", "value").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); diff --git a/server/src/test/java/org/opensearch/search/fetch/subphase/FieldFetcherTests.java b/server/src/test/java/org/opensearch/search/fetch/subphase/FieldFetcherTests.java index 8147d1afb8c..0aff1efff88 100644 --- a/server/src/test/java/org/opensearch/search/fetch/subphase/FieldFetcherTests.java +++ b/server/src/test/java/org/opensearch/search/fetch/subphase/FieldFetcherTests.java @@ -117,6 +117,11 @@ public class FieldFetcherTests extends OpenSearchSingleNodeTestCase { Map fields = fetchFields(mapperService, source, "_routing"); assertTrue(fields.isEmpty()); + + // The _type field was deprecated in 7.x and is not supported in 2.0. So the behavior + // should be the same as if the field didn't exist. + fields = fetchFields(mapperService, source, "_type"); + assertTrue(fields.isEmpty()); } public void testFetchAllFields() throws IOException {