count.asciidoc 3.2 KB

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