Method: Elasticsearch::API::Actions#count

Defined in:
lib/elasticsearch/api/actions/count.rb

#count(arguments = {}) ⇒ Object

Count search results. Get the number of documents matching a query. The query can be provided either by using a simple query string as a parameter, or by defining Query DSL within the request body. The query is optional. When no query is provided, the API uses ‘match_all` to count all the documents. The count API supports multi-target syntax. You can run a single count API search across multiple data streams and indices. The operation is broadcast across all shards. For each shard ID group, a replica is chosen and the search is run against it. This means that replicas increase the scalability of the count.

Parameters:

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments):

  • :index (String, Array)

    A comma-separated list of data streams, indices, and aliases to search. It supports wildcards (‘*`). To search all data streams and indices, omit this parameter or use `*` or `_all`.

  • :allow_no_indices (Boolean)

    If ‘false`, the request returns an error if any wildcard expression, index alias, or `_all` value targets only missing or closed indices. This behavior applies even if the request targets other open indices. For example, a request targeting `foo*,bar*` returns an error if an index starts with `foo` but no index starts with `bar`. Server default: true.

  • :analyzer (String)

    The analyzer to use for the query string. This parameter can be used only when the ‘q` query string parameter is specified.

  • :analyze_wildcard (Boolean)

    If ‘true`, wildcard and prefix queries are analyzed. This parameter can be used only when the `q` query string parameter is specified.

  • :default_operator (String)

    The default operator for query string query: ‘AND` or `OR`. This parameter can be used only when the `q` query string parameter is specified. Server default: OR.

  • :df (String)

    The field to use as a default when no field prefix is given in the query string. This parameter can be used only when the ‘q` query string parameter is specified.

  • :expand_wildcards (String, Array<String>)

    The type of index that wildcard patterns can match. If the request can target data streams, this argument determines whether wildcard expressions match hidden data streams. It supports comma-separated values, such as ‘open,hidden`. Server default: open.

  • :ignore_throttled (Boolean)

    If ‘true`, concrete, expanded, or aliased indices are ignored when frozen. Server default: true.

  • :ignore_unavailable (Boolean)

    If ‘false`, the request returns an error if it targets a missing or closed index.

  • :lenient (Boolean)

    If ‘true`, format-based query failures (such as providing text to a numeric field) in the query string will be ignored. This parameter can be used only when the `q` query string parameter is specified.

  • :min_score (Float)

    The minimum ‘_score` value that documents must have to be included in the result.

  • :preference (String)

    The node or shard the operation should be performed on. By default, it is random.

  • :routing (String)

    A custom value used to route operations to a specific shard.

  • :terminate_after (Integer)

    The maximum number of documents to collect for each shard. If a query reaches this limit, Elasticsearch terminates the query early. Elasticsearch collects documents before sorting.IMPORTANT: Use with caution. Elasticsearch applies this parameter to each shard handling the request. When possible, let Elasticsearch perform early termination automatically. Avoid specifying this parameter for requests that target data streams with backing indices across multiple data tiers.

  • :q (String)

    The query in Lucene query string syntax. This parameter cannot be used with a request body.

  • :error_trace (Boolean)

    When set to ‘true` Elasticsearch will include the full stack trace of errors when they occur.

  • :filter_path (String, Array<String>)

    Comma-separated list of filters in dot notation which reduce the response returned by Elasticsearch.

  • :human (Boolean)

    When set to ‘true` will return statistics in a format suitable for humans. For example `“exists_time”: “1h”` for humans and `“exists_time_in_millis”: 3600000` for computers. When disabled the human readable values will be omitted. This makes sense for responses being consumed only by machines.

  • :pretty (Boolean)

    If set to ‘true` the returned JSON will be “pretty-formatted”. Only use this option for debugging only.

  • :headers (Hash)

    Custom HTTP headers

  • :body (Hash)

    request body

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/elasticsearch/api/actions/count.rb', line 81

def count(arguments = {})
  request_opts = { endpoint: arguments[:endpoint] || 'count' }

  defined_params = [:index].each_with_object({}) do |variable, set_variables|
    set_variables[variable] = arguments[variable] if arguments.key?(variable)
  end
  request_opts[:defined_params] = defined_params unless defined_params.empty?

  arguments = arguments.clone
  headers = arguments.delete(:headers) || {}

  body = arguments.delete(:body)

  _index = arguments.delete(:index)

  method = if body
             Elasticsearch::API::HTTP_POST
           else
             Elasticsearch::API::HTTP_GET
           end

  path   = if _index
             "#{Utils.listify(_index)}/_count"
           else
             '_count'
           end
  params = Utils.process_params(arguments)

  Elasticsearch::API::Response.new(
    perform_request(method, path, params, body, headers, request_opts)
  )
end