diff --git a/server/src/main/java/org/opensearch/common/lucene/search/Queries.java b/server/src/main/java/org/opensearch/common/lucene/search/Queries.java index 2f500efba9e8e..ac77420a79f73 100644 --- a/server/src/main/java/org/opensearch/common/lucene/search/Queries.java +++ b/server/src/main/java/org/opensearch/common/lucene/search/Queries.java @@ -33,7 +33,6 @@ package org.opensearch.common.lucene.search; import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.index.Term; import org.apache.lucene.queries.ExtendedCommonTermsQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause.Occur; @@ -43,17 +42,14 @@ import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery; -import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryVisitor; import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Weight; -import org.apache.lucene.util.BytesRef; import org.opensearch.OpenSearchException; import org.opensearch.common.Nullable; import org.opensearch.index.mapper.SeqNoFieldMapper; -import org.opensearch.index.mapper.TypeFieldMapper; import java.io.IOException; import java.util.Collection; @@ -87,7 +83,7 @@ public static Query newLenientFieldQuery(String field, RuntimeException e) { } public static Query newNestedFilter() { - return new PrefixQuery(new Term(TypeFieldMapper.NAME, new BytesRef("__"))); + return not(newNonNestedFilter()); } /** diff --git a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java deleted file mode 100644 index 8d3f1df677040..0000000000000 --- a/server/src/main/java/org/opensearch/index/mapper/TypeFieldMapper.java +++ /dev/null @@ -1,187 +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.document.Field; -import org.apache.lucene.document.FieldType; -import org.apache.lucene.document.SortedSetDocValuesField; -import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.search.MatchAllDocsQuery; -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.regex.Regex; -import org.opensearch.common.time.DateMathParser; -import org.opensearch.index.fielddata.IndexFieldData; -import org.opensearch.index.fielddata.plain.ConstantIndexFieldData; -import org.opensearch.index.query.QueryShardContext; -import org.opensearch.search.aggregations.support.CoreValuesSourceType; -import org.opensearch.search.lookup.SearchLookup; - -import java.time.ZoneId; -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 { - - public static final String NAME = "_type"; - - public static final String CONTENT_TYPE = "_type"; - - public static final TypeParser PARSER = new FixedTypeParser(c -> new TypeFieldMapper()); - - public static class Defaults { - - public static final FieldType NESTED_FIELD_TYPE = new FieldType(); - - static { - NESTED_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); - NESTED_FIELD_TYPE.setTokenized(false); - NESTED_FIELD_TYPE.setStored(false); - NESTED_FIELD_TYPE.setOmitNorms(true); - NESTED_FIELD_TYPE.freeze(); - } - } - - public static final class TypeFieldType extends ConstantFieldType { - - private final String type; - - public TypeFieldType(String type) { - super(NAME, Collections.emptyMap()); - this.type = type; - } - - @Override - public String typeName() { - return CONTENT_TYPE; - } - - @Override - public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier searchLookup) { - return new ConstantIndexFieldData.Builder(type, name(), CoreValuesSourceType.BYTES); - } - - @Override - public ValueFetcher valueFetcher(QueryShardContext context, SearchLookup lookup, String format) { - throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); - } - - @Override - public Query existsQuery(QueryShardContext context) { - return new MatchAllDocsQuery(); - } - - @Override - protected boolean matches(String pattern, boolean caseInsensitive, QueryShardContext context) { - if (type == null) { - return false; - } - return Regex.simpleMatch(pattern, type, caseInsensitive); - } - - @Override - public Query rangeQuery( - Object lowerTerm, - Object upperTerm, - boolean includeLower, - boolean includeUpper, - ShapeRelation relation, - ZoneId timeZone, - DateMathParser parser, - QueryShardContext context - ) { - BytesRef lower = (BytesRef) lowerTerm; - BytesRef upper = (BytesRef) upperTerm; - if (includeLower) { - if (lower.utf8ToString().compareTo(type) > 0) { - return new MatchNoDocsQuery(); - } - } else { - if (lower.utf8ToString().compareTo(type) >= 0) { - return new MatchNoDocsQuery(); - } - } - if (includeUpper) { - if (upper.utf8ToString().compareTo(type) < 0) { - return new MatchNoDocsQuery(); - } - } else { - if (upper.utf8ToString().compareTo(type) <= 0) { - return new MatchNoDocsQuery(); - } - } - return new MatchAllDocsQuery(); - } - - /** - * Build a type filter - * - * This does not emit a deprecation warning, as it is only called when a type - * has been specified in a REST request and warnings will have already been - * emitted at the REST layer. - */ - public Query typeFilter(String[] types) { - for (String t : types) { - if (Objects.equals(this.type, t)) { - return new MatchAllDocsQuery(); - } - } - return new MatchNoDocsQuery(); - } - } - - private TypeFieldMapper() { - super(new TypeFieldType(null)); - } - - @Override - public void preParse(ParseContext context) { - if (fieldType.indexOptions() == IndexOptions.NONE && !fieldType.stored()) { - return; - } - context.doc().add(new Field(fieldType().name(), MapperService.SINGLE_MAPPING_NAME, fieldType)); - if (fieldType().hasDocValues()) { - context.doc().add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(MapperService.SINGLE_MAPPING_NAME))); - } - } - - @Override - protected String contentType() { - return CONTENT_TYPE; - } - -}