Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_aggs_bucket_adjacency_matrix.go 2.9KB

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