|
52 | 52 | import org.elasticsearch.rest.RestStatus;
|
53 | 53 | import org.elasticsearch.script.Script;
|
54 | 54 | import org.elasticsearch.script.ScriptType;
|
| 55 | +import org.elasticsearch.script.mustache.SearchTemplateRequest; |
55 | 56 | import org.elasticsearch.search.aggregations.AggregationBuilders;
|
56 | 57 | import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
57 | 58 | import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
73 | 74 | import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration;
|
74 | 75 | import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
|
75 | 76 | import org.springframework.http.HttpHeaders;
|
| 77 | +import org.springframework.http.MediaType; |
76 | 78 | import org.springframework.test.context.ContextConfiguration;
|
77 | 79 |
|
78 | 80 | /**
|
@@ -434,6 +436,63 @@ public void deleteShouldReturnNotFoundForNonExistingDocument() {
|
434 | 436 | .verifyComplete();
|
435 | 437 | }
|
436 | 438 |
|
| 439 | + @Test // #1725 |
| 440 | + public void inlineSearchTemplateShouldFindMatchingDocuments() { |
| 441 | + |
| 442 | + addSourceDocument().to(INDEX_I); |
| 443 | + addSourceDocument().to(INDEX_I); |
| 444 | + |
| 445 | + Map<String, Object> testDoc = new LinkedHashMap<>(); |
| 446 | + testDoc.put("firstname", "inline"); |
| 447 | + testDoc.put("lastname", "template"); |
| 448 | + add(testDoc).to(INDEX_I); |
| 449 | + |
| 450 | + SearchTemplateRequest request = new SearchTemplateRequest(new SearchRequest(INDEX_I)); |
| 451 | + request.setScriptType(ScriptType.INLINE); |
| 452 | + request.setScript("{\"query\":{\"match\":{\"firstname\":\"{{firstname}}\"}}}"); |
| 453 | + Map<String, Object> params = new LinkedHashMap<>(); |
| 454 | + params.put("firstname", "inline"); |
| 455 | + request.setScriptParams(params); |
| 456 | + |
| 457 | + client.searchTemplate(request) |
| 458 | + .as(StepVerifier::create) |
| 459 | + .expectNextCount(1) |
| 460 | + .verifyComplete(); |
| 461 | + } |
| 462 | + |
| 463 | + @Test // #1725 |
| 464 | + public void storedSearchTemplateShouldFindMatchingDocuments() { |
| 465 | + |
| 466 | + addSourceDocument().to(INDEX_I); |
| 467 | + addSourceDocument().to(INDEX_I); |
| 468 | + |
| 469 | + Map<String, Object> testDoc = new LinkedHashMap<>(); |
| 470 | + testDoc.put("firstname", "stored"); |
| 471 | + testDoc.put("lastname", "template"); |
| 472 | + add(testDoc).to(INDEX_I); |
| 473 | + |
| 474 | + client.execute(c -> c.post() |
| 475 | + .uri(builder -> builder.path("_scripts/searchbyfirstname").build()) |
| 476 | + .contentType(MediaType.APPLICATION_JSON) |
| 477 | + .bodyValue( |
| 478 | + "{\"script\":{\"lang\":\"mustache\",\"source\":{\"query\":{\"match\":{\"firstname\":\"{{firstname}}\"}}}}}") |
| 479 | + .retrieve() |
| 480 | + .bodyToMono(Void.class)) |
| 481 | + .block(); |
| 482 | + |
| 483 | + SearchTemplateRequest request = new SearchTemplateRequest(new SearchRequest(INDEX_I)); |
| 484 | + request.setScriptType(ScriptType.STORED); |
| 485 | + request.setScript("searchbyfirstname"); |
| 486 | + Map<String, Object> params = new LinkedHashMap<>(); |
| 487 | + params.put("firstname", "stored"); |
| 488 | + request.setScriptParams(params); |
| 489 | + |
| 490 | + client.searchTemplate(request) |
| 491 | + .as(StepVerifier::create) |
| 492 | + .expectNextCount(1) |
| 493 | + .verifyComplete(); |
| 494 | + } |
| 495 | + |
437 | 496 | @Test // DATAES-488
|
438 | 497 | public void searchShouldFindExistingDocuments() {
|
439 | 498 |
|
|
0 commit comments