/
geo-bounding-box-query.js
196 lines (174 loc) · 5.25 KB
/
geo-bounding-box-query.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const isNil = require('lodash.isnil');
const {
GeoPoint,
util: { checkType, invalidParam }
} = require('../../core');
const GeoQueryBase = require('./geo-query-base');
const ES_REF_URL =
'https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html';
const invalidTypeParam = invalidParam(
ES_REF_URL,
'type',
"'memory' or 'indexed'"
);
/**
* A query allowing to filter hits based on a point location using a bounding box.
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html)
*
* @example
* // Format of point in Geohash
* const qry = esb.geoBoundingBoxQuery('pin.location')
* .topLeft(esb.geoPoint().string('dr5r9ydj2y73'))
* .bottomRight(esb.geoPoint().string('drj7teegpus6'));
*
* @example
* // Format of point with lat lon as properties
* const qry = esb.geoBoundingBoxQuery()
* .field('pin.location')
* .topLeft(esb.geoPoint()
* .lat(40.73)
* .lon(-74.1))
* .bottomRight(esb.geoPoint()
* .lat(40.10)
* .lon(-71.12));
*
* @example
* // Set bounding box values separately
* const qry = esb.geoBoundingBoxQuery('pin.location')
* .top(40.73)
* .left(-74.1)
* .bottom(40.01)
* .right(-71.12);
*
* @param {string=} field
*
* @extends GeoQueryBase
*/
class GeoBoundingBoxQuery extends GeoQueryBase {
// eslint-disable-next-line require-jsdoc
constructor(field) {
super('geo_bounding_box', field);
}
/**
* Sets the top left coordinate for the Geo bounding box filter for
* querying documents
*
* @param {GeoPoint} point A valid `GeoPoint`
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
topLeft(point) {
checkType(point, GeoPoint);
this._fieldOpts.top_left = point;
return this;
}
/**
* Sets the bottom right coordinate for the Geo bounding box filter for
* querying documents
*
* @param {GeoPoint} point A valid `GeoPoint`
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
bottomRight(point) {
checkType(point, GeoPoint);
this._fieldOpts.bottom_right = point;
return this;
}
/**
* Sets the top right coordinate for the Geo bounding box filter for
* querying documents
*
* @param {GeoPoint} point A valid `GeoPoint`
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
topRight(point) {
checkType(point, GeoPoint);
this._fieldOpts.top_right = point;
return this;
}
/**
* Sets the bottom left coordinate for the Geo bounding box filter for
* querying documents
*
* @param {GeoPoint} point A valid `GeoPoint`
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
bottomLeft(point) {
checkType(point, GeoPoint);
this._fieldOpts.bottom_left = point;
return this;
}
/**
* Sets value for top of the bounding box.
*
* @param {number} val
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
top(val) {
this._fieldOpts.top = val;
return this;
}
/**
* Sets value for left of the bounding box.
*
* @param {number} val
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
left(val) {
this._fieldOpts.left = val;
return this;
}
/**
* Sets value for bottom of the bounding box.
*
* @param {number} val
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
bottom(val) {
this._fieldOpts.bottom = val;
return this;
}
/**
* Sets value for right of the bounding box.
*
* @param {number} val
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
right(val) {
this._fieldOpts.right = val;
return this;
}
/**
* Sets the type of execution for the bounding box query.
* The type of the bounding box execution by default is set to memory,
* which means in memory checks if the doc falls within the bounding
* box range. In some cases, an indexed option will perform faster
* (but note that the geo_point type must have lat and lon indexed in this case)
*
* @example
*
* const geoQry = esb.geoBoundingBoxQuery()
* .field('pin.location')
* .topLeft(esb.geoPoint()
* .lat(40.73)
* .lon(-74.1))
* .bottomRight(esb.geoPoint()
* .lat(40.10)
* .lon(-71.12))
* .type('indexed');
*
* @param {string} type Can either `memory` or `indexed`
* @returns {GeoBoundingBoxQuery} returns `this` so that calls can be chained.
*/
type(type) {
if (isNil(type)) invalidTypeParam(type);
const typeLower = type.toLowerCase();
if (typeLower !== 'memory' && typeLower !== 'indexed') {
invalidTypeParam(type);
}
this._queryOpts.type = typeLower;
return this;
}
}
module.exports = GeoBoundingBoxQuery;