Skip to content

Commit a4f0224

Browse files
Add documentation for templated search (#362) (#363)
Co-authored-by: Sylvain Wallez <[email protected]>
1 parent 3f4c2eb commit a4f0224

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

docs/usage/searching.asciidoc

+25
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,29 @@ include-tagged::{doc-tests-src}/usage/SearchingTest.java[search-nested]
4343
<4> The search query is a boolean query that combines the text search and max price queries.
4444
<5> Both queries are added as `must` as we want results to match all criteria.
4545

46+
[discrete]
47+
==== Templated search
48+
49+
A search template is a stored search that you can run with different variables. Search templates let you change your searches without modifying your application code.
50+
51+
Before running a template search, you first have to create the template. This is a stored script that returns the search request body, and is usually defined as a Mustache template. This stored script can be created outside the application, and also with the {java-client}:
52+
53+
["source","java"]
54+
--------------------------------------------------
55+
include-tagged::{doc-tests-src}/usage/SearchingTest.java[search-template-script]
56+
--------------------------------------------------
57+
<1> Identifier of the template script to create.
58+
59+
To use the search template, use the `searchTemplate` method to refer to the script and provide values for its parameters:
60+
61+
["source","java"]
62+
--------------------------------------------------
63+
include-tagged::{doc-tests-src}/usage/SearchingTest.java[search-template-query]
64+
--------------------------------------------------
65+
<1> Identifier of the template script to use.
66+
<2> Template parameter values.
67+
68+
For more in-depth information, see the {es-docs}/search-template.html[{es} search template documentation].
69+
70+
4671
{doc-tests-blurb}

java-client/src/test/java/co/elastic/clients/documentation/usage/SearchingTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
2525
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
2626
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
27+
import co.elastic.clients.elasticsearch.core.PutScriptResponse;
2728
import co.elastic.clients.elasticsearch.core.SearchResponse;
29+
import co.elastic.clients.elasticsearch.core.SearchTemplateResponse;
2830
import co.elastic.clients.elasticsearch.core.search.Hit;
2931
import co.elastic.clients.elasticsearch.core.search.TotalHits;
3032
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
@@ -133,4 +135,43 @@ public void searchNested() throws Exception {
133135
}
134136
//end::search-nested
135137
}
138+
139+
@Test
140+
public void searchTemplate() throws Exception {
141+
142+
transport.setResult(PutScriptResponse.of(r -> r.acknowledged(true)));
143+
144+
//tag::search-template-script
145+
// Create a script
146+
esClient.putScript(r -> r
147+
.id("query-script") // <1>
148+
.script(s -> s
149+
.lang("mustache")
150+
.source("{\"query\":{\"match\":{\"{{field}}\":\"{{value}}\"}}}")
151+
));
152+
//end::search-template-script
153+
154+
transport.setResult(SearchTemplateResponse.<JsonData>of(r -> r
155+
.hits(searchResponse.hits())
156+
.took(searchResponse.took())
157+
.timedOut(false)
158+
.shards(searchResponse.shards())
159+
));
160+
161+
//tag::search-template-query
162+
SearchTemplateResponse<Product> response = esClient.searchTemplate(r -> r
163+
.index("some-index")
164+
.id("query-script") // <1>
165+
.params("field", JsonData.of("some-field")) // <2>
166+
.params("value", JsonData.of("some-data")),
167+
Product.class
168+
);
169+
170+
List<Hit<Product>> hits = response.hits().hits();
171+
for (Hit<Product> hit: hits) {
172+
Product product = hit.source();
173+
logger.info("Found product " + product.getSku() + ", score " + hit.score());
174+
}
175+
//end::search-template-query
176+
}
136177
}

0 commit comments

Comments
 (0)