count.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. [[search-count]]
  2. === Count API
  3. Gets the number of matches for a search query.
  4. [source,console]
  5. --------------------------------------------------
  6. GET /my-index-000001/_count?q=user:kimchy
  7. --------------------------------------------------
  8. // TEST[setup:my_index]
  9. NOTE: The query being sent in the body must be nested in a `query` key, same as
  10. the <<search-search,search API>> works.
  11. [[search-count-api-request]]
  12. ==== {api-request-title}
  13. `GET /<target>/_count`
  14. [[search-count-api-desc]]
  15. ==== {api-description-title}
  16. The count API allows you to execute a query and get the number of matches for
  17. that query. The query can either
  18. be provided using a simple query string as a parameter, or using the
  19. <<query-dsl,Query DSL>> defined within the request body.
  20. The count API supports <<multi-index,multi-target syntax>>. You can run a single
  21. count API search across multiple data streams and indices.
  22. The operation is broadcast across all shards. For each shard id group, a replica
  23. is chosen and executed against it. This means that replicas increase the
  24. scalability of count.
  25. [[search-count-api-path-params]]
  26. ==== {api-path-parms-title}
  27. `<target>`::
  28. (Optional, string)
  29. Comma-separated list of data streams, indices, and index aliases to search.
  30. Wildcard (`*`) expressions are supported.
  31. +
  32. To search all data streams and indices in a cluster, omit this parameter or use
  33. `_all` or `*`.
  34. [[search-count-api-query-params]]
  35. ==== {api-query-parms-title}
  36. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  37. +
  38. Defaults to `true`.
  39. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyzer]
  40. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  41. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=default_operator]
  42. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=df]
  43. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  44. +
  45. Defaults to `open`.
  46. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=lenient]
  49. `min_score`::
  50. (Optional, float)
  51. Sets the minimum `_score` value that documents must have to be included in the
  52. result.
  53. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  54. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  55. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  56. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  57. [[search-count-request-body]]
  58. ==== {api-request-body-title}
  59. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=query]
  60. [[search-count-api-example]]
  61. ==== {api-examples-title}
  62. [source,console]
  63. --------------------------------------------------
  64. PUT /my-index-000001/_doc/1?refresh
  65. {
  66. "user.id": "kimchy"
  67. }
  68. GET /my-index-000001/_count?q=user:kimchy
  69. GET /my-index-000001/_count
  70. {
  71. "query" : {
  72. "term" : { "user.id" : "kimchy" }
  73. }
  74. }
  75. --------------------------------------------------
  76. Both examples above do the same: count the number of documents in
  77. `my-index-000001` with a `user.id` of `kimchy`. The API returns the following response:
  78. [source,console-result]
  79. --------------------------------------------------
  80. {
  81. "count": 1,
  82. "_shards": {
  83. "total": 1,
  84. "successful": 1,
  85. "skipped": 0,
  86. "failed": 0
  87. }
  88. }
  89. --------------------------------------------------
  90. The query is optional, and when not provided, it will use `match_all` to
  91. count all the docs.