Skip to content

Commit

Permalink
[Rest Api Compatibility] Typed query
Browse files Browse the repository at this point in the history
Type information is no longer available, thereofre type query in v7
compatibility is working as match_all query
previously removed in elastic#47207

relates main meta issue elastic#51816
relates types removal meta elastic#54160
  • Loading branch information
pgomulka committed Jul 19, 2021
1 parent e978b72 commit fcce7af
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rest-api-spec/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TypeQueryV7Builder> {
public static final String NAME = "type";
private static final ParseField VALUE_FIELD = new ParseField("value");
private static final ObjectParser<TypeQueryV7Builder, Void> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -837,6 +838,7 @@ private void registerQueryParsers(List<SearchPlugin> 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);
}
Expand Down

0 comments on commit fcce7af

Please sign in to comment.