Count

Introduced 1.0

The count API gives you quick access to the number of documents that match a query. You can also use it to check the document count of an index, data stream, or cluster.

Example

To see the number of documents that match a query:

  1. GET opensearch_dashboards_sample_data_logs/_count
  2. {
  3. "query": {
  4. "term": {
  5. "response": "200"
  6. }
  7. }
  8. }

copy

The following call to the search API produces equivalent results:

  1. GET opensearch_dashboards_sample_data_logs/_search
  2. {
  3. "query": {
  4. "term": {
  5. "response": "200"
  6. }
  7. },
  8. "size": 0,
  9. "track_total_hits": true
  10. }

copy

To see the number of documents in an index:

  1. GET opensearch_dashboards_sample_data_logs/_count

copy

To check for the number of documents in a data stream, replace the index name with the data stream name.

To see the number of documents in your cluster:

  1. GET _count

copy

Alternatively, you could use the cat indices and cat count APIs to see the number of documents per index or data stream.

Path and HTTP methods

  1. GET <target>/_count/<id>
  2. POST <target>/_count/<id>

URL parameters

All count parameters are optional.

ParameterTypeDescription
allow_no_indicesBooleanIf false, the request returns an error if any wildcard expression or index alias targets any closed or missing indices. Default is false.
analyzerStringThe analyzer to use in the query string.
analyze_wildcardBooleanSpecifies whether to analyze wildcard and prefix queries. Default is false.
default_operatorStringIndicates whether the default operator for a string query should be AND or OR. Default is OR.
dfStringThe default field in case a field prefix is not provided in the query string.
expand_wildcardsStringSpecifies the type of index that wildcard expressions can match. Supports comma-separated values. Valid values are all (match any index), open (match open, non-hidden indices), closed (match closed, non-hidden indices), hidden (match hidden indices), and none (deny wildcard expressions). Default is open.
ignore_unavailableBooleanSpecifies whether to include missing or closed indices in the response. Default is false.
lenientBooleanSpecifies whether OpenSearch should accept requests if queries have format errors (for example, querying a text field for an integer). Default is false.
min_scoreFloatInclude only documents with a minimum _score value in the result.
routingStringValue used to route the operation to a specific shard.
preferenceStringSpecifies which shard or node OpenSearch should perform the count operation on.
terminate_afterIntegerThe maximum number of documents OpenSearch should process before terminating the request.

Response

  1. {
  2. "count" : 14074,
  3. "_shards" : {
  4. "total" : 1,
  5. "successful" : 1,
  6. "skipped" : 0,
  7. "failed" : 0
  8. }
  9. }