WARNING: Version 2.3 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Search Exists API
editSearch Exists API
editDeprecated in 2.1.0.
use regular _search
with size
set to 0
and terminate_after
set to 1
instead
The exists API allows to easily determine if any matching documents exist for a provided query. It can be executed across one or more indices and across one or more types. The query can either be provided using a simple query string as a parameter, or using the Query DSL defined within the request body. Here is an example:
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search/exists?q=user:kimchy' $ curl -XGET 'http://localhost:9200/twitter/tweet/_search/exists' -d ' { "query" : { "term" : { "user" : "kimchy" } } }'
The query being sent in the body must be nested in a query
key, same as
how the search api works.
Both the examples above do the same thing, which is determine the existence of tweets from the twitter index for a certain user. The response body will be of the following format:
{ "exists" : true }
Multi index, Multi type
editThe exists API can be applied to multiple types in multiple indices.
Request Parameters
editWhen executing exists using the query parameter q
, the query passed is
a query string using Lucene query parser. There are additional
parameters that can be passed:
Name | Description |
---|---|
|
The default field to use when no field prefix is defined within the query. |
|
The analyzer name to be used when analyzing the query string. |
|
The default operator to be used, can be |
|
If set to true will cause format based failures (like providing text to a numeric field) to be ignored. Defaults to false. |
|
Should terms be automatically lowercased or
not. Defaults to |
|
Should wildcard and prefix queries be analyzed or
not. Defaults to |
Request Body
editThe exists API can use the Query DSL within
its body in order to express the query that should be executed. The body
content can also be passed as a REST parameter named source
.
HTTP GET and HTTP POST can be used to execute exists with body. Since not all clients support GET with body, POST is allowed as well.
Distributed
editThe exists operation is broadcast across all shards. For each shard id group, a replica is chosen and executed against it. This means that replicas increase the scalability of exists. The exists operation also early terminates shard requests once the first shard reports matched document existence.
Routing
editThe routing value (a comma separated list of the routing values) can be specified to control which shards the exists request will be executed on.