| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 | [[search]]= Search APIs[partintro]--Most search APIs are <<search-multi-index-type,multi-index, multi-type>>, with theexception of the <<search-explain>> endpoints.[float][[search-routing]]== RoutingWhen executing a search, Elasticsearch will pick the "best" copy of the databased on the <<search-adaptive-replica,adaptive replica selection>> formula.Which shards will be searched on can also be controlled by providing the`routing` parameter. For example, when indexing tweets, the routing value can bethe user name:[source,js]--------------------------------------------------POST /twitter/tweet?routing=kimchy{    "user" : "kimchy",    "postDate" : "2009-11-15T14:12:12",    "message" : "trying out Elasticsearch"}--------------------------------------------------// CONSOLEIn such a case, if we want to search only on the tweets for a specificuser, we can specify it as the routing, resulting in the search hittingonly the relevant shard:[source,js]--------------------------------------------------POST /twitter/_search?routing=kimchy{    "query": {        "bool" : {            "must" : {                "query_string" : {                    "query" : "some query string here"                }            },            "filter" : {                "term" : { "user" : "kimchy" }            }        }    }}--------------------------------------------------// CONSOLE// TEST[continued]The routing parameter can be multi valued represented as a commaseparated string. This will result in hitting the relevant shards wherethe routing values match to.[float][[search-adaptive-replica]]== Adaptive Replica SelectionBy default, Elasticsearch will use what is called adaptive replica selection.This allows the coordinating node to send the request to the copy deemed "best"based on a number of criteria:- Response time of past requests between the coordinating node and the node  containing the copy of the data- Time past search requests took to execute on the node containing the data- The queue size of the search threadpool on the node containing the dataThis can be turned off by changing the dynamic cluster setting`cluster.routing.use_adaptive_replica_selection` from `true` to `false`:[source,js]--------------------------------------------------PUT /_cluster/settings{    "transient": {        "cluster.routing.use_adaptive_replica_selection": false    }}--------------------------------------------------// CONSOLEIf adaptive replica selection is turned off, searches are sent to theindex/indices shards in a round robin fashion between all copies of the data(primaries and replicas).[float][[stats-groups]]== Stats GroupsA search can be associated with stats groups, which maintains astatistics aggregation per group. It can later be retrieved using the<<indices-stats,indices stats>> APIspecifically. For example, here is a search body request that associatethe request with two different groups:[source,js]--------------------------------------------------POST /_search{    "query" : {        "match_all" : {}    },    "stats" : ["group1", "group2"]}--------------------------------------------------// CONSOLE// TEST[setup:twitter][float][[global-search-timeout]]== Global Search TimeoutIndividual searches can have a timeout as part of the<<search-request-body>>. Since search requests can originate from manysources, Elasticsearch has a dynamic cluster-level setting for a globalsearch timeout that applies to all search requests that do not set atimeout in the <<search-request-body>>. The default value is no globaltimeout. The setting key is `search.default_search_timeout` and can beset using the <<cluster-update-settings>> endpoints. Setting this valueto `-1` resets the global search timeout to no timeout.[float][[global-search-cancellation]]== Search CancellationSearches can be cancelled using standard <<task-cancellation,task cancellation>>mechanism. By default, a running search only checks if it is cancelled ornot on segment boundaries, therefore the cancellation can be delayed by largesegments. The search cancellation responsiveness can be improved by settingthe dynamic cluster-level setting `search.low_level_cancellation` to `true`.However, it comes with an additional overhead of more frequent cancellationchecks that can be noticeable on large fast running search queries. Changing thissetting only affects the searches that start after the change is made.[float][[search-concurrency-and-parallelism]]== Search concurrency and parallelismBy default Elasticsearch doesn't reject any search requests based on the numberof shards the request hits. While Elasticsearch will optimize the searchexecution on the coordinating node a large number of shards can have asignificant impact CPU and memory wise. It is usually a better idea to organizedata in such a way that there are fewer larger shards. In case you would like toconfigure a soft limit, you can update the `action.search.shard_count.limit`cluster setting in order to reject search requests that hit too many shards.The request parameter `max_concurrent_shard_requests` can be used to control themaximum number of concurrent shard requests the search API will execute for therequest. This parameter should be used to protect a single request fromoverloading a cluster (e.g., a default request will hit all indices in a clusterwhich could cause shard request rejections if the number of shards per node ishigh). This default is based on the number of data nodes in the cluster but atmost `256`.--include::search/search.asciidoc[]include::search/uri-request.asciidoc[]include::search/request-body.asciidoc[]include::search/search-template.asciidoc[]include::search/search-shards.asciidoc[]include::search/suggesters.asciidoc[]include::search/multi-search.asciidoc[]include::search/count.asciidoc[]include::search/validate.asciidoc[]include::search/explain.asciidoc[]include::search/profile.asciidoc[]include::search/field-caps.asciidoc[]include::search/rank-eval.asciidoc[]
 |