Skip to content

Commit fcce7af

Browse files
committed
[Rest Api Compatibility] Typed query
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
1 parent e978b72 commit fcce7af

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

rest-api-spec/build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ tasks.named("yamlRestCompatTest").configure {
9494
'indices.forcemerge/10_basic/Check deprecation warning when incompatible only_expunge_deletes and max_num_segments values are both set',
9595
// not fixing this in #70966
9696
'indices.put_template/11_basic_with_types/Put template with empty mappings',
97-
'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers',
97+
'search.aggregation/200_top_hits_metric/top_hits aggregation with sequence numbers', //#42809 nested path and nested filter
9898
'search.aggregation/51_filter_with_types/Filter aggs with terms lookup and ensure it\'s cached',
9999
'search/150_rewrite_on_coordinator/Ensure that we fetch the document only once', //terms_lookup
100100
'search/310_match_bool_prefix/multi_match multiple fields with cutoff_frequency throws exception', //cutoff_frequency
101-
'search/340_type_query/type query', // type_query - probably should behave like match_all
102101
] + v7compatibilityNotSupportedTests())
103102
.join(',')
104103

@@ -226,6 +225,8 @@ tasks.named("transformV7RestTests").configure({ task ->
226225
task.removeWarningForTest("the default value for the ?wait_for_active_shards parameter will change from '0' to 'index-setting' in version 8; " +
227226
"specify '?wait_for_active_shards=index-setting' to adopt the future default behaviour, or '?wait_for_active_shards=0' to preserve today's behaviour"
228227
, "?wait_for_active_shards default is deprecated")
228+
229+
task.replaceValueInMatch("hits.total", 1, "type query") // 340_type_query.yml
229230
})
230231

231232
tasks.register('enforceYamlTestConvention').configure {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.index.query;
10+
11+
import org.apache.lucene.search.MatchAllDocsQuery;
12+
import org.apache.lucene.search.Query;
13+
import org.elasticsearch.common.ParsingException;
14+
import org.elasticsearch.common.io.stream.StreamInput;
15+
import org.elasticsearch.common.io.stream.StreamOutput;
16+
import org.elasticsearch.common.xcontent.ObjectParser;
17+
import org.elasticsearch.common.xcontent.ParseField;
18+
import org.elasticsearch.common.xcontent.XContentBuilder;
19+
import org.elasticsearch.common.xcontent.XContentParser;
20+
import org.elasticsearch.core.RestApiVersion;
21+
import org.elasticsearch.index.mapper.MapperService;
22+
23+
import java.io.IOException;
24+
25+
public class TypeQueryV7Builder extends AbstractQueryBuilder<TypeQueryV7Builder> {
26+
public static final String NAME = "type";
27+
private static final ParseField VALUE_FIELD = new ParseField("value");
28+
private static final ObjectParser<TypeQueryV7Builder, Void> PARSER = new ObjectParser<>(NAME, TypeQueryV7Builder::new);
29+
30+
static {
31+
PARSER.declareString(QueryBuilder::queryName,
32+
AbstractQueryBuilder.NAME_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7)));
33+
PARSER.declareFloat(QueryBuilder::boost,
34+
AbstractQueryBuilder.BOOST_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7)));
35+
PARSER.declareString((queryBuilder, value) -> {
36+
},
37+
VALUE_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7)));
38+
}
39+
40+
public TypeQueryV7Builder() {
41+
}
42+
43+
/**
44+
* Read from a stream.
45+
*/
46+
public TypeQueryV7Builder(StreamInput in) throws IOException {
47+
super(in);
48+
}
49+
50+
@Override
51+
protected void doWriteTo(StreamOutput out) throws IOException {
52+
}
53+
54+
@Override
55+
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
56+
builder.startObject(NAME);
57+
builder.field(VALUE_FIELD.getPreferredName(), MapperService.SINGLE_MAPPING_NAME);
58+
printBoostAndQueryName(builder);
59+
builder.endObject();
60+
}
61+
62+
@Override
63+
protected Query doToQuery(SearchExecutionContext context) throws IOException {
64+
return new MatchAllDocsQuery();
65+
}
66+
67+
@Override
68+
protected boolean doEquals(TypeQueryV7Builder other) {
69+
return true;
70+
}
71+
72+
@Override
73+
protected int doHashCode() {
74+
return 0;
75+
}
76+
77+
public static TypeQueryV7Builder fromXContent(XContentParser parser) throws IOException {
78+
try {
79+
return PARSER.apply(parser, null);
80+
} catch (IllegalArgumentException e) {
81+
throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e);
82+
}
83+
}
84+
85+
@Override
86+
public String getWriteableName() {
87+
return NAME;
88+
}
89+
}

server/src/main/java/org/elasticsearch/search/SearchModule.java

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.elasticsearch.index.query.TermQueryBuilder;
6868
import org.elasticsearch.index.query.TermsQueryBuilder;
6969
import org.elasticsearch.index.query.TermsSetQueryBuilder;
70+
import org.elasticsearch.index.query.TypeQueryV7Builder;
7071
import org.elasticsearch.index.query.WildcardQueryBuilder;
7172
import org.elasticsearch.index.query.WrapperQueryBuilder;
7273
import org.elasticsearch.index.query.functionscore.ExponentialDecayFunctionBuilder;
@@ -837,6 +838,7 @@ private void registerQueryParsers(List<SearchPlugin> plugins) {
837838
if (ShapesAvailability.JTS_AVAILABLE && ShapesAvailability.SPATIAL4J_AVAILABLE) {
838839
registerQuery(new QuerySpec<>(GeoShapeQueryBuilder.NAME, GeoShapeQueryBuilder::new, GeoShapeQueryBuilder::fromXContent));
839840
}
841+
registerQuery(new QuerySpec<>(TypeQueryV7Builder.NAME, TypeQueryV7Builder::new, TypeQueryV7Builder::fromXContent));
840842

841843
registerFromPlugin(plugins, SearchPlugin::getQueries, this::registerQuery);
842844
}

0 commit comments

Comments
 (0)