count.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  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. |`lenient` |If set to true will cause format based failures (like
  53. providing text to a numeric field) to be ignored. Defaults to false.
  54. |`lowercase_expanded_terms` |Should terms be automatically lowercased or
  55. not. Defaults to `true`.
  56. |`analyze_wildcard` |Should wildcard and prefix queries be analyzed or
  57. not. Defaults to `false`.
  58. |`terminate_after` |experimental[The API for this feature may change in the future]
  59. The maximum count for each shard, upon
  60. reaching which the query execution will terminate early.
  61. If set, the response will have a boolean field `terminated_early` to
  62. indicate whether the query execution has actually terminated_early.
  63. Defaults to no terminate_after.
  64. |=======================================================================
  65. [float]
  66. === Request Body
  67. The count can use the <<query-dsl,Query DSL>> within
  68. its body in order to express the query that should be executed. The body
  69. content can also be passed as a REST parameter named `source`.
  70. Both HTTP GET and HTTP POST can be used to execute count with body.
  71. Since not all clients support GET with body, POST is allowed as well.
  72. [float]
  73. === Distributed
  74. The count operation is broadcast across all shards. For each shard id
  75. group, a replica is chosen and executed against it. This means that
  76. replicas increase the scalability of count.
  77. [float]
  78. === Routing
  79. The routing value (a comma separated list of the routing values) can be
  80. specified to control which shards the count request will be executed on.