misc.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. [[caching-heavy-aggregations]]
  2. == Caching heavy aggregations
  3. Frequently used aggregations (e.g. for display on the home page of a website)
  4. can be cached for faster responses. These cached results are the same results
  5. that would be returned by an uncached aggregation -- you will never get stale
  6. results.
  7. See <<shard-request-cache>> for more details.
  8. [[returning-only-agg-results]]
  9. == Returning only aggregation results
  10. There are many occasions when aggregations are required but search hits are not. For these cases the hits can be ignored by
  11. setting `size=0`. For example:
  12. [source,console,id=returning-only-agg-results-example]
  13. --------------------------------------------------
  14. GET /twitter/_search
  15. {
  16. "size": 0,
  17. "aggregations": {
  18. "my_agg": {
  19. "terms": {
  20. "field": "text"
  21. }
  22. }
  23. }
  24. }
  25. --------------------------------------------------
  26. // TEST[setup:twitter]
  27. Setting `size` to `0` avoids executing the fetch phase of the search making the request more efficient.
  28. [[agg-metadata]]
  29. == Aggregation Metadata
  30. You can associate a piece of metadata with individual aggregations at request time that will be returned in place
  31. at response time.
  32. Consider this example where we want to associate the color blue with our `terms` aggregation.
  33. [source,console,id=agg-metadata-example]
  34. --------------------------------------------------
  35. GET /twitter/_search
  36. {
  37. "size": 0,
  38. "aggs": {
  39. "titles": {
  40. "terms": {
  41. "field": "title"
  42. },
  43. "meta": {
  44. "color": "blue"
  45. }
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. // TEST[setup:twitter]
  51. Then that piece of metadata will be returned in place for our `titles` terms aggregation
  52. [source,console-result]
  53. --------------------------------------------------
  54. {
  55. "aggregations": {
  56. "titles": {
  57. "meta": {
  58. "color" : "blue"
  59. },
  60. "doc_count_error_upper_bound" : 0,
  61. "sum_other_doc_count" : 0,
  62. "buckets": [
  63. ]
  64. }
  65. },
  66. ...
  67. }
  68. --------------------------------------------------
  69. // TESTRESPONSE[s/\.\.\./"took": "$body.took", "timed_out": false, "_shards": "$body._shards", "hits": "$body.hits"/]
  70. [[returning-aggregation-type]]
  71. == Returning the type of the aggregation
  72. Sometimes you need to know the exact type of an aggregation in order to parse its results. The `typed_keys` parameter
  73. can be used to change the aggregation's name in the response so that it will be prefixed by its internal type.
  74. Considering the following <<search-aggregations-bucket-datehistogram-aggregation,`date_histogram` aggregation>> named
  75. `tweets_over_time` which has a sub <<search-aggregations-metrics-top-hits-aggregation, 'top_hits` aggregation>> named
  76. `top_users`:
  77. [source,console,id=returning-aggregation-type-example]
  78. --------------------------------------------------
  79. GET /twitter/_search?typed_keys
  80. {
  81. "aggregations": {
  82. "tweets_over_time": {
  83. "date_histogram": {
  84. "field": "date",
  85. "calendar_interval": "year"
  86. },
  87. "aggregations": {
  88. "top_users": {
  89. "top_hits": {
  90. "size": 1
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. --------------------------------------------------
  98. // TEST[setup:twitter]
  99. In the response, the aggregations names will be changed to respectively `date_histogram#tweets_over_time` and
  100. `top_hits#top_users`, reflecting the internal types of each aggregation:
  101. [source,console-result]
  102. --------------------------------------------------
  103. {
  104. "aggregations": {
  105. "date_histogram#tweets_over_time": { <1>
  106. "buckets" : [
  107. {
  108. "key_as_string" : "2009-01-01T00:00:00.000Z",
  109. "key" : 1230768000000,
  110. "doc_count" : 5,
  111. "top_hits#top_users" : { <2>
  112. "hits" : {
  113. "total" : {
  114. "value": 5,
  115. "relation": "eq"
  116. },
  117. "max_score" : 1.0,
  118. "hits" : [
  119. {
  120. "_index": "twitter",
  121. "_id": "0",
  122. "_score": 1.0,
  123. "_source": {
  124. "date": "2009-11-15T14:12:12",
  125. "message": "trying out Elasticsearch",
  126. "user": "kimchy",
  127. "likes": 0
  128. }
  129. }
  130. ]
  131. }
  132. }
  133. }
  134. ]
  135. }
  136. },
  137. ...
  138. }
  139. --------------------------------------------------
  140. // TESTRESPONSE[s/\.\.\./"took": "$body.took", "timed_out": false, "_shards": "$body._shards", "hits": "$body.hits"/]
  141. <1> The name `tweets_over_time` now contains the `date_histogram` prefix.
  142. <2> The name `top_users` now contains the `top_hits` prefix.
  143. NOTE: For some aggregations, it is possible that the returned type is not the same as the one provided with the
  144. request. This is the case for Terms, Significant Terms and Percentiles aggregations, where the returned type
  145. also contains information about the type of the targeted field: `lterms` (for a terms aggregation on a Long field),
  146. `sigsterms` (for a significant terms aggregation on a String field), `tdigest_percentiles` (for a percentile
  147. aggregation based on the TDigest algorithm).
  148. [[indexing-aggregation-results]]
  149. == Indexing aggregation results with {transforms}
  150. <<transforms,{transforms-cap}>> enable you to convert existing {es} indices
  151. into summarized indices, which provide opportunities for new insights and
  152. analytics. You can use {transforms} to persistently index your aggregation
  153. results into entity-centric indices.