count.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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::{docdir}/rest-api/common-parms.asciidoc[tag=index]
  27. [[search-count-api-query-params]]
  28. ==== {api-query-parms-title}
  29. include::{docdir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  30. include::{docdir}/rest-api/common-parms.asciidoc[tag=analyzer]
  31. include::{docdir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  32. include::{docdir}/rest-api/common-parms.asciidoc[tag=default_operator]
  33. include::{docdir}/rest-api/common-parms.asciidoc[tag=df]
  34. include::{docdir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  35. +
  36. Defaults to `open`.
  37. include::{docdir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
  38. include::{docdir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  39. include::{docdir}/rest-api/common-parms.asciidoc[tag=lenient]
  40. `min_score`::
  41. (Optional, float)
  42. Sets the minimum `_score` value that documents must have to be included in the
  43. result.
  44. include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
  45. include::{docdir}/rest-api/common-parms.asciidoc[tag=search-q]
  46. include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
  47. include::{docdir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  48. [[search-count-request-body]]
  49. ==== {api-request-body-title}
  50. include::{docdir}/rest-api/common-parms.asciidoc[tag=query]
  51. [[search-count-api-example]]
  52. ==== {api-examples-title}
  53. [source,console]
  54. --------------------------------------------------
  55. PUT /twitter/_doc/1?refresh
  56. {
  57. "user": "kimchy"
  58. }
  59. GET /twitter/_count?q=user:kimchy
  60. GET /twitter/_count
  61. {
  62. "query" : {
  63. "term" : { "user" : "kimchy" }
  64. }
  65. }
  66. --------------------------------------------------
  67. Both examples above do the same: count the number of tweets from the `twitter`
  68. index for a certain user. The API returns the following response:
  69. [source,console-result]
  70. --------------------------------------------------
  71. {
  72. "count" : 1,
  73. "_shards" : {
  74. "total" : 1,
  75. "successful" : 1,
  76. "skipped" : 0,
  77. "failed" : 0
  78. }
  79. }
  80. --------------------------------------------------
  81. The query is optional, and when not provided, it will use `match_all` to
  82. count all the docs.