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