2013-08-28 19:24:34 -04:00
|
|
|
[[search-count]]
|
|
|
|
== Count API
|
|
|
|
|
|
|
|
The count API allows to easily execute a query and get the number of
|
|
|
|
matches for that 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,Query DSL>> defined within the request
|
|
|
|
body. Here is an example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /twitter/_doc/1?refresh
|
2016-05-18 07:36:19 -04:00
|
|
|
{
|
|
|
|
"user": "kimchy"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /twitter/_doc/_count?q=user:kimchy
|
2016-05-18 07:36:19 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /twitter/_doc/_count
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2014-02-13 07:36:20 -05:00
|
|
|
"query" : {
|
|
|
|
"term" : { "user" : "kimchy" }
|
|
|
|
}
|
2016-05-18 07:36:19 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-18 07:36:19 -04:00
|
|
|
//CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-02-13 05:30:13 -05:00
|
|
|
NOTE: The query being sent in the body must be nested in a `query` key, same as
|
2014-09-26 15:04:42 -04:00
|
|
|
the <<search-search,search api>> works
|
2014-02-13 05:30:13 -05:00
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
Both examples above do the same thing, which is count the number of
|
|
|
|
tweets from the twitter index for a certain user. The result is:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"count" : 1,
|
|
|
|
"_shards" : {
|
|
|
|
"total" : 5,
|
|
|
|
"successful" : 5,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2013-08-28 19:24:34 -04:00
|
|
|
"failed" : 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-18 07:36:19 -04:00
|
|
|
// TESTRESPONSE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The query is optional, and when not provided, it will use `match_all` to
|
|
|
|
count all the docs.
|
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Multi index, Multi type
|
|
|
|
|
|
|
|
The count API can be applied to <<search-multi-index-type,multiple types in multiple indices>>.
|
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Request Parameters
|
|
|
|
|
|
|
|
When executing count using the query parameter `q`, the query passed is
|
|
|
|
a query string using Lucene query parser. There are additional
|
|
|
|
parameters that can be passed:
|
|
|
|
|
|
|
|
[cols="<,<",options="header",]
|
|
|
|
|=======================================================================
|
|
|
|
|Name |Description
|
2015-05-11 06:58:12 -04:00
|
|
|
|`df` |The default field to use when no field prefix is defined within the
|
2013-08-28 19:24:34 -04:00
|
|
|
query.
|
|
|
|
|
2015-05-11 06:58:12 -04:00
|
|
|
|`analyzer` |The analyzer name to be used when analyzing the query string.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2015-05-11 06:58:12 -04:00
|
|
|
|`default_operator` |The default operator to be used, can be `AND` or
|
2013-08-28 19:24:34 -04:00
|
|
|
`OR`. Defaults to `OR`.
|
2014-07-18 10:53:20 -04:00
|
|
|
|
2015-05-11 06:58:12 -04:00
|
|
|
|`lenient` |If set to true will cause format based failures (like
|
|
|
|
providing text to a numeric field) to be ignored. Defaults to false.
|
|
|
|
|
|
|
|
|`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
|
|
|
|
not. Defaults to `false`.
|
|
|
|
|
2015-10-19 03:48:54 -04:00
|
|
|
|`terminate_after` |The maximum count for each shard, upon
|
2014-07-18 10:53:20 -04:00
|
|
|
reaching which the query execution will terminate early.
|
|
|
|
If set, the response will have a boolean field `terminated_early` to
|
|
|
|
indicate whether the query execution has actually terminated_early.
|
|
|
|
Defaults to no terminate_after.
|
2013-08-28 19:24:34 -04:00
|
|
|
|=======================================================================
|
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Request Body
|
|
|
|
|
|
|
|
The count can use the <<query-dsl,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`.
|
|
|
|
|
|
|
|
Both HTTP GET and HTTP POST can be used to execute count with body.
|
|
|
|
Since not all clients support GET with body, POST is allowed as well.
|
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Distributed
|
|
|
|
|
|
|
|
The count 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 count.
|
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Routing
|
|
|
|
|
|
|
|
The routing value (a comma separated list of the routing values) can be
|
|
|
|
specified to control which shards the count request will be executed on.
|