quickly-check-for-matching-docs.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. [discrete]
  2. [[quickly-check-for-matching-docs]]
  3. === Quickly check for matching docs
  4. If you only want to know if there are any documents matching a
  5. specific query, you can set the `size` to `0` to indicate that we are not
  6. interested in the search results. You can also set `terminate_after` to `1`
  7. to indicate that the query execution can be terminated whenever the first
  8. matching document was found (per shard).
  9. [source,console]
  10. --------------------------------------------------
  11. GET /_search?q=user.id:elkbee&size=0&terminate_after=1
  12. --------------------------------------------------
  13. // TEST[setup:my_index]
  14. NOTE: `terminate_after` is always applied **after** the
  15. <<post-filter,`post_filter`>> and stops the query as well as the aggregation
  16. executions when enough hits have been collected on the shard. Though the doc
  17. count on aggregations may not reflect the `hits.total` in the response since
  18. aggregations are applied **before** the post filtering.
  19. The response will not contain any hits as the `size` was set to `0`. The
  20. `hits.total` will be either equal to `0`, indicating that there were no
  21. matching documents, or greater than `0` meaning that there were at least
  22. as many documents matching the query when it was early terminated.
  23. Also if the query was terminated early, the `terminated_early` flag will
  24. be set to `true` in the response.
  25. [source,console-result]
  26. --------------------------------------------------
  27. {
  28. "took": 3,
  29. "timed_out": false,
  30. "terminated_early": true,
  31. "_shards": {
  32. "total": 1,
  33. "successful": 1,
  34. "skipped" : 0,
  35. "failed": 0
  36. },
  37. "hits": {
  38. "total" : {
  39. "value": 1,
  40. "relation": "eq"
  41. },
  42. "max_score": null,
  43. "hits": []
  44. }
  45. }
  46. --------------------------------------------------
  47. // TESTRESPONSE[s/"took": 3/"took": $body.took/]
  48. The `took` time in the response contains the milliseconds that this request
  49. took for processing, beginning quickly after the node received the query, up
  50. until all search related work is done and before the above JSON is returned
  51. to the client. This means it includes the time spent waiting in thread pools,
  52. executing a distributed search across the whole cluster and gathering all the
  53. results.