sampler-aggregation.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. [[search-aggregations-bucket-sampler-aggregation]]
  2. === Sampler Aggregation
  3. A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents.
  4. .Example use cases:
  5. * Tightening the focus of analytics to high-relevance matches rather than the potentially very long tail of low-quality matches
  6. * Reducing the running cost of aggregations that can produce useful results using only samples e.g. `significant_terms`
  7. Example:
  8. A query on StackOverflow data for the popular term `javascript` OR the rarer term
  9. `kibana` will match many documents - most of them missing the word Kibana. To focus
  10. the `significant_terms` aggregation on top-scoring documents that are more likely to match
  11. the most interesting parts of our query we use a sample.
  12. [source,js]
  13. --------------------------------------------------
  14. POST /stackoverflow/_search?size=0
  15. {
  16. "query": {
  17. "query_string": {
  18. "query": "tags:kibana OR tags:javascript"
  19. }
  20. },
  21. "aggs": {
  22. "sample": {
  23. "sampler": {
  24. "shard_size": 200
  25. },
  26. "aggs": {
  27. "keywords": {
  28. "significant_terms": {
  29. "field": "tags",
  30. "exclude": ["kibana", "javascript"]
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. --------------------------------------------------
  38. // CONSOLE
  39. // TEST[setup:stackoverflow]
  40. Response:
  41. [source,js]
  42. --------------------------------------------------
  43. {
  44. ...
  45. "aggregations": {
  46. "sample": {
  47. "doc_count": 200,<1>
  48. "keywords": {
  49. "doc_count": 200,
  50. "bg_count": 650,
  51. "buckets": [
  52. {
  53. "key": "elasticsearch",
  54. "doc_count": 150,
  55. "score": 1.078125,
  56. "bg_count": 200
  57. },
  58. {
  59. "key": "logstash",
  60. "doc_count": 50,
  61. "score": 0.5625,
  62. "bg_count": 50
  63. }
  64. ]
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  71. <1> 200 documents were sampled in total. The cost of performing the nested significant_terms aggregation was
  72. therefore limited rather than unbounded.
  73. Without the `sampler` aggregation the request query considers the full "long tail" of low-quality matches and therefore identifies
  74. less significant terms such as `jquery` and `angular` rather than focusing on the more insightful Kibana-related terms.
  75. [source,js]
  76. --------------------------------------------------
  77. POST /stackoverflow/_search?size=0
  78. {
  79. "query": {
  80. "query_string": {
  81. "query": "tags:kibana OR tags:javascript"
  82. }
  83. },
  84. "aggs": {
  85. "low_quality_keywords": {
  86. "significant_terms": {
  87. "field": "tags",
  88. "size": 3,
  89. "exclude":["kibana", "javascript"]
  90. }
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. // CONSOLE
  96. // TEST[setup:stackoverflow]
  97. Response:
  98. [source,js]
  99. --------------------------------------------------
  100. {
  101. ...
  102. "aggregations": {
  103. "low_quality_keywords": {
  104. "doc_count": 600,
  105. "bg_count": 650,
  106. "buckets": [
  107. {
  108. "key": "angular",
  109. "doc_count": 200,
  110. "score": 0.02777,
  111. "bg_count": 200
  112. },
  113. {
  114. "key": "jquery",
  115. "doc_count": 200,
  116. "score": 0.02777,
  117. "bg_count": 200
  118. },
  119. {
  120. "key": "logstash",
  121. "doc_count": 50,
  122. "score": 0.0069,
  123. "bg_count": 50
  124. }
  125. ]
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  131. // TESTRESPONSE[s/0.02777/$body.aggregations.low_quality_keywords.buckets.0.score/]
  132. // TESTRESPONSE[s/0.0069/$body.aggregations.low_quality_keywords.buckets.2.score/]
  133. ==== shard_size
  134. The `shard_size` parameter limits how many top-scoring documents are collected in the sample processed on each shard.
  135. The default value is 100.
  136. ==== Limitations
  137. ===== Cannot be nested under `breadth_first` aggregations
  138. Being a quality-based filter the sampler aggregation needs access to the relevance score produced for each document.
  139. It therefore cannot be nested under a `terms` aggregation which has the `collect_mode` switched from the default `depth_first` mode to `breadth_first` as this discards scores.
  140. In this situation an error will be thrown.