count.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/tweet/1?refresh
  12. {
  13. "user": "kimchy"
  14. }
  15. GET /twitter/tweet/_count?q=user:kimchy
  16. GET /twitter/tweet/_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" : 5,
  34. "successful" : 5,
  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, Multi type
  44. The count API can be applied to <<search-multi-index-type,multiple types in 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. |`lowercase_expanded_terms` |Should terms be automatically lowercased or
  61. not. Defaults to `true`.
  62. |`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
  63. not. Defaults to `false`.
  64. |`terminate_after` |The maximum count for each shard, upon
  65. reaching which the query execution will terminate early.
  66. If set, the response will have a boolean field `terminated_early` to
  67. indicate whether the query execution has actually terminated_early.
  68. Defaults to no terminate_after.
  69. |=======================================================================
  70. [float]
  71. === Request Body
  72. The count can use the <<query-dsl,Query DSL>> within
  73. its body in order to express the query that should be executed. The body
  74. content can also be passed as a REST parameter named `source`.
  75. Both HTTP GET and HTTP POST can be used to execute count with body.
  76. Since not all clients support GET with body, POST is allowed as well.
  77. [float]
  78. === Distributed
  79. The count operation is broadcast across all shards. For each shard id
  80. group, a replica is chosen and executed against it. This means that
  81. replicas increase the scalability of count.
  82. [float]
  83. === Routing
  84. The routing value (a comma separated list of the routing values) can be
  85. specified to control which shards the count request will be executed on.