|
@@ -21,8 +21,7 @@ SearchResponse response = client.prepareSearch("index1", "index2")
|
|
|
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
|
|
|
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
|
|
|
.setFrom(0).setSize(60).setExplain(true)
|
|
|
- .execute()
|
|
|
- .actionGet();
|
|
|
+ .get();
|
|
|
--------------------------------------------------
|
|
|
|
|
|
Note that all parameters are optional. Here is the smallest search call
|
|
@@ -31,7 +30,7 @@ you can write:
|
|
|
[source,java]
|
|
|
--------------------------------------------------
|
|
|
// MatchAll on the whole cluster with all default options
|
|
|
-SearchResponse response = client.prepareSearch().execute().actionGet();
|
|
|
+SearchResponse response = client.prepareSearch().get();
|
|
|
--------------------------------------------------
|
|
|
|
|
|
NOTE: Although the Java API defines the additional search types QUERY_AND_FETCH and
|
|
@@ -58,7 +57,7 @@ SearchResponse scrollResp = client.prepareSearch(test)
|
|
|
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
|
|
|
.setScroll(new TimeValue(60000))
|
|
|
.setQuery(qb)
|
|
|
- .setSize(100).execute().actionGet(); //max of 100 hits will be returned for each scroll
|
|
|
+ .setSize(100).get(); //max of 100 hits will be returned for each scroll
|
|
|
//Scroll until no hits are returned
|
|
|
do {
|
|
|
for (SearchHit hit : scrollResp.getHits().getHits()) {
|
|
@@ -85,7 +84,7 @@ SearchRequestBuilder srb2 = client
|
|
|
MultiSearchResponse sr = client.prepareMultiSearch()
|
|
|
.add(srb1)
|
|
|
.add(srb2)
|
|
|
- .execute().actionGet();
|
|
|
+ .get();
|
|
|
|
|
|
// You will get all individual responses from MultiSearchResponse#getResponses()
|
|
|
long nbHits = 0;
|
|
@@ -113,7 +112,7 @@ SearchResponse sr = client.prepareSearch()
|
|
|
.field("birth")
|
|
|
.dateHistogramInterval(DateHistogramInterval.YEAR)
|
|
|
)
|
|
|
- .execute().actionGet();
|
|
|
+ .get();
|
|
|
|
|
|
// Get your facet results
|
|
|
Terms agg1 = sr.getAggregations().get("agg1");
|
|
@@ -142,3 +141,115 @@ if (sr.isTerminatedEarly()) {
|
|
|
}
|
|
|
--------------------------------------------------
|
|
|
<1> Finish after 1000 docs
|
|
|
+
|
|
|
+[[java-search-template]]
|
|
|
+==== Search Template
|
|
|
+
|
|
|
+See {ref}/search-template.html[Search Template] documentation
|
|
|
+
|
|
|
+Define your template parameters as a `Map<String,Object>`:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+--------------------------------------------------
|
|
|
+Map<String, Object> template_params = new HashMap<>();
|
|
|
+template_params.put("param_gender", "male");
|
|
|
+--------------------------------------------------
|
|
|
+
|
|
|
+You can use your stored search templates in `config/scripts`.
|
|
|
+For example, if you have a file named `config/scripts/template_gender.mustache` containing:
|
|
|
+
|
|
|
+[source,js]
|
|
|
+--------------------------------------------------
|
|
|
+{
|
|
|
+ "template" : {
|
|
|
+ "query" : {
|
|
|
+ "match" : {
|
|
|
+ "gender" : "{{param_gender}}"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+--------------------------------------------------
|
|
|
+// NOTCONSOLE
|
|
|
+
|
|
|
+Create your search template request:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+--------------------------------------------------
|
|
|
+SearchResponse sr = new SearchTemplateRequestBuilder(client)
|
|
|
+ .setScript("template_gender") <1>
|
|
|
+ .setScriptType(ScriptService.ScriptType.FILE) <2>
|
|
|
+ .setScriptParams(template_params) <3>
|
|
|
+ .setRequest(new SearchRequest()) <4>
|
|
|
+ .get() <5>
|
|
|
+ .getResponse(); <6>
|
|
|
+--------------------------------------------------
|
|
|
+<1> template name
|
|
|
+<2> template stored on disk in `gender_template.mustache`
|
|
|
+<3> parameters
|
|
|
+<4> set the execution context (ie. define the index name here)
|
|
|
+<5> execute and get the template response
|
|
|
+<6> get from the template response the search response itself
|
|
|
+
|
|
|
+You can also store your template in the cluster state:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+--------------------------------------------------
|
|
|
+client.admin().cluster().preparePutStoredScript()
|
|
|
+ .setScriptLang("mustache")
|
|
|
+ .setId("template_gender")
|
|
|
+ .setSource(new BytesArray(
|
|
|
+ "{\n" +
|
|
|
+ " \"template\" : {\n" +
|
|
|
+ " \"query\" : {\n" +
|
|
|
+ " \"match\" : {\n" +
|
|
|
+ " \"gender\" : \"{{param_gender}}\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}")).get();
|
|
|
+--------------------------------------------------
|
|
|
+
|
|
|
+To execute a stored templates, use `ScriptService.ScriptType.STORED`:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+--------------------------------------------------
|
|
|
+SearchResponse sr = new SearchTemplateRequestBuilder(client)
|
|
|
+ .setScript("template_gender") <1>
|
|
|
+ .setScriptType(ScriptService.ScriptType.STORED) <2>
|
|
|
+ .setScriptParams(template_params) <3>
|
|
|
+ .setRequest(new SearchRequest()) <4>
|
|
|
+ .get() <5>
|
|
|
+ .getResponse(); <6>
|
|
|
+--------------------------------------------------
|
|
|
+<1> template name
|
|
|
+<2> template stored in the cluster state
|
|
|
+<3> parameters
|
|
|
+<4> set the execution context (ie. define the index name here)
|
|
|
+<5> execute and get the template response
|
|
|
+<6> get from the template response the search response itself
|
|
|
+
|
|
|
+You can also execute inline templates:
|
|
|
+
|
|
|
+[source,java]
|
|
|
+--------------------------------------------------
|
|
|
+sr = new SearchTemplateRequestBuilder(client)
|
|
|
+ .setScript("{\n" + <1>
|
|
|
+ " \"query\" : {\n" +
|
|
|
+ " \"match\" : {\n" +
|
|
|
+ " \"gender\" : \"{{param_gender}}\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ "}")
|
|
|
+ .setScriptType(ScriptService.ScriptType.INLINE) <2>
|
|
|
+ .setScriptParams(template_params) <3>
|
|
|
+ .setRequest(new SearchRequest()) <4>
|
|
|
+ .get() <5>
|
|
|
+ .getResponse(); <6>
|
|
|
+--------------------------------------------------
|
|
|
+<1> template name
|
|
|
+<2> template is passed inline
|
|
|
+<3> parameters
|
|
|
+<4> set the execution context (ie. define the index name here)
|
|
|
+<5> execute and get the template response
|
|
|
+<6> get from the template response the search response itself
|