summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/olivere/elastic.v5/search_aggs_bucket_geohash_grid.go
blob: 07f61b3314d19cd14d37a8a5ebe4908ad6419a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package elastic

type GeoHashGridAggregation struct {
	field           string
	precision       int
	size            int
	shardSize       int
	subAggregations map[string]Aggregation
	meta            map[string]interface{}
}

func NewGeoHashGridAggregation() *GeoHashGridAggregation {
	return &GeoHashGridAggregation{
		subAggregations: make(map[string]Aggregation),
		precision:       -1,
		size:            -1,
		shardSize:       -1,
	}
}

func (a *GeoHashGridAggregation) Field(field string) *GeoHashGridAggregation {
	a.field = field
	return a
}

func (a *GeoHashGridAggregation) Precision(precision int) *GeoHashGridAggregation {
	a.precision = precision
	return a
}

func (a *GeoHashGridAggregation) Size(size int) *GeoHashGridAggregation {
	a.size = size
	return a
}

func (a *GeoHashGridAggregation) ShardSize(shardSize int) *GeoHashGridAggregation {
	a.shardSize = shardSize
	return a
}

func (a *GeoHashGridAggregation) SubAggregation(name string, subAggregation Aggregation) *GeoHashGridAggregation {
	a.subAggregations[name] = subAggregation
	return a
}

func (a *GeoHashGridAggregation) Meta(metaData map[string]interface{}) *GeoHashGridAggregation {
	a.meta = metaData
	return a
}

func (a *GeoHashGridAggregation) Source() (interface{}, error) {
	// Example:
	// {
	//     "aggs": {
	//         "new_york": {
	//             "geohash_grid": {
	//                 "field": "location",
	//                 "precision": 5
	//             }
	//         }
	//     }
	// }

	source := make(map[string]interface{})
	opts := make(map[string]interface{})
	source["geohash_grid"] = opts

	if a.field != "" {
		opts["field"] = a.field
	}

	if a.precision != -1 {
		opts["precision"] = a.precision
	}

	if a.size != -1 {
		opts["size"] = a.size
	}

	if a.shardSize != -1 {
		opts["shard_size"] = a.shardSize
	}

	// AggregationBuilder (SubAggregations)
	if len(a.subAggregations) > 0 {
		aggsMap := make(map[string]interface{})
		source["aggregations"] = aggsMap
		for name, aggregate := range a.subAggregations {
			src, err := aggregate.Source()
			if err != nil {
				return nil, err
			}
			aggsMap[name] = src
		}
	}

	if len(a.meta) > 0 {
		source["meta"] = a.meta
	}

	return source, nil
}