diff --git a/rest-api-spec/build.gradle b/rest-api-spec/build.gradle index 56aabfc0092ab..e1475dbb6beee 100644 --- a/rest-api-spec/build.gradle +++ b/rest-api-spec/build.gradle @@ -94,11 +94,10 @@ tasks.named("yamlRestCompatTest").configure { 'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set', // not fixing this in #70966 'indices.put_template/11_basic_with_types/Put template with empty mappings', - 'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers', + 'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers', //#42809 nested path and nested filter 'search.aggregation/51_filter_with_types/Filter aggs with terms lookup and ensure it\'s cached', 'search/150_rewrite_on_coordinator/Ensure that we fetch the document only once', //terms_lookup 'search/310_match_bool_prefix/multi_match multiple fields with cutoff_frequency throws exception', //cutoff_frequency - 'search/340_type_query/type query', // type_query - probably should behave like match_all ] + v7compatibilityNotSupportedTests()) .join(',') @@ -226,6 +225,8 @@ tasks.named("transformV7RestTests").configure({ task -> task.removeWarningForTest("the default value for the ?wait_for_active_shards parameter will change from '0' to 'index-setting' in version 8; " + "specify '?wait_for_active_shards=index-setting' to adopt the future default behaviour, or '?wait_for_active_shards=0' to preserve today's behaviour" , "?wait_for_active_shards default is deprecated") + + task.replaceValueInMatch("hits.total", 1, "type query") // 340_type_query.yml }) tasks.register('enforceYamlTestConvention').configure { diff --git a/server/src/main/java/org/elasticsearch/index/query/TypeQueryV7Builder.java b/server/src/main/java/org/elasticsearch/index/query/TypeQueryV7Builder.java new file mode 100644 index 0000000000000..e502d10d06ce7 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/query/TypeQueryV7Builder.java @@ -0,0 +1,89 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.index.query; + +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.common.ParsingException; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ParseField; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.index.mapper.MapperService; + +import java.io.IOException; + +public class TypeQueryV7Builder extends AbstractQueryBuilder { + public static final String NAME = "type"; + private static final ParseField VALUE_FIELD = new ParseField("value"); + private static final ObjectParser PARSER = new ObjectParser<>(NAME, TypeQueryV7Builder::new); + + static { + PARSER.declareString(QueryBuilder::queryName, + AbstractQueryBuilder.NAME_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); + PARSER.declareFloat(QueryBuilder::boost, + AbstractQueryBuilder.BOOST_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); + PARSER.declareString((queryBuilder, value) -> { + }, + VALUE_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); + } + + public TypeQueryV7Builder() { + } + + /** + * Read from a stream. + */ + public TypeQueryV7Builder(StreamInput in) throws IOException { + super(in); + } + + @Override + protected void doWriteTo(StreamOutput out) throws IOException { + } + + @Override + protected void doXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(NAME); + builder.field(VALUE_FIELD.getPreferredName(), MapperService.SINGLE_MAPPING_NAME); + printBoostAndQueryName(builder); + builder.endObject(); + } + + @Override + protected Query doToQuery(SearchExecutionContext context) throws IOException { + return new MatchAllDocsQuery(); + } + + @Override + protected boolean doEquals(TypeQueryV7Builder other) { + return true; + } + + @Override + protected int doHashCode() { + return 0; + } + + public static TypeQueryV7Builder fromXContent(XContentParser parser) throws IOException { + try { + return PARSER.apply(parser, null); + } catch (IllegalArgumentException e) { + throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e); + } + } + + @Override + public String getWriteableName() { + return NAME; + } +} diff --git a/server/src/main/java/org/elasticsearch/search/SearchModule.java b/server/src/main/java/org/elasticsearch/search/SearchModule.java index 927e0e42544a5..671983ab4bb1d 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/server/src/main/java/org/elasticsearch/search/SearchModule.java @@ -67,6 +67,7 @@ import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.TermsQueryBuilder; import org.elasticsearch.index.query.TermsSetQueryBuilder; +import org.elasticsearch.index.query.TypeQueryV7Builder; import org.elasticsearch.index.query.WildcardQueryBuilder; import org.elasticsearch.index.query.WrapperQueryBuilder; import org.elasticsearch.index.query.functionscore.ExponentialDecayFunctionBuilder; @@ -837,6 +838,7 @@ private void registerQueryParsers(List plugins) { if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) { registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent)); } + registerQuery(new QuerySpec<>(TypeQueryV7Builder.NAME, TypeQueryV7Builder::new, TypeQueryV7Builder::fromXContent)); registerFromPlugin(plugins, SearchPlugin::getQueries, this::registerQuery); }