29
29
import org .elasticsearch .common .bytes .BytesReference ;
30
30
import org .elasticsearch .common .compress .CompressedXContent ;
31
31
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
32
+ import org .elasticsearch .common .lucene .uid .Versions ;
32
33
import org .elasticsearch .common .xcontent .XContentBuilder ;
33
34
import org .elasticsearch .common .xcontent .XContentFactory ;
34
35
import org .elasticsearch .common .xcontent .XContentType ;
@@ -69,7 +70,6 @@ public class PercolateQueryBuilderTests extends AbstractQueryTestCase<PercolateQ
69
70
private static String docType ;
70
71
71
72
private String indexedDocumentIndex ;
72
- private String indexedDocumentType ;
73
73
private String indexedDocumentId ;
74
74
private String indexedDocumentRouting ;
75
75
private String indexedDocumentPreference ;
@@ -88,7 +88,7 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
88
88
queryField = randomAlphaOfLength (4 );
89
89
aliasField = randomAlphaOfLength (4 );
90
90
91
- String docType = "_doc" ;
91
+ docType = "_doc" ;
92
92
mapperService .merge (docType , new CompressedXContent (Strings .toString (PutMappingRequest .buildFromSimplifiedDef (docType ,
93
93
queryField , "type=percolator" , aliasField , "type=alias,path=" + queryField
94
94
))), MapperService .MergeReason .MAPPING_UPDATE );
@@ -117,15 +117,14 @@ private PercolateQueryBuilder doCreateTestQueryBuilder(boolean indexedDocument)
117
117
PercolateQueryBuilder queryBuilder ;
118
118
if (indexedDocument ) {
119
119
indexedDocumentIndex = randomAlphaOfLength (4 );
120
- indexedDocumentType = "doc" ;
121
120
indexedDocumentId = randomAlphaOfLength (4 );
122
121
indexedDocumentRouting = randomAlphaOfLength (4 );
123
122
indexedDocumentPreference = randomAlphaOfLength (4 );
124
123
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 ,
126
125
indexedDocumentRouting , indexedDocumentPreference , indexedDocumentVersion );
127
126
} else {
128
- queryBuilder = new PercolateQueryBuilder (queryField , docType , documentSource , XContentType .JSON );
127
+ queryBuilder = new PercolateQueryBuilder (queryField , null , documentSource , XContentType .JSON );
129
128
}
130
129
if (randomBoolean ()) {
131
130
queryBuilder .setName (randomAlphaOfLength (4 ));
@@ -146,19 +145,19 @@ protected String[] shuffleProtectedFields() {
146
145
@ Override
147
146
protected GetResponse executeGet (GetRequest getRequest ) {
148
147
assertThat (getRequest .index (), Matchers .equalTo (indexedDocumentIndex ));
149
- assertThat (getRequest .type (), Matchers .equalTo (indexedDocumentType ));
148
+ assertThat (getRequest .type (), Matchers .equalTo (MapperService . SINGLE_MAPPING_NAME ));
150
149
assertThat (getRequest .id (), Matchers .equalTo (indexedDocumentId ));
151
150
assertThat (getRequest .routing (), Matchers .equalTo (indexedDocumentRouting ));
152
151
assertThat (getRequest .preference (), Matchers .equalTo (indexedDocumentPreference ));
153
152
assertThat (getRequest .version (), Matchers .equalTo (indexedDocumentVersion ));
154
153
if (indexedDocumentExists ) {
155
154
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 ,
157
156
documentSource .iterator ().next (), Collections .emptyMap ())
158
157
);
159
158
} else {
160
159
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 ,
162
161
false , null , Collections .emptyMap ())
163
162
);
164
163
}
@@ -168,7 +167,7 @@ protected GetResponse executeGet(GetRequest getRequest) {
168
167
protected void doAssertLuceneQuery (PercolateQueryBuilder queryBuilder , Query query , SearchContext context ) throws IOException {
169
168
assertThat (query , Matchers .instanceOf (PercolateQuery .class ));
170
169
PercolateQuery percolateQuery = (PercolateQuery ) query ;
171
- assertThat ( docType , Matchers . equalTo ( queryBuilder .getDocumentType () ));
170
+ assertNull ( queryBuilder .getDocumentType ());
172
171
assertThat (percolateQuery .getDocuments (), Matchers .equalTo (documentSource ));
173
172
}
174
173
@@ -188,7 +187,7 @@ public void testIndexedDocumentDoesNotExist() throws IOException {
188
187
PercolateQueryBuilder pqb = doCreateTestQueryBuilder (true );
189
188
ResourceNotFoundException e = expectThrows (ResourceNotFoundException .class , () -> rewriteAndFetch (pqb ,
190
189
createShardContext ()));
191
- String expectedString = "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" +
190
+ String expectedString = "indexed document [" + indexedDocumentIndex + "/" +
192
191
indexedDocumentId + "] couldn't be found" ;
193
192
assertThat (e .getMessage () , equalTo (expectedString ));
194
193
}
@@ -220,11 +219,6 @@ public void testRequiredParameters() {
220
219
});
221
220
assertThat (e .getMessage (), equalTo ("[index] is a required argument" ));
222
221
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
-
228
222
e = expectThrows (IllegalArgumentException .class , () -> {
229
223
new PercolateQueryBuilder ("_field" , "_document_type" , "_index" , "_type" , null , null , null , null );
230
224
});
@@ -237,6 +231,39 @@ public void testFromJsonNoDocumentType() throws IOException {
237
231
queryBuilder .toQuery (queryShardContext );
238
232
}
239
233
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
+
240
267
public void testBothDocumentAndDocumentsSpecified () throws IOException {
241
268
expectThrows (IllegalArgumentException .class ,
242
269
() -> parseQuery ("{\" percolate\" : { \" document\" : {}, \" documents\" : [{}, {}], \" field\" :\" " + queryField + "\" }}" ));
0 commit comments