原文地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-count.html, 原文档版权归 www.elastic.co 所有

Count APIedit

Gets the number of matches for a search query.

GET /twitter/_count?q=user:kimchy

The query being sent in the body must be nested in a query key, same as the search api works.

Requestedit

GET /<index>/_count

Descriptionedit

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 defined within the request body.

The count API can be applied to 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.

Path parametersedit

<index>
(Optional, string) Comma-separated list or wildcard expression of index names used to limit the request.

Query parametersedit

allow_no_indices

(Optional, boolean) If true, the request does not return an error if a wildcard expression or _all value retrieves only missing or closed indices.

This parameter also applies to index aliases that point to a missing or closed index.

Defaults to true.

analyzer
(Optional, string) Analyzer to use for the query string.
analyze_wildcard
(Optional, boolean) If true, wildcard and prefix queries are analyzed. Defaults to false.
default_operator
(Optional, string) The default operator for query string query: AND or OR. Defaults to OR.
df
(Optional, string) Field to use as default where no field prefix is given in the query string.
expand_wildcards

(Optional, string) Controls what kind of indices that wildcard expressions can expand to. Multiple values are accepted when separated by a comma, as in open,hidden. Valid values are:

all
Expand to open and closed indices, including hidden indices.
open
Expand only to open indices.
closed
Expand only to closed indices.
hidden
Expansion of wildcards will include hidden indices. Must be combined with open, closed, or both.
none
Wildcard expressions are not accepted.

Defaults to open.

ignore_throttled
(Optional, boolean) If true, concrete, expanded or aliased indices are ignored when throttled.
ignore_unavailable
(Optional, boolean) If true, missing or closed indices are not included in the response. Defaults to false.
lenient
(Optional, boolean) If true, format-based query failures (such as providing text to a numeric field) will be ignored. Defaults to false.
min_score
(Optional, float) Sets the minimum _score value that documents must have to be included in the result.
preference
(Optional, string) Specifies the node or shard the operation should be performed on. Random by default.
q
(Optional, string) Query in the Lucene query string syntax.
routing
(Optional, string) Target the specified primary shard.
terminate_after
(Optional, integer) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.

Request bodyedit

query
(Optional, query object) Defines the search definition using the Query DSL.

Examplesedit

PUT /twitter/_doc/1?refresh
{
    "user": "kimchy"
}

GET /twitter/_count?q=user:kimchy

GET /twitter/_count
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

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:

{
    "count" : 1,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
    }
}

The query is optional, and when not provided, it will use match_all to count all the docs.