| 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 |
// AdjacencyMatrixAggregation returning a form of adjacency matrix. |
| 8 |
// The request provides a collection of named filter expressions, |
| 9 |
// similar to the filters aggregation request. Each bucket in the |
| 10 |
// response represents a non-empty cell in the matrix of intersecting filters. |
| 11 |
// |
| 12 |
// For details, see |
| 13 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-adjacency-matrix-aggregation.html |
| 14 |
type AdjacencyMatrixAggregation struct { |
| 15 |
filters map[string]Query |
| 16 |
subAggregations map[string]Aggregation |
| 17 |
meta map[string]interface{} |
| 18 |
} |
| 19 |
|
| 20 |
// NewAdjacencyMatrixAggregation initializes a new AdjacencyMatrixAggregation. |
| 21 |
func NewAdjacencyMatrixAggregation() *AdjacencyMatrixAggregation { |
| 22 |
return &AdjacencyMatrixAggregation{ |
| 23 |
filters: make(map[string]Query), |
| 24 |
subAggregations: make(map[string]Aggregation), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// Filters adds the filter |
| 29 |
func (a *AdjacencyMatrixAggregation) Filters(name string, filter Query) *AdjacencyMatrixAggregation { |
| 30 |
a.filters[name] = filter |
| 31 |
return a |
| 32 |
} |
| 33 |
|
| 34 |
// SubAggregation adds a sub-aggregation to this aggregation. |
| 35 |
func (a *AdjacencyMatrixAggregation) SubAggregation(name string, subAggregation Aggregation) *AdjacencyMatrixAggregation { |
| 36 |
a.subAggregations[name] = subAggregation |
| 37 |
return a |
| 38 |
} |
| 39 |
|
| 40 |
// Meta sets the meta data to be included in the aggregation response. |
| 41 |
func (a *AdjacencyMatrixAggregation) Meta(metaData map[string]interface{}) *AdjacencyMatrixAggregation { |
| 42 |
a.meta = metaData |
| 43 |
return a |
| 44 |
} |
| 45 |
|
| 46 |
// Source returns the a JSON-serializable interface. |
| 47 |
func (a *AdjacencyMatrixAggregation) Source() (interface{}, error) { |
| 48 |
// Example: |
| 49 |
// { |
| 50 |
// "aggs" : { |
| 51 |
// "interactions" : { |
| 52 |
// "adjacency_matrix" : { |
| 53 |
// "filters" : { |
| 54 |
// "grpA" : { "terms" : { "accounts" : ["hillary", "sidney"] }}, |
| 55 |
// "grpB" : { "terms" : { "accounts" : ["donald", "mitt"] }}, |
| 56 |
// "grpC" : { "terms" : { "accounts" : ["vladimir", "nigel"] }} |
| 57 |
// } |
| 58 |
// } |
| 59 |
// } |
| 60 |
// } |
| 61 |
// This method returns only the (outer) { "adjacency_matrix" : {} } part. |
| 62 |
|
| 63 |
source := make(map[string]interface{}) |
| 64 |
adjacencyMatrix := make(map[string]interface{}) |
| 65 |
source["adjacency_matrix"] = adjacencyMatrix |
| 66 |
|
| 67 |
dict := make(map[string]interface{}) |
| 68 |
for key, filter := range a.filters { |
| 69 |
src, err := filter.Source() |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
dict[key] = src |
| 74 |
} |
| 75 |
adjacencyMatrix["filters"] = dict |
| 76 |
|
| 77 |
// AggregationBuilder (SubAggregations) |
| 78 |
if len(a.subAggregations) > 0 { |
| 79 |
aggsMap := make(map[string]interface{}) |
| 80 |
source["aggregations"] = aggsMap |
| 81 |
for name, aggregate := range a.subAggregations { |
| 82 |
src, err := aggregate.Source() |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
aggsMap[name] = src |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// Add Meta data if available |
| 91 |
if len(a.meta) > 0 { |
| 92 |
source["meta"] = a.meta |
| 93 |
} |
| 94 |
|
| 95 |
return source, nil |
| 96 |
} |