count.asciidoc 3.7 KB

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