count.asciidoc 3.2 KB

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