2013-08-28 19:24:34 -04:00
|
|
|
[[search-count]]
|
2019-07-19 14:35:36 -04:00
|
|
|
=== Count API
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-17 03:53:03 -04:00
|
|
|
Gets the number of matches for a search query.
|
|
|
|
|
|
|
|
[source,console]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET /twitter/_count?q=user:kimchy
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[setup:twitter]
|
|
|
|
|
|
|
|
NOTE: The query being sent in the body must be nested in a `query` key, same as
|
|
|
|
the <<search-search,search api>> works.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-count-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
2019-11-12 09:44:57 -05:00
|
|
|
`GET /<index>/_count`
|
2019-09-17 03:53:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
[[search-count-api-desc]]
|
|
|
|
==== {api-description-title}
|
|
|
|
|
|
|
|
The count API allows you to execute a query and get the number of matches for
|
|
|
|
that query. It can be executed across one or more indices. 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.
|
|
|
|
|
|
|
|
The count API can be applied to <<search-multi-index,multiple indices>>.
|
|
|
|
|
|
|
|
The 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.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-count-api-path-params]]
|
|
|
|
==== {api-path-parms-title}
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-count-api-query-params]]
|
|
|
|
==== {api-query-parms-title}
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=analyzer]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=default_operator]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=df]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
|
|
|
|
+
|
|
|
|
Defaults to `open`.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=lenient]
|
|
|
|
|
|
|
|
`min_score`::
|
|
|
|
(Optional, float)
|
|
|
|
Sets the minimum `_score` value that documents must have to be included in the
|
|
|
|
result.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=search-q]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=terminate_after]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-count-request-body]]
|
|
|
|
==== {api-request-body-title}
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=query]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-count-api-example]]
|
|
|
|
==== {api-examples-title}
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
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
|
|
|
|
2018-11-16 16:04:43 -05:00
|
|
|
GET /twitter/_count?q=user:kimchy
|
2016-05-18 07:36:19 -04:00
|
|
|
|
2018-11-16 16:04:43 -05:00
|
|
|
GET /twitter/_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
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-09-17 03:53:03 -04:00
|
|
|
Both examples above do the same: count the number of tweets from the `twitter`
|
|
|
|
index for a certain user. The API returns the following response:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-06 09:22:08 -04:00
|
|
|
[source,console-result]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"count" : 1,
|
|
|
|
"_shards" : {
|
2018-05-14 12:22:35 -04:00
|
|
|
"total" : 1,
|
|
|
|
"successful" : 1,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2013-08-28 19:24:34 -04:00
|
|
|
"failed" : 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
The query is optional, and when not provided, it will use `match_all` to
|
|
|
|
count all the docs.
|