Skip to content

Commit 9731ba4

Browse files
authored
Make the type parameter optional when percolating existing documents. (#39987) (#39989)
`document_type` is the type to use for parsing the document to percolate, which is already optional and deprecated. However `percotale` queries also have the ability to percolate existing documents, identified by an index, an id and a type. This change makes the latter optional and deprecated. Closes #39963
1 parent 16e4499 commit 9731ba4

File tree

5 files changed

+202
-27
lines changed

5 files changed

+202
-27
lines changed

docs/reference/query-dsl/percolate-query.asciidoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ The following parameters are required when percolating a document:
134134
This is an optional parameter.
135135
`document`:: The source of the document being percolated.
136136
`documents`:: Like the `document` parameter, but accepts multiple documents via a json array.
137-
`document_type`:: The type / mapping of the document being percolated. This setting is deprecated and only required for indices created before 6.0
137+
`document_type`:: The type / mapping of the document being percolated. This parameter is deprecated and will be removed in Elasticsearch 8.0.
138138

139139
Instead of specifying the source of the document being percolated, the source can also be retrieved from an already
140140
stored document. The `percolate` query will then internally execute a get request to fetch that document.
@@ -143,7 +143,7 @@ In that case the `document` parameter can be substituted with the following para
143143

144144
[horizontal]
145145
`index`:: The index the document resides in. This is a required parameter.
146-
`type`:: The type of the document to fetch. This is a required parameter.
146+
`type`:: The type of the document to fetch. This parameter is deprecated and will be removed in Elasticsearch 8.0.
147147
`id`:: The id of the document to fetch. This is a required parameter.
148148
`routing`:: Optionally, routing to be used to fetch document to percolate.
149149
`preference`:: Optionally, preference to be used to fetch document to percolate.
@@ -323,7 +323,6 @@ GET /my-index/_search
323323
"percolate" : {
324324
"field": "query",
325325
"index" : "my-index",
326-
"type" : "_doc",
327326
"id" : "2",
328327
"version" : 1 <1>
329328
}

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
9696
public static final String NAME = "percolate";
9797

9898
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ParseField.class));
99+
static final String DOCUMENT_TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
100+
"The [document_type] should no longer be specified.";
101+
static final String TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
102+
"The [type] of the indexed document should no longer be specified.";
99103

100104
static final ParseField DOCUMENT_FIELD = new ParseField("document");
101105
static final ParseField DOCUMENTS_FIELD = new ParseField("documents");
@@ -117,6 +121,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
117121
private final XContentType documentXContentType;
118122

119123
private final String indexedDocumentIndex;
124+
@Deprecated
120125
private final String indexedDocumentType;
121126
private final String indexedDocumentId;
122127
private final String indexedDocumentRouting;
@@ -220,9 +225,6 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
220225
if (indexedDocumentIndex == null) {
221226
throw new IllegalArgumentException("[index] is a required argument");
222227
}
223-
if (indexedDocumentType == null) {
224-
throw new IllegalArgumentException("[type] is a required argument");
225-
}
226228
if (indexedDocumentId == null) {
227229
throw new IllegalArgumentException("[id] is a required argument");
228230
}
@@ -521,7 +523,13 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
521523
XContentHelper.xContentType(source));
522524
}
523525
}
524-
GetRequest getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
526+
GetRequest getRequest;
527+
if (indexedDocumentType != null) {
528+
deprecationLogger.deprecatedAndMaybeLog("percolate_with_type", TYPE_DEPRECATION_MESSAGE);
529+
getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentType, indexedDocumentId);
530+
} else {
531+
getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentId);
532+
}
525533
getRequest.preference("_local");
526534
getRequest.routing(indexedDocumentRouting);
527535
getRequest.preference(indexedDocumentPreference);
@@ -533,13 +541,14 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
533541
client.get(getRequest, ActionListener.wrap(getResponse -> {
534542
if (getResponse.isExists() == false) {
535543
throw new ResourceNotFoundException(
536-
"indexed document [{}/{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentType, indexedDocumentId
544+
"indexed document [{}{}/{}] couldn't be found", indexedDocumentIndex,
545+
indexedDocumentType == null ? "" : "/" + indexedDocumentType, indexedDocumentId
537546
);
538547
}
539548
if(getResponse.isSourceEmpty()) {
540549
throw new IllegalArgumentException(
541-
"indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" + indexedDocumentId
542-
+ "] source disabled"
550+
"indexed document [" + indexedDocumentIndex + (indexedDocumentType == null ? "" : "/" + indexedDocumentType) +
551+
"/" + indexedDocumentId + "] source disabled"
543552
);
544553
}
545554
documentSupplier.set(getResponse.getSourceAsBytesRef());
@@ -554,7 +563,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
554563
// Call nowInMillis() so that this query becomes un-cacheable since we
555564
// can't be sure that it doesn't use now or scripts
556565
context.nowInMillis();
557-
if (indexedDocumentIndex != null || indexedDocumentType != null || indexedDocumentId != null || documentSupplier != null) {
566+
if (indexedDocumentIndex != null || indexedDocumentId != null || documentSupplier != null) {
558567
throw new IllegalStateException("query builder must be rewritten first");
559568
}
560569

@@ -577,7 +586,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
577586
final MapperService mapperService = context.getMapperService();
578587
String type = mapperService.documentMapper().type();
579588
if (documentType != null) {
580-
deprecationLogger.deprecated("[document_type] parameter has been deprecated because types have been deprecated");
589+
deprecationLogger.deprecatedAndMaybeLog("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE);
581590
if (documentType.equals(type) == false) {
582591
throw new IllegalArgumentException("specified document_type [" + documentType +
583592
"] is not equal to the actual type [" + type + "]");

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.common.bytes.BytesReference;
3030
import org.elasticsearch.common.compress.CompressedXContent;
3131
import org.elasticsearch.common.io.stream.BytesStreamOutput;
32+
import org.elasticsearch.common.lucene.uid.Versions;
3233
import org.elasticsearch.common.xcontent.XContentBuilder;
3334
import org.elasticsearch.common.xcontent.XContentFactory;
3435
import org.elasticsearch.common.xcontent.XContentType;
@@ -69,7 +70,6 @@ public class PercolateQueryBuilderTests extends AbstractQueryTestCase<PercolateQ
6970
private static String docType;
7071

7172
private String indexedDocumentIndex;
72-
private String indexedDocumentType;
7373
private String indexedDocumentId;
7474
private String indexedDocumentRouting;
7575
private String indexedDocumentPreference;
@@ -88,7 +88,7 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
8888
queryField = randomAlphaOfLength(4);
8989
aliasField = randomAlphaOfLength(4);
9090

91-
String docType = "_doc";
91+
docType = "_doc";
9292
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
9393
queryField, "type=percolator", aliasField, "type=alias,path=" + queryField
9494
))), MapperService.MergeReason.MAPPING_UPDATE);
@@ -117,15 +117,14 @@ private PercolateQueryBuilder doCreateTestQueryBuilder(boolean indexedDocument)
117117
PercolateQueryBuilder queryBuilder;
118118
if (indexedDocument) {
119119
indexedDocumentIndex = randomAlphaOfLength(4);
120-
indexedDocumentType = "doc";
121120
indexedDocumentId = randomAlphaOfLength(4);
122121
indexedDocumentRouting = randomAlphaOfLength(4);
123122
indexedDocumentPreference = randomAlphaOfLength(4);
124123
indexedDocumentVersion = (long) randomIntBetween(0, Integer.MAX_VALUE);
125-
queryBuilder = new PercolateQueryBuilder(queryField, docType, indexedDocumentIndex, indexedDocumentType, indexedDocumentId,
124+
queryBuilder = new PercolateQueryBuilder(queryField, null, indexedDocumentIndex, null, indexedDocumentId,
126125
indexedDocumentRouting, indexedDocumentPreference, indexedDocumentVersion);
127126
} else {
128-
queryBuilder = new PercolateQueryBuilder(queryField, docType, documentSource, XContentType.JSON);
127+
queryBuilder = new PercolateQueryBuilder(queryField, null, documentSource, XContentType.JSON);
129128
}
130129
if (randomBoolean()) {
131130
queryBuilder.setName(randomAlphaOfLength(4));
@@ -146,19 +145,19 @@ protected String[] shuffleProtectedFields() {
146145
@Override
147146
protected GetResponse executeGet(GetRequest getRequest) {
148147
assertThat(getRequest.index(), Matchers.equalTo(indexedDocumentIndex));
149-
assertThat(getRequest.type(), Matchers.equalTo(indexedDocumentType));
148+
assertThat(getRequest.type(), Matchers.equalTo(MapperService.SINGLE_MAPPING_NAME));
150149
assertThat(getRequest.id(), Matchers.equalTo(indexedDocumentId));
151150
assertThat(getRequest.routing(), Matchers.equalTo(indexedDocumentRouting));
152151
assertThat(getRequest.preference(), Matchers.equalTo(indexedDocumentPreference));
153152
assertThat(getRequest.version(), Matchers.equalTo(indexedDocumentVersion));
154153
if (indexedDocumentExists) {
155154
return new GetResponse(
156-
new GetResult(indexedDocumentIndex, indexedDocumentType, indexedDocumentId, 0, 1, 0L, true,
155+
new GetResult(indexedDocumentIndex, MapperService.SINGLE_MAPPING_NAME, indexedDocumentId, 0, 1, 0L, true,
157156
documentSource.iterator().next(), Collections.emptyMap())
158157
);
159158
} else {
160159
return new GetResponse(
161-
new GetResult(indexedDocumentIndex, indexedDocumentType, indexedDocumentId, UNASSIGNED_SEQ_NO, 0, -1,
160+
new GetResult(indexedDocumentIndex, MapperService.SINGLE_MAPPING_NAME, indexedDocumentId, UNASSIGNED_SEQ_NO, 0, -1,
162161
false, null, Collections.emptyMap())
163162
);
164163
}
@@ -168,7 +167,7 @@ protected GetResponse executeGet(GetRequest getRequest) {
168167
protected void doAssertLuceneQuery(PercolateQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
169168
assertThat(query, Matchers.instanceOf(PercolateQuery.class));
170169
PercolateQuery percolateQuery = (PercolateQuery) query;
171-
assertThat(docType, Matchers.equalTo(queryBuilder.getDocumentType()));
170+
assertNull(queryBuilder.getDocumentType());
172171
assertThat(percolateQuery.getDocuments(), Matchers.equalTo(documentSource));
173172
}
174173

@@ -188,7 +187,7 @@ public void testIndexedDocumentDoesNotExist() throws IOException {
188187
PercolateQueryBuilder pqb = doCreateTestQueryBuilder(true);
189188
ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> rewriteAndFetch(pqb,
190189
createShardContext()));
191-
String expectedString = "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" +
190+
String expectedString = "indexed document [" + indexedDocumentIndex + "/" +
192191
indexedDocumentId + "] couldn't be found";
193192
assertThat(e.getMessage() , equalTo(expectedString));
194193
}
@@ -220,11 +219,6 @@ public void testRequiredParameters() {
220219
});
221220
assertThat(e.getMessage(), equalTo("[index] is a required argument"));
222221

223-
e = expectThrows(IllegalArgumentException.class, () -> {
224-
new PercolateQueryBuilder("_field", "_document_type", "_index", null, "_id", null, null, null);
225-
});
226-
assertThat(e.getMessage(), equalTo("[type] is a required argument"));
227-
228222
e = expectThrows(IllegalArgumentException.class, () -> {
229223
new PercolateQueryBuilder("_field", "_document_type", "_index", "_type", null, null, null, null);
230224
});
@@ -237,6 +231,39 @@ public void testFromJsonNoDocumentType() throws IOException {
237231
queryBuilder.toQuery(queryShardContext);
238232
}
239233

234+
public void testFromJsonWithDocumentType() throws IOException {
235+
QueryShardContext queryShardContext = createShardContext();
236+
QueryBuilder queryBuilder = parseQuery("{\"percolate\" : { \"document\": {}, \"document_type\":\"" + docType + "\", \"field\":\"" +
237+
queryField + "\"}}");
238+
queryBuilder.toQuery(queryShardContext);
239+
assertWarnings(PercolateQueryBuilder.DOCUMENT_TYPE_DEPRECATION_MESSAGE);
240+
}
241+
242+
public void testFromJsonNoType() throws IOException {
243+
indexedDocumentIndex = randomAlphaOfLength(4);
244+
indexedDocumentId = randomAlphaOfLength(4);
245+
indexedDocumentVersion = Versions.MATCH_ANY;
246+
documentSource = Collections.singletonList(randomSource(new HashSet<>()));
247+
248+
QueryShardContext queryShardContext = createShardContext();
249+
QueryBuilder queryBuilder = parseQuery("{\"percolate\" : { \"index\": \"" + indexedDocumentIndex + "\", \"id\": \"" +
250+
indexedDocumentId + "\", \"field\":\"" + queryField + "\"}}");
251+
rewriteAndFetch(queryBuilder, queryShardContext).toQuery(queryShardContext);
252+
}
253+
254+
public void testFromJsonWithType() throws IOException {
255+
indexedDocumentIndex = randomAlphaOfLength(4);
256+
indexedDocumentId = randomAlphaOfLength(4);
257+
indexedDocumentVersion = Versions.MATCH_ANY;
258+
documentSource = Collections.singletonList(randomSource(new HashSet<>()));
259+
260+
QueryShardContext queryShardContext = createShardContext();
261+
QueryBuilder queryBuilder = parseQuery("{\"percolate\" : { \"index\": \"" + indexedDocumentIndex +
262+
"\", \"type\": \"_doc\", \"id\": \"" + indexedDocumentId + "\", \"field\":\"" + queryField + "\"}}");
263+
rewriteAndFetch(queryBuilder, queryShardContext).toQuery(queryShardContext);
264+
assertWarnings(PercolateQueryBuilder.TYPE_DEPRECATION_MESSAGE);
265+
}
266+
240267
public void testBothDocumentAndDocumentsSpecified() throws IOException {
241268
expectThrows(IllegalArgumentException.class,
242269
() -> parseQuery("{\"percolate\" : { \"document\": {}, \"documents\": [{}, {}], \"field\":\"" + queryField + "\"}}"));

modules/percolator/src/test/resources/rest-api-spec/test/10_basic.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
---
22
"Test percolator basics via rest":
3+
4+
- skip:
5+
version: " - 6.99.99"
6+
reason: types are required in requests before 7.0.0
7+
38
- do:
49
indices.create:
510
index: queries_index
@@ -11,6 +16,15 @@
1116
foo:
1217
type: keyword
1318

19+
- do:
20+
indices.create:
21+
index: documents_index
22+
body:
23+
mappings:
24+
properties:
25+
foo:
26+
type: keyword
27+
1428
- do:
1529
index:
1630
index: queries_index
@@ -19,6 +33,13 @@
1933
query:
2034
match_all: {}
2135

36+
- do:
37+
index:
38+
index: documents_index
39+
id: some_id
40+
body:
41+
foo: bar
42+
2243
- do:
2344
indices.refresh: {}
2445

@@ -44,3 +65,26 @@
4465
document:
4566
foo: bar
4667
- match: { responses.0.hits.total: 1 }
68+
69+
- do:
70+
search:
71+
rest_total_hits_as_int: true
72+
body:
73+
- query:
74+
percolate:
75+
field: query
76+
index: documents_index
77+
id: some_id
78+
- match: { hits.total: 1 }
79+
80+
- do:
81+
msearch:
82+
rest_total_hits_as_int: true
83+
body:
84+
- index: queries_index
85+
- query:
86+
percolate:
87+
field: query
88+
index: documents_index
89+
id: some_id
90+
- match: { responses.0.hits.total: 1 }

0 commit comments

Comments
 (0)