count.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [[search-count]]
  2. === Count API
  3. The count API allows to easily execute a query and get the number of
  4. matches for that query. It can be executed across one or more indices.
  5. The query can either be provided using a simple query string as a
  6. parameter, or using the <<query-dsl,Query DSL>> defined within the request
  7. body. Here is an example:
  8. [source,console]
  9. --------------------------------------------------
  10. PUT /twitter/_doc/1?refresh
  11. {
  12. "user": "kimchy"
  13. }
  14. GET /twitter/_count?q=user:kimchy
  15. GET /twitter/_count
  16. {
  17. "query" : {
  18. "term" : { "user" : "kimchy" }
  19. }
  20. }
  21. --------------------------------------------------
  22. NOTE: The query being sent in the body must be nested in a `query` key, same as
  23. the <<search-search,search api>> works
  24. Both examples above do the same thing, which is count the number of
  25. tweets from the `twitter` index for a certain user. The result is:
  26. [source,console-result]
  27. --------------------------------------------------
  28. {
  29. "count" : 1,
  30. "_shards" : {
  31. "total" : 1,
  32. "successful" : 1,
  33. "skipped" : 0,
  34. "failed" : 0
  35. }
  36. }
  37. --------------------------------------------------
  38. The query is optional, and when not provided, it will use `match_all` to
  39. count all the docs.
  40. [float]
  41. ==== Multi index
  42. The count API can be applied to <<search-multi-index,multiple indices>>.
  43. [float]
  44. ==== Request Parameters
  45. When executing count using the query parameter `q`, the query passed is
  46. a query string using Lucene query parser. There are additional
  47. parameters that can be passed:
  48. [cols="<,<",options="header",]
  49. |=======================================================================
  50. |Name |Description
  51. |`df` |The default field to use when no field prefix is defined within the
  52. query.
  53. |`analyzer` |The analyzer name to be used when analyzing the query string.
  54. |`default_operator` |The default operator to be used, can be `AND` or
  55. `OR`. Defaults to `OR`.
  56. |`lenient` |If set to true will cause format based failures (like
  57. providing text to a numeric field) to be ignored. Defaults to false.
  58. |`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
  59. not. Defaults to `false`.
  60. |`terminate_after` |The maximum count for each shard, upon
  61. reaching which the query execution will terminate early.
  62. If set, the response will have a boolean field `terminated_early` to
  63. indicate whether the query execution has actually terminated_early.
  64. Defaults to no terminate_after.
  65. |=======================================================================
  66. [float]
  67. ==== Request Body
  68. The count can use the <<query-dsl,Query DSL>> within
  69. its body in order to express the query that should be executed. The body
  70. content can also be passed as a REST parameter named `source`.
  71. Both HTTP GET and HTTP POST can be used to execute count with body.
  72. Since not all clients support GET with body, POST is allowed as well.
  73. [float]
  74. ==== Distributed
  75. The count operation is broadcast across all shards. For each shard id
  76. group, a replica is chosen and executed against it. This means that
  77. replicas increase the scalability of count.
  78. [float]
  79. ==== Routing
  80. The routing value (a comma separated list of the routing values) can be
  81. specified to control which shards the count request will be executed on.