search_aggs_metrics_top_hits.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. // TopHitsAggregation keeps track of the most relevant document
  6. // being aggregated. This aggregator is intended to be used as a
  7. // sub aggregator, so that the top matching documents
  8. // can be aggregated per bucket.
  9. //
  10. // It can effectively be used to group result sets by certain fields via
  11. // a bucket aggregator. One or more bucket aggregators determines by
  12. // which properties a result set get sliced into.
  13. //
  14. // See: https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-metrics-top-hits-aggregation.html
  15. type TopHitsAggregation struct {
  16. searchSource *SearchSource
  17. }
  18. func NewTopHitsAggregation() *TopHitsAggregation {
  19. return &TopHitsAggregation{
  20. searchSource: NewSearchSource(),
  21. }
  22. }
  23. func (a *TopHitsAggregation) From(from int) *TopHitsAggregation {
  24. a.searchSource = a.searchSource.From(from)
  25. return a
  26. }
  27. func (a *TopHitsAggregation) Size(size int) *TopHitsAggregation {
  28. a.searchSource = a.searchSource.Size(size)
  29. return a
  30. }
  31. func (a *TopHitsAggregation) TrackScores(trackScores bool) *TopHitsAggregation {
  32. a.searchSource = a.searchSource.TrackScores(trackScores)
  33. return a
  34. }
  35. func (a *TopHitsAggregation) Explain(explain bool) *TopHitsAggregation {
  36. a.searchSource = a.searchSource.Explain(explain)
  37. return a
  38. }
  39. func (a *TopHitsAggregation) Version(version bool) *TopHitsAggregation {
  40. a.searchSource = a.searchSource.Version(version)
  41. return a
  42. }
  43. func (a *TopHitsAggregation) NoStoredFields() *TopHitsAggregation {
  44. a.searchSource = a.searchSource.NoStoredFields()
  45. return a
  46. }
  47. func (a *TopHitsAggregation) FetchSource(fetchSource bool) *TopHitsAggregation {
  48. a.searchSource = a.searchSource.FetchSource(fetchSource)
  49. return a
  50. }
  51. func (a *TopHitsAggregation) FetchSourceContext(fetchSourceContext *FetchSourceContext) *TopHitsAggregation {
  52. a.searchSource = a.searchSource.FetchSourceContext(fetchSourceContext)
  53. return a
  54. }
  55. func (a *TopHitsAggregation) DocvalueFields(docvalueFields ...string) *TopHitsAggregation {
  56. a.searchSource = a.searchSource.DocvalueFields(docvalueFields...)
  57. return a
  58. }
  59. func (a *TopHitsAggregation) DocvalueField(docvalueField string) *TopHitsAggregation {
  60. a.searchSource = a.searchSource.DocvalueField(docvalueField)
  61. return a
  62. }
  63. func (a *TopHitsAggregation) ScriptFields(scriptFields ...*ScriptField) *TopHitsAggregation {
  64. a.searchSource = a.searchSource.ScriptFields(scriptFields...)
  65. return a
  66. }
  67. func (a *TopHitsAggregation) ScriptField(scriptField *ScriptField) *TopHitsAggregation {
  68. a.searchSource = a.searchSource.ScriptField(scriptField)
  69. return a
  70. }
  71. func (a *TopHitsAggregation) Sort(field string, ascending bool) *TopHitsAggregation {
  72. a.searchSource = a.searchSource.Sort(field, ascending)
  73. return a
  74. }
  75. func (a *TopHitsAggregation) SortWithInfo(info SortInfo) *TopHitsAggregation {
  76. a.searchSource = a.searchSource.SortWithInfo(info)
  77. return a
  78. }
  79. func (a *TopHitsAggregation) SortBy(sorter ...Sorter) *TopHitsAggregation {
  80. a.searchSource = a.searchSource.SortBy(sorter...)
  81. return a
  82. }
  83. func (a *TopHitsAggregation) Highlight(highlight *Highlight) *TopHitsAggregation {
  84. a.searchSource = a.searchSource.Highlight(highlight)
  85. return a
  86. }
  87. func (a *TopHitsAggregation) Highlighter() *Highlight {
  88. return a.searchSource.Highlighter()
  89. }
  90. func (a *TopHitsAggregation) Source() (interface{}, error) {
  91. // Example:
  92. // {
  93. // "aggs": {
  94. // "top_tag_hits": {
  95. // "top_hits": {
  96. // "sort": [
  97. // {
  98. // "last_activity_date": {
  99. // "order": "desc"
  100. // }
  101. // }
  102. // ],
  103. // "_source": {
  104. // "include": [
  105. // "title"
  106. // ]
  107. // },
  108. // "size" : 1
  109. // }
  110. // }
  111. // }
  112. // }
  113. // This method returns only the { "top_hits" : { ... } } part.
  114. source := make(map[string]interface{})
  115. src, err := a.searchSource.Source()
  116. if err != nil {
  117. return nil, err
  118. }
  119. source["top_hits"] = src
  120. return source, nil
  121. }