search_aggs_tophits.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2012-2015 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: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
  15. type TopHitsAggregation struct {
  16. searchSource *SearchSource
  17. }
  18. func NewTopHitsAggregation() TopHitsAggregation {
  19. a := TopHitsAggregation{
  20. searchSource: NewSearchSource(),
  21. }
  22. return a
  23. }
  24. func (a TopHitsAggregation) From(from int) TopHitsAggregation {
  25. a.searchSource = a.searchSource.From(from)
  26. return a
  27. }
  28. func (a TopHitsAggregation) Size(size int) TopHitsAggregation {
  29. a.searchSource = a.searchSource.Size(size)
  30. return a
  31. }
  32. func (a TopHitsAggregation) TrackScores(trackScores bool) TopHitsAggregation {
  33. a.searchSource = a.searchSource.TrackScores(trackScores)
  34. return a
  35. }
  36. func (a TopHitsAggregation) Explain(explain bool) TopHitsAggregation {
  37. a.searchSource = a.searchSource.Explain(explain)
  38. return a
  39. }
  40. func (a TopHitsAggregation) Version(version bool) TopHitsAggregation {
  41. a.searchSource = a.searchSource.Version(version)
  42. return a
  43. }
  44. func (a TopHitsAggregation) NoFields() TopHitsAggregation {
  45. a.searchSource = a.searchSource.NoFields()
  46. return a
  47. }
  48. func (a TopHitsAggregation) FetchSource(fetchSource bool) TopHitsAggregation {
  49. a.searchSource = a.searchSource.FetchSource(fetchSource)
  50. return a
  51. }
  52. func (a TopHitsAggregation) FetchSourceContext(fetchSourceContext *FetchSourceContext) TopHitsAggregation {
  53. a.searchSource = a.searchSource.FetchSourceContext(fetchSourceContext)
  54. return a
  55. }
  56. func (a TopHitsAggregation) FieldDataFields(fieldDataFields ...string) TopHitsAggregation {
  57. a.searchSource = a.searchSource.FieldDataFields(fieldDataFields...)
  58. return a
  59. }
  60. func (a TopHitsAggregation) FieldDataField(fieldDataField string) TopHitsAggregation {
  61. a.searchSource = a.searchSource.FieldDataField(fieldDataField)
  62. return a
  63. }
  64. func (a TopHitsAggregation) ScriptFields(scriptFields ...*ScriptField) TopHitsAggregation {
  65. a.searchSource = a.searchSource.ScriptFields(scriptFields...)
  66. return a
  67. }
  68. func (a TopHitsAggregation) ScriptField(scriptField *ScriptField) TopHitsAggregation {
  69. a.searchSource = a.searchSource.ScriptField(scriptField)
  70. return a
  71. }
  72. func (a TopHitsAggregation) PartialFields(partialFields ...*PartialField) TopHitsAggregation {
  73. a.searchSource = a.searchSource.PartialFields(partialFields...)
  74. return a
  75. }
  76. func (a TopHitsAggregation) PartialField(partialField *PartialField) TopHitsAggregation {
  77. a.searchSource = a.searchSource.PartialField(partialField)
  78. return a
  79. }
  80. func (a TopHitsAggregation) Sort(field string, ascending bool) TopHitsAggregation {
  81. a.searchSource = a.searchSource.Sort(field, ascending)
  82. return a
  83. }
  84. func (a TopHitsAggregation) SortWithInfo(info SortInfo) TopHitsAggregation {
  85. a.searchSource = a.searchSource.SortWithInfo(info)
  86. return a
  87. }
  88. func (a TopHitsAggregation) SortBy(sorter ...Sorter) TopHitsAggregation {
  89. a.searchSource = a.searchSource.SortBy(sorter...)
  90. return a
  91. }
  92. func (a TopHitsAggregation) Highlight(highlight *Highlight) TopHitsAggregation {
  93. a.searchSource = a.searchSource.Highlight(highlight)
  94. return a
  95. }
  96. func (a TopHitsAggregation) Highlighter() *Highlight {
  97. return a.searchSource.Highlighter()
  98. }
  99. func (a TopHitsAggregation) Source() interface{} {
  100. // Example:
  101. // {
  102. // "aggs": {
  103. // "top_tag_hits": {
  104. // "top_hits": {
  105. // "sort": [
  106. // {
  107. // "last_activity_date": {
  108. // "order": "desc"
  109. // }
  110. // }
  111. // ],
  112. // "_source": {
  113. // "include": [
  114. // "title"
  115. // ]
  116. // },
  117. // "size" : 1
  118. // }
  119. // }
  120. // }
  121. // }
  122. // This method returns only the { "top_hits" : { ... } } part.
  123. source := make(map[string]interface{})
  124. source["top_hits"] = a.searchSource.Source()
  125. return source
  126. }