Skip to content

Commit 603ebf7

Browse files
authored
[Rest Api Compatibility] Types for Percolate Query Api (#74698)
Previously removed in #46985. The yaml test is included in this PR, but will be removed once #74689 is merged. relates #54160 relates main meta issue #51816
1 parent eca5735 commit 603ebf7

File tree

4 files changed

+207
-2
lines changed

4 files changed

+207
-2
lines changed

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
4343
import org.elasticsearch.common.io.stream.StreamInput;
4444
import org.elasticsearch.common.io.stream.StreamOutput;
45+
import org.elasticsearch.common.logging.DeprecationLogger;
4546
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
4647
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
4748
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -51,10 +52,12 @@
5152
import org.elasticsearch.common.xcontent.XContentHelper;
5253
import org.elasticsearch.common.xcontent.XContentParser;
5354
import org.elasticsearch.common.xcontent.XContentType;
55+
import org.elasticsearch.core.RestApiVersion;
5456
import org.elasticsearch.index.fielddata.IndexFieldData;
5557
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
5658
import org.elasticsearch.index.mapper.LuceneDocument;
5759
import org.elasticsearch.index.mapper.MappedFieldType;
60+
import org.elasticsearch.index.mapper.MapperService;
5861
import org.elasticsearch.index.mapper.ParsedDocument;
5962
import org.elasticsearch.index.mapper.SourceToParse;
6063
import org.elasticsearch.index.query.AbstractQueryBuilder;
@@ -74,19 +77,30 @@
7477
import java.util.Collections;
7578
import java.util.List;
7679
import java.util.Objects;
80+
import java.util.function.BiConsumer;
7781
import java.util.function.Supplier;
7882

7983
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
8084
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
85+
import static org.elasticsearch.core.RestApiVersion.equalTo;
8186
import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES;
8287

8388
public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBuilder> {
89+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ParseField.class);
90+
static final String DOCUMENT_TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
91+
"The [document_type] should no longer be specified.";
92+
static final String TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
93+
"The [type] of the indexed document should no longer be specified.";
94+
95+
8496
public static final String NAME = "percolate";
8597

8698
static final ParseField DOCUMENT_FIELD = new ParseField("document");
8799
static final ParseField DOCUMENTS_FIELD = new ParseField("documents");
88100
private static final ParseField NAME_FIELD = new ParseField("name");
89101
private static final ParseField QUERY_FIELD = new ParseField("field");
102+
private static final ParseField DOCUMENT_TYPE_FIELD = new ParseField("document_type");
103+
private static final ParseField INDEXED_DOCUMENT_FIELD_TYPE = new ParseField("type");
90104
private static final ParseField INDEXED_DOCUMENT_FIELD_INDEX = new ParseField("index");
91105
private static final ParseField INDEXED_DOCUMENT_FIELD_ID = new ParseField("id");
92106
private static final ParseField INDEXED_DOCUMENT_FIELD_ROUTING = new ParseField("routing");
@@ -287,6 +301,9 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
287301
if (indexedDocumentIndex != null) {
288302
builder.field(INDEXED_DOCUMENT_FIELD_INDEX.getPreferredName(), indexedDocumentIndex);
289303
}
304+
if (builder.getRestApiVersion() == RestApiVersion.V_7) {
305+
builder.field(INDEXED_DOCUMENT_FIELD_TYPE.getPreferredName(), MapperService.SINGLE_MAPPING_NAME);
306+
}
290307
if (indexedDocumentId != null) {
291308
builder.field(INDEXED_DOCUMENT_FIELD_ID.getPreferredName(), indexedDocumentId);
292309
}
@@ -338,6 +355,14 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
338355
DOCUMENTS_FIELD.getPreferredName(), INDEXED_DOCUMENT_FIELD_ID.getPreferredName());
339356
PARSER.declareExclusiveFieldSet(DOCUMENT_FIELD.getPreferredName(),
340357
DOCUMENTS_FIELD.getPreferredName(), INDEXED_DOCUMENT_FIELD_ID.getPreferredName());
358+
PARSER.declareString(deprecateAndIgnoreType("percolate_with_type", TYPE_DEPRECATION_MESSAGE),
359+
INDEXED_DOCUMENT_FIELD_TYPE.forRestApiVersion(equalTo(RestApiVersion.V_7)));
360+
PARSER.declareString(deprecateAndIgnoreType("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE),
361+
DOCUMENT_TYPE_FIELD.forRestApiVersion(equalTo(RestApiVersion.V_7)));
362+
}
363+
364+
private static BiConsumer<PercolateQueryBuilder, String> deprecateAndIgnoreType(String key, String message) {
365+
return (target, type) -> deprecationLogger.compatibleApiWarning(key, message);
341366
}
342367

343368
private static BytesReference parseDocument(XContentParser parser) throws IOException {

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import org.elasticsearch.common.lucene.uid.Versions;
2323
import org.elasticsearch.common.xcontent.XContentBuilder;
2424
import org.elasticsearch.common.xcontent.XContentFactory;
25+
import org.elasticsearch.common.xcontent.XContentParser;
2526
import org.elasticsearch.common.xcontent.XContentType;
27+
import org.elasticsearch.common.xcontent.json.JsonXContent;
28+
import org.elasticsearch.core.RestApiVersion;
2629
import org.elasticsearch.index.get.GetResult;
2730
import org.elasticsearch.index.mapper.MapperService;
2831
import org.elasticsearch.index.query.QueryBuilder;
@@ -346,4 +349,29 @@ public void testDisallowExpensiveQueries() {
346349
assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.",
347350
e.getMessage());
348351
}
352+
353+
public void testFromJsonWithDocumentType() throws IOException {
354+
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
355+
String queryAsString = "{\"percolate\" : { \"document\": {}, \"document_type\":\"" + docType + "\", \"field\":\"" +
356+
queryField + "\"}}";
357+
XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7);
358+
QueryBuilder queryBuilder = parseQuery(parser);
359+
queryBuilder.toQuery(searchExecutionContext);
360+
assertWarnings(PercolateQueryBuilder.DOCUMENT_TYPE_DEPRECATION_MESSAGE);
361+
}
362+
363+
public void testFromJsonWithType() throws IOException {
364+
indexedDocumentIndex = randomAlphaOfLength(4);
365+
indexedDocumentId = randomAlphaOfLength(4);
366+
indexedDocumentVersion = Versions.MATCH_ANY;
367+
documentSource = Collections.singletonList(randomSource(new HashSet<>()));
368+
SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
369+
370+
String queryAsString = "{\"percolate\" : { \"index\": \"" + indexedDocumentIndex +
371+
"\", \"type\": \"_doc\", \"id\": \"" + indexedDocumentId + "\", \"field\":\"" + queryField + "\"}}";
372+
XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7);
373+
QueryBuilder queryBuilder = parseQuery(parser);
374+
rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext);
375+
assertWarnings(PercolateQueryBuilder.TYPE_DEPRECATION_MESSAGE);
376+
}
349377
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
setup:
3+
- skip:
4+
version: "9.0.0 - "
5+
reason: "compatible from 8.x to 7.x"
6+
features:
7+
- "headers"
8+
- "warnings"
9+
- "allowed_warnings_regex"
10+
---
11+
"Test percolator basics via rest":
12+
13+
- do:
14+
headers:
15+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
16+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
17+
allowed_warnings_regex:
18+
- "\\[types removal\\].*"
19+
indices.create:
20+
include_type_name: true
21+
index: queries_index
22+
body:
23+
mappings:
24+
queries_type:
25+
properties:
26+
query:
27+
type: percolator
28+
foo:
29+
type: keyword
30+
31+
- do:
32+
headers:
33+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
34+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
35+
allowed_warnings_regex:
36+
- "\\[types removal\\].*"
37+
indices.create:
38+
include_type_name: true
39+
index: documents_index
40+
body:
41+
mappings:
42+
documents_type:
43+
properties:
44+
foo:
45+
type: keyword
46+
47+
- do:
48+
headers:
49+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
50+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
51+
allowed_warnings_regex:
52+
- "\\[types removal\\].*"
53+
index:
54+
index: queries_index
55+
type: queries_type
56+
id: test_percolator
57+
body:
58+
query:
59+
match_all: {}
60+
61+
- do:
62+
headers:
63+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
64+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
65+
allowed_warnings_regex:
66+
- "\\[types removal\\].*"
67+
index:
68+
index: documents_index
69+
type: documents_type
70+
id: some_id
71+
body:
72+
foo: bar
73+
74+
- do:
75+
headers:
76+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
77+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
78+
allowed_warnings_regex:
79+
- "\\[types removal\\].*"
80+
indices.refresh: {}
81+
82+
- do:
83+
headers:
84+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
85+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
86+
allowed_warnings_regex:
87+
- "\\[types removal\\].*"
88+
search:
89+
rest_total_hits_as_int: true
90+
body:
91+
- query:
92+
percolate:
93+
field: query
94+
document:
95+
document_type: queries_type
96+
foo: bar
97+
- match: { hits.total: 1 }
98+
99+
- do:
100+
headers:
101+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
102+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
103+
allowed_warnings_regex:
104+
- "\\[types removal\\].*"
105+
msearch:
106+
rest_total_hits_as_int: true
107+
body:
108+
- index: queries_index
109+
- query:
110+
percolate:
111+
field: query
112+
document_type: queries_type
113+
document:
114+
foo: bar
115+
- match: { responses.0.hits.total: 1 }
116+
117+
- do:
118+
headers:
119+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
120+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
121+
allowed_warnings_regex:
122+
- "\\[types removal\\].*"
123+
search:
124+
rest_total_hits_as_int: true
125+
body:
126+
- query:
127+
percolate:
128+
field: query
129+
index: documents_index
130+
type: documents_type
131+
id: some_id
132+
- match: { hits.total: 1 }
133+
134+
- do:
135+
headers:
136+
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
137+
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
138+
allowed_warnings_regex:
139+
- "\\[types removal\\].*"
140+
msearch:
141+
rest_total_hits_as_int: true
142+
body:
143+
- index: queries_index
144+
- query:
145+
percolate:
146+
field: query
147+
index: documents_index
148+
type: documents_type
149+
id: some_id
150+
- match: { responses.0.hits.total: 1 }

server/src/main/java/org/elasticsearch/action/search/MultiSearchRequest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public static void readMultiLineFormat(BytesReference data,
205205
// now parse the action
206206
if (nextMarker - from > 0) {
207207
try (InputStream stream = data.slice(from, nextMarker - from).streamInput();
208-
XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) {
208+
XContentParser parser = xContent
209+
.createParserForCompatibility(registry, LoggingDeprecationHandler.INSTANCE, stream, restApiVersion)) {
209210
Map<String, Object> source = parser.map();
210211
Object expandWildcards = null;
211212
Object ignoreUnavailable = null;
@@ -260,7 +261,8 @@ public static void readMultiLineFormat(BytesReference data,
260261
}
261262
BytesReference bytes = data.slice(from, nextMarker - from);
262263
try (InputStream stream = bytes.streamInput();
263-
XContentParser parser = xContent.createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) {
264+
XContentParser parser = xContent
265+
.createParserForCompatibility(registry, LoggingDeprecationHandler.INSTANCE, stream, restApiVersion)) {
264266
consumer.accept(searchRequest, parser);
265267
}
266268
// move pointers

0 commit comments

Comments
 (0)