-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsearch_queries_geo_bounding_box.go
180 lines (155 loc) · 5.28 KB
/
search_queries_geo_bounding_box.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// GeoBoundingBoxQuery allows to filter hits based on a point location using
// a bounding box.
//
// For more details, see:
// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-geo-bounding-box-query.html
type GeoBoundingBoxQuery struct {
name string
topLeft interface{} // can be a GeoPoint, a GeoHash (string), or a lat/lon pair as float64
topRight interface{}
bottomRight interface{} // can be a GeoPoint, a GeoHash (string), or a lat/lon pair as float64
bottomLeft interface{}
wkt interface{}
typ string
validationMethod string
ignoreUnmapped *bool
queryName string
}
// NewGeoBoundingBoxQuery creates and initializes a new GeoBoundingBoxQuery.
func NewGeoBoundingBoxQuery(name string) *GeoBoundingBoxQuery {
return &GeoBoundingBoxQuery{
name: name,
}
}
// TopLeft position from longitude (left) and latitude (top).
func (q *GeoBoundingBoxQuery) TopLeft(top, left float64) *GeoBoundingBoxQuery {
q.topLeft = []float64{left, top}
return q
}
// TopLeftFromGeoPoint from a GeoPoint.
func (q *GeoBoundingBoxQuery) TopLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
return q.TopLeft(point.Lat, point.Lon)
}
// TopLeftFromGeoHash from a Geo hash.
func (q *GeoBoundingBoxQuery) TopLeftFromGeoHash(topLeft string) *GeoBoundingBoxQuery {
q.topLeft = topLeft
return q
}
// BottomRight position from longitude (right) and latitude (bottom).
func (q *GeoBoundingBoxQuery) BottomRight(bottom, right float64) *GeoBoundingBoxQuery {
q.bottomRight = []float64{right, bottom}
return q
}
// BottomRightFromGeoPoint from a GeoPoint.
func (q *GeoBoundingBoxQuery) BottomRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
return q.BottomRight(point.Lat, point.Lon)
}
// BottomRightFromGeoHash from a Geo hash.
func (q *GeoBoundingBoxQuery) BottomRightFromGeoHash(bottomRight string) *GeoBoundingBoxQuery {
q.bottomRight = bottomRight
return q
}
// BottomLeft position from longitude (left) and latitude (bottom).
func (q *GeoBoundingBoxQuery) BottomLeft(bottom, left float64) *GeoBoundingBoxQuery {
q.bottomLeft = []float64{bottom, left}
return q
}
// BottomLeftFromGeoPoint from a GeoPoint.
func (q *GeoBoundingBoxQuery) BottomLeftFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
return q.BottomLeft(point.Lat, point.Lon)
}
// BottomLeftFromGeoHash from a Geo hash.
func (q *GeoBoundingBoxQuery) BottomLeftFromGeoHash(bottomLeft string) *GeoBoundingBoxQuery {
q.bottomLeft = bottomLeft
return q
}
// TopRight position from longitude (right) and latitude (top).
func (q *GeoBoundingBoxQuery) TopRight(top, right float64) *GeoBoundingBoxQuery {
q.topRight = []float64{right, top}
return q
}
// TopRightFromGeoPoint from a GeoPoint.
func (q *GeoBoundingBoxQuery) TopRightFromGeoPoint(point *GeoPoint) *GeoBoundingBoxQuery {
return q.TopRight(point.Lat, point.Lon)
}
// TopRightFromGeoHash from a Geo hash.
func (q *GeoBoundingBoxQuery) TopRightFromGeoHash(topRight string) *GeoBoundingBoxQuery {
q.topRight = topRight
return q
}
// WKT initializes the bounding box from Well-Known Text (WKT),
// e.g. "BBOX (-74.1, -71.12, 40.73, 40.01)".
func (q *GeoBoundingBoxQuery) WKT(wkt interface{}) *GeoBoundingBoxQuery {
q.wkt = wkt
return q
}
// Type sets the type of executing the geo bounding box. It can be either
// memory or indexed. It defaults to memory.
func (q *GeoBoundingBoxQuery) Type(typ string) *GeoBoundingBoxQuery {
q.typ = typ
return q
}
// ValidationMethod accepts IGNORE_MALFORMED, COERCE, and STRICT (default).
// IGNORE_MALFORMED accepts geo points with invalid lat/lon.
// COERCE tries to infer the correct lat/lon.
func (q *GeoBoundingBoxQuery) ValidationMethod(method string) *GeoBoundingBoxQuery {
q.validationMethod = method
return q
}
// IgnoreUnmapped indicates whether to ignore unmapped fields (and run a
// MatchNoDocsQuery in place of this).
func (q *GeoBoundingBoxQuery) IgnoreUnmapped(ignoreUnmapped bool) *GeoBoundingBoxQuery {
q.ignoreUnmapped = &ignoreUnmapped
return q
}
// QueryName gives the query a name. It is used for caching.
func (q *GeoBoundingBoxQuery) QueryName(queryName string) *GeoBoundingBoxQuery {
q.queryName = queryName
return q
}
// Source returns JSON for the function score query.
func (q *GeoBoundingBoxQuery) Source() (interface{}, error) {
// {
// "geo_bounding_box" : {
// ...
// }
// }
source := make(map[string]interface{})
params := make(map[string]interface{})
source["geo_bounding_box"] = params
box := make(map[string]interface{})
if q.wkt != nil {
box["wkt"] = q.wkt
} else {
if q.topLeft != nil {
box["top_left"] = q.topLeft
}
if q.topRight != nil {
box["top_right"] = q.topRight
}
if q.bottomLeft != nil {
box["bottom_left"] = q.bottomLeft
}
if q.bottomRight != nil {
box["bottom_right"] = q.bottomRight
}
}
params[q.name] = box
if q.typ != "" {
params["type"] = q.typ
}
if q.validationMethod != "" {
params["validation_method"] = q.validationMethod
}
if q.ignoreUnmapped != nil {
params["ignore_unmapped"] = *q.ignoreUnmapped
}
if q.queryName != "" {
params["_name"] = q.queryName
}
return source, nil
}