count.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. [[search-count]]
  2. === Count API
  3. Gets the number of matches for a search query.
  4. [source,console]
  5. --------------------------------------------------
  6. GET /twitter/_count?q=user:kimchy
  7. --------------------------------------------------
  8. // TEST[setup:twitter]
  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 /<index>/_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. It can be executed across one or more indices. 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 can be applied to <<search-multi-index,multiple indices>>.
  21. The operation is broadcast across all shards. For each shard id group, a replica
  22. is chosen and executed against it. This means that replicas increase the
  23. scalability of count.
  24. [[search-count-api-path-params]]
  25. ==== {api-path-parms-title}
  26. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index]
  27. [[search-count-api-query-params]]
  28. ==== {api-query-parms-title}
  29. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  30. +
  31. Defaults to `true`.
  32. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyzer]
  33. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  34. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=default_operator]
  35. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=df]
  36. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  37. +
  38. Defaults to `open`.
  39. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
  40. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  41. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=lenient]
  42. `min_score`::
  43. (Optional, float)
  44. Sets the minimum `_score` value that documents must have to be included in the
  45. result.
  46. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  49. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  50. [[search-count-request-body]]
  51. ==== {api-request-body-title}
  52. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=query]
  53. [[search-count-api-example]]
  54. ==== {api-examples-title}
  55. [source,console]
  56. --------------------------------------------------
  57. PUT /twitter/_doc/1?refresh
  58. {
  59. "user": "kimchy"
  60. }
  61. GET /twitter/_count?q=user:kimchy
  62. GET /twitter/_count
  63. {
  64. "query" : {
  65. "term" : { "user" : "kimchy" }
  66. }
  67. }
  68. --------------------------------------------------
  69. Both examples above do the same: count the number of tweets from the `twitter`
  70. index for a certain user. The API returns the following response:
  71. [source,console-result]
  72. --------------------------------------------------
  73. {
  74. "count" : 1,
  75. "_shards" : {
  76. "total" : 1,
  77. "successful" : 1,
  78. "skipped" : 0,
  79. "failed" : 0
  80. }
  81. }
  82. --------------------------------------------------
  83. The query is optional, and when not provided, it will use `match_all` to
  84. count all the docs.