2015-06-24 17:27:19 -04:00
|
|
|
[[java-search]]
|
2013-08-28 19:24:34 -04:00
|
|
|
== Search API
|
|
|
|
|
2014-05-12 22:07:46 -04:00
|
|
|
The search API allows one to execute a search query and get back search hits
|
2013-08-28 19:24:34 -04:00
|
|
|
that match the query. It can be executed across one or more indices and
|
2017-02-01 03:23:32 -05:00
|
|
|
across one or more types. The query can be provided using the <<java-query-dsl,query Java API>>.
|
2015-06-24 17:27:19 -04:00
|
|
|
The body of the search request is built using the `SearchSourceBuilder`. Here is an example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
|
import org.elasticsearch.action.search.SearchType;
|
|
|
|
import org.elasticsearch.index.query.QueryBuilders.*;
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
SearchResponse response = client.prepareSearch("index1", "index2")
|
|
|
|
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
2015-06-24 17:27:19 -04:00
|
|
|
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
|
|
|
|
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
|
2013-08-28 19:24:34 -04:00
|
|
|
.setFrom(0).setSize(60).setExplain(true)
|
2016-11-28 11:33:40 -05:00
|
|
|
.get();
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Note that all parameters are optional. Here is the smallest search call
|
|
|
|
you can write:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
// MatchAll on the whole cluster with all default options
|
2016-11-28 11:33:40 -05:00
|
|
|
SearchResponse response = client.prepareSearch().get();
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-04-28 19:28:54 -04:00
|
|
|
NOTE: Although the Java API defines the additional search types QUERY_AND_FETCH and
|
|
|
|
DFS_QUERY_AND_FETCH, these modes are internal optimizations and should not
|
|
|
|
be specified explicitly by users of the API.
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
For more information on the search operation, check out the REST
|
2013-09-03 10:15:28 -04:00
|
|
|
{ref}/search.html[search] docs.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
[[java-search-scrolling]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Using scrolls in Java
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
Read the {ref}/search-request-scroll.html[scroll documentation]
|
2013-08-28 19:24:34 -04:00
|
|
|
first!
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
|
|
|
|
|
|
|
QueryBuilder qb = termQuery("multi", "test");
|
|
|
|
|
|
|
|
SearchResponse scrollResp = client.prepareSearch(test)
|
2016-04-07 00:52:14 -04:00
|
|
|
.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
|
2013-08-28 19:24:34 -04:00
|
|
|
.setScroll(new TimeValue(60000))
|
|
|
|
.setQuery(qb)
|
2016-11-28 11:33:40 -05:00
|
|
|
.setSize(100).get(); //max of 100 hits will be returned for each scroll
|
2013-08-28 19:24:34 -04:00
|
|
|
//Scroll until no hits are returned
|
2015-09-11 17:08:37 -04:00
|
|
|
do {
|
2015-01-15 13:01:52 -05:00
|
|
|
for (SearchHit hit : scrollResp.getHits().getHits()) {
|
2013-08-28 19:24:34 -04:00
|
|
|
//Handle the hit...
|
|
|
|
}
|
2015-09-11 17:08:37 -04:00
|
|
|
|
2016-01-19 08:42:29 -05:00
|
|
|
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
|
2015-09-11 17:08:37 -04:00
|
|
|
} while(scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
[[java-search-msearch]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== MultiSearch API
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
See {ref}/search-multi-search.html[MultiSearch API Query]
|
2013-08-28 19:24:34 -04:00
|
|
|
documentation
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-10-17 12:16:59 -04:00
|
|
|
SearchRequestBuilder srb1 = client
|
2015-06-03 11:08:32 -04:00
|
|
|
.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
|
2016-10-17 12:16:59 -04:00
|
|
|
SearchRequestBuilder srb2 = client
|
2013-08-28 19:24:34 -04:00
|
|
|
.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
|
|
|
|
|
2016-10-17 12:16:59 -04:00
|
|
|
MultiSearchResponse sr = client.prepareMultiSearch()
|
2013-08-28 19:24:34 -04:00
|
|
|
.add(srb1)
|
|
|
|
.add(srb2)
|
2016-11-28 11:33:40 -05:00
|
|
|
.get();
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-10-17 03:47:58 -04:00
|
|
|
// You will get all individual responses from MultiSearchResponse#getResponses()
|
2013-08-28 19:24:34 -04:00
|
|
|
long nbHits = 0;
|
2013-10-06 15:18:43 -04:00
|
|
|
for (MultiSearchResponse.Item item : sr.getResponses()) {
|
|
|
|
SearchResponse response = item.getResponse();
|
|
|
|
nbHits += response.getHits().getTotalHits();
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2014-05-15 06:36:05 -04:00
|
|
|
[[java-search-aggs]]
|
|
|
|
=== Using Aggregations
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-05-15 06:36:05 -04:00
|
|
|
The following code shows how to add two aggregations within your search:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-10-17 12:16:59 -04:00
|
|
|
SearchResponse sr = client.prepareSearch()
|
2013-08-28 19:24:34 -04:00
|
|
|
.setQuery(QueryBuilders.matchAllQuery())
|
2014-05-15 06:36:05 -04:00
|
|
|
.addAggregation(
|
|
|
|
AggregationBuilders.terms("agg1").field("field")
|
|
|
|
)
|
|
|
|
.addAggregation(
|
|
|
|
AggregationBuilders.dateHistogram("agg2")
|
|
|
|
.field("birth")
|
2016-02-12 08:54:10 -05:00
|
|
|
.dateHistogramInterval(DateHistogramInterval.YEAR)
|
2014-05-15 06:36:05 -04:00
|
|
|
)
|
2016-11-28 11:33:40 -05:00
|
|
|
.get();
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
// Get your facet results
|
2014-05-15 06:36:05 -04:00
|
|
|
Terms agg1 = sr.getAggregations().get("agg1");
|
2017-08-14 14:49:13 -04:00
|
|
|
Histogram agg2 = sr.getAggregations().get("agg2");
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-05-15 06:36:05 -04:00
|
|
|
See <<java-aggs,Aggregations Java API>>
|
2013-08-28 19:24:34 -04:00
|
|
|
documentation for details.
|
2014-11-30 15:17:53 -05:00
|
|
|
|
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
[[java-search-terminate-after]]
|
|
|
|
=== Terminate After
|
2014-11-30 15:17:53 -05:00
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
|
|
|
|
If set, you will be able to check if the operation terminated early by asking for `isTerminatedEarly()` in the
|
2018-03-14 13:02:55 -04:00
|
|
|
`SearchResponse` object:
|
2014-11-30 15:17:53 -05:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2015-06-24 17:27:19 -04:00
|
|
|
SearchResponse sr = client.prepareSearch(INDEX)
|
|
|
|
.setTerminateAfter(1000) <1>
|
|
|
|
.get();
|
2014-11-30 15:17:53 -05:00
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
if (sr.isTerminatedEarly()) {
|
|
|
|
// We finished early
|
2014-11-30 15:17:53 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2015-06-24 17:27:19 -04:00
|
|
|
<1> Finish after 1000 docs
|
2016-11-28 11:33:40 -05:00
|
|
|
|
|
|
|
[[java-search-template]]
|
2016-11-30 08:51:06 -05:00
|
|
|
=== Search Template
|
2016-11-28 11:33:40 -05:00
|
|
|
|
|
|
|
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]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2017-04-17 22:16:37 -04:00
|
|
|
"query" : {
|
|
|
|
"match" : {
|
|
|
|
"gender" : "{{param_gender}}"
|
2016-11-28 11:33:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// 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" +
|
2017-04-17 22:16:37 -04:00
|
|
|
" \"query\" : {\n" +
|
|
|
|
" \"match\" : {\n" +
|
|
|
|
" \"gender\" : \"{{param_gender}}\"\n" +
|
2016-11-28 11:33:40 -05:00
|
|
|
" }\n" +
|
|
|
|
" }\n" +
|
|
|
|
"}")).get();
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
To execute a stored templates, use `ScriptService.ScriptType.STORED`:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
SearchResponse sr = new SearchTemplateRequestBuilder(client)
|
2018-11-26 18:05:15 -05:00
|
|
|
.setScript("template_gender") <1>
|
2016-12-12 08:21:10 -05:00
|
|
|
.setScriptType(ScriptType.STORED) <2>
|
2018-11-26 18:05:15 -05:00
|
|
|
.setScriptParams(template_params) <3>
|
|
|
|
.setRequest(new SearchRequest()) <4>
|
|
|
|
.get() <5>
|
|
|
|
.getResponse(); <6>
|
2016-11-28 11:33:40 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
<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)
|
2018-11-26 18:05:15 -05:00
|
|
|
.setScript("{\n" + <1>
|
2016-11-28 11:33:40 -05:00
|
|
|
" \"query\" : {\n" +
|
|
|
|
" \"match\" : {\n" +
|
|
|
|
" \"gender\" : \"{{param_gender}}\"\n" +
|
|
|
|
" }\n" +
|
|
|
|
" }\n" +
|
|
|
|
"}")
|
2016-12-12 08:21:10 -05:00
|
|
|
.setScriptType(ScriptType.INLINE) <2>
|
2018-11-26 18:05:15 -05:00
|
|
|
.setScriptParams(template_params) <3>
|
|
|
|
.setRequest(new SearchRequest()) <4>
|
|
|
|
.get() <5>
|
|
|
|
.getResponse(); <6>
|
2016-11-28 11:33:40 -05:00
|
|
|
--------------------------------------------------
|
2018-11-22 23:29:20 -05:00
|
|
|
<1> template's body
|
2016-11-28 11:33:40 -05:00
|
|
|
<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
|