count.asciidoc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --
  2. :api: count
  3. :request: CountRequest
  4. :response: CountResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Count API
  8. [id="{upid}-{api}-request"]
  9. ==== Count Request
  10. The +{request}+ is used to execute a query and get the number of matches for the query. The query to use in +{request}+ can be
  11. set in similar way as query in `SearchRequest` using `SearchSourceBuilder`.
  12. In its most basic form, we can add a query to the request:
  13. ["source","java",subs="attributes,callouts,macros"]
  14. --------------------------------------------------
  15. include-tagged::{doc-tests-file}[{api}-request-basic]
  16. --------------------------------------------------
  17. <1> Creates the +{request}+. Without arguments this runs against all indices.
  18. <2> Most search parameters are added to the `SearchSourceBuilder`.
  19. <3> Add a `match_all` query to the `SearchSourceBuilder`.
  20. <4> Add the `SearchSourceBuilder` to the +{request}+.
  21. [[java-rest-high-count-request-optional]]
  22. ===== Count Request optional arguments
  23. A +{request}+ also takes the following optional arguments:
  24. ["source","java",subs="attributes,callouts,macros"]
  25. --------------------------------------------------
  26. include-tagged::{doc-tests-file}[{api}-request-args]
  27. --------------------------------------------------
  28. <1> Restricts the request to an index
  29. <2> Set a routing parameter
  30. <3> Setting `IndicesOptions` controls how unavailable indices are resolved and how wildcard expressions are expanded
  31. <4> Use the preference parameter e.g. to execute the search to prefer local shards. The default is to randomize across shards.
  32. ===== Using the SearchSourceBuilder in CountRequest
  33. Both in search and count API calls, most options controlling the search behavior can be set on the `SearchSourceBuilder`,
  34. which contains more or less the equivalent of the options in the search request body of the Rest API.
  35. Here are a few examples of some common options:
  36. ["source","java",subs="attributes,callouts,macros"]
  37. --------------------------------------------------
  38. include-tagged::{doc-tests-file}[{api}-source-basics]
  39. --------------------------------------------------
  40. <1> Create a `SearchSourceBuilder` with default options.
  41. <2> Set the query. Can be any type of `QueryBuilder`
  42. After this, the `SearchSourceBuilder` only needs to be added to the
  43. +{request}+:
  44. ["source","java",subs="attributes,callouts,macros"]
  45. --------------------------------------------------
  46. include-tagged::{doc-tests-file}[{api}-source-setter]
  47. --------------------------------------------------
  48. Note subtle difference when using `SearchSourceBuilder` in `SearchRequest` and using `SearchSourceBuilder` in +{request}+ - using
  49. `SearchSourceBuilder` in `SearchRequest` one can use `SearchSourceBuilder.size()` and `SearchSourceBuilder.from()` methods to set the
  50. number of search hits to return, and the starting index. In +{request}+ we're interested in total number of matches and these methods
  51. have no meaning.
  52. The <<java-rest-high-query-builders, Building Queries>> page gives a list of all available search queries with
  53. their corresponding `QueryBuilder` objects and `QueryBuilders` helper methods.
  54. include::../execution.asciidoc[]
  55. [id="{upid}-{api}-response"]
  56. ==== CountResponse
  57. The +{response}+ that is returned by executing the count API call provides total count of hits and details about the count execution
  58. itself, like the HTTP status code, or whether the request terminated early:
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests-file}[{api}-response-1]
  62. --------------------------------------------------
  63. The response also provides information about the execution on the
  64. shard level by offering statistics about the total number of shards that were
  65. affected by the underlying search, and the successful vs. unsuccessful shards. Possible
  66. failures can also be handled by iterating over an array off
  67. `ShardSearchFailures` like in the following example:
  68. ["source","java",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{doc-tests-file}[{api}-response-2]
  71. --------------------------------------------------