count.asciidoc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $ curl -XGET 'http://localhost:9200/twitter/tweet/_count?q=user:kimchy'
  12. $ curl -XGET 'http://localhost:9200/twitter/tweet/_count' -d '
  13. {
  14. "query" : {
  15. "term" : { "user" : "kimchy" }
  16. }
  17. }'
  18. --------------------------------------------------
  19. NOTE: The query being sent in the body must be nested in a `query` key, same as
  20. the <<search-search,search api>> works added[1.0.0.RC1,The query was previously the top-level object].
  21. Both examples above do the same thing, which is count the number of
  22. tweets from the twitter index for a certain user. The result is:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. "count" : 1,
  27. "_shards" : {
  28. "total" : 5,
  29. "successful" : 5,
  30. "failed" : 0
  31. }
  32. }
  33. --------------------------------------------------
  34. The query is optional, and when not provided, it will use `match_all` to
  35. count all the docs.
  36. [float]
  37. === Multi index, Multi type
  38. The count API can be applied to <<search-multi-index-type,multiple types in multiple indices>>.
  39. [float]
  40. === Request Parameters
  41. When executing count using the query parameter `q`, the query passed is
  42. a query string using Lucene query parser. There are additional
  43. parameters that can be passed:
  44. [cols="<,<",options="header",]
  45. |=======================================================================
  46. |Name |Description
  47. |df |The default field to use when no field prefix is defined within the
  48. query.
  49. |analyzer |The analyzer name to be used when analyzing the query string.
  50. |default_operator |The default operator to be used, can be `AND` or
  51. `OR`. Defaults to `OR`.
  52. |=======================================================================
  53. [float]
  54. === Request Body
  55. The count can use the <<query-dsl,Query DSL>> within
  56. its body in order to express the query that should be executed. The body
  57. content can also be passed as a REST parameter named `source`.
  58. Both HTTP GET and HTTP POST can be used to execute count with body.
  59. Since not all clients support GET with body, POST is allowed as well.
  60. [float]
  61. === Distributed
  62. The count operation is broadcast across all shards. For each shard id
  63. group, a replica is chosen and executed against it. This means that
  64. replicas increase the scalability of count.
  65. [float]
  66. === Routing
  67. The routing value (a comma separated list of the routing values) can be
  68. specified to control which shards the count request will be executed on.