1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.GeoPolygonSearchQuery = exports.GeoBoundingBoxSearchQuery = exports.GeoDistanceSearchQuery = exports.MatchNoneSearchQuery = exports.MatchAllSearchQuery = exports.PrefixSearchQuery = exports.PhraseSearchQuery = exports.TermSearchQuery = exports.BooleanFieldSearchQuery = exports.DocIdSearchQuery = exports.WildcardSearchQuery = exports.BooleanSearchQuery = exports.DisjunctionSearchQuery = exports.ConjunctionSearchQuery = exports.DateRangeSearchQuery = exports.NumericRangeSearchQuery = exports.QueryStringSearchQuery = exports.RegexpSearchQuery = exports.MatchPhraseSearchQuery = exports.MatchSearchQuery = exports.SearchQuery = exports.MatchOperator = void 0;
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var MatchOperator;
|
12 | (function (MatchOperator) {
|
13 | |
14 |
|
15 |
|
16 | MatchOperator["Or"] = "or";
|
17 | |
18 |
|
19 |
|
20 | MatchOperator["And"] = "and";
|
21 | })(MatchOperator || (exports.MatchOperator = MatchOperator = {}));
|
22 | function _parseGeoPoint(v) {
|
23 | if (Array.isArray(v)) {
|
24 | return v;
|
25 | }
|
26 | else if (v instanceof Object) {
|
27 | const latLonObj = v;
|
28 | if (latLonObj.lon || latLonObj.lat) {
|
29 | return [latLonObj.lon, latLonObj.lat];
|
30 | }
|
31 | else if (latLonObj.longitude || latLonObj.latitude) {
|
32 | return [latLonObj.longitude, latLonObj.latitude];
|
33 | }
|
34 | }
|
35 | throw new Error('invalid geopoint specified');
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 | function _unpackListArgs(args) {
|
41 | if (Array.isArray(args[0])) {
|
42 | return args[0];
|
43 | }
|
44 | return args;
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | class SearchQuery {
|
52 | constructor(data) {
|
53 | if (!data) {
|
54 | data = {};
|
55 | }
|
56 | this._data = data;
|
57 | }
|
58 | toJSON() {
|
59 | return this._data;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 | static toJSON(query) {
|
65 | if (query.toJSON) {
|
66 | return query.toJSON();
|
67 | }
|
68 | return query;
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | static hasProp(query, prop) {
|
74 | const json = this.toJSON(query);
|
75 | return json[prop] !== undefined;
|
76 | }
|
77 | static match(match) {
|
78 | return new MatchSearchQuery(match);
|
79 | }
|
80 | static matchPhrase(phrase) {
|
81 | return new MatchPhraseSearchQuery(phrase);
|
82 | }
|
83 | static regexp(regexp) {
|
84 | return new RegexpSearchQuery(regexp);
|
85 | }
|
86 | static queryString(query) {
|
87 | return new QueryStringSearchQuery(query);
|
88 | }
|
89 | static numericRange() {
|
90 | return new NumericRangeSearchQuery();
|
91 | }
|
92 | static dateRange() {
|
93 | return new DateRangeSearchQuery();
|
94 | }
|
95 | |
96 |
|
97 |
|
98 | static conjuncts(...args) {
|
99 | const queries = _unpackListArgs(args);
|
100 | return new ConjunctionSearchQuery(...queries);
|
101 | }
|
102 | |
103 |
|
104 |
|
105 | static disjuncts(...args) {
|
106 | const queries = _unpackListArgs(args);
|
107 | return new DisjunctionSearchQuery(...queries);
|
108 | }
|
109 | static boolean() {
|
110 | return new BooleanSearchQuery();
|
111 | }
|
112 | static wildcard(wildcard) {
|
113 | return new WildcardSearchQuery(wildcard);
|
114 | }
|
115 | |
116 |
|
117 |
|
118 | static docIds(...args) {
|
119 | const queries = _unpackListArgs(args);
|
120 | return new DocIdSearchQuery(...queries);
|
121 | }
|
122 | static booleanField(val) {
|
123 | return new BooleanFieldSearchQuery(val);
|
124 | }
|
125 | static term(term) {
|
126 | return new TermSearchQuery(term);
|
127 | }
|
128 | static phrase(terms) {
|
129 | return new PhraseSearchQuery(terms);
|
130 | }
|
131 | static prefix(prefix) {
|
132 | return new PrefixSearchQuery(prefix);
|
133 | }
|
134 | static matchAll() {
|
135 | return new MatchAllSearchQuery();
|
136 | }
|
137 | static matchNone() {
|
138 | return new MatchNoneSearchQuery();
|
139 | }
|
140 | static geoDistance(lon, lat, distance) {
|
141 | return new GeoDistanceSearchQuery(lon, lat, distance);
|
142 | }
|
143 | static geoBoundingBox(tl_lon, tl_lat, br_lon, br_lat) {
|
144 | return new GeoBoundingBoxSearchQuery(tl_lon, tl_lat, br_lon, br_lat);
|
145 | }
|
146 | static geoPolygon(points) {
|
147 | return new GeoPolygonSearchQuery(points);
|
148 | }
|
149 | }
|
150 | exports.SearchQuery = SearchQuery;
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | class MatchSearchQuery extends SearchQuery {
|
157 | |
158 |
|
159 |
|
160 | constructor(match) {
|
161 | super({
|
162 | match: match,
|
163 | });
|
164 | }
|
165 | operator(op) {
|
166 | this._data.operator = op;
|
167 | return this;
|
168 | }
|
169 | field(field) {
|
170 | this._data.field = field;
|
171 | return this;
|
172 | }
|
173 | analyzer(analyzer) {
|
174 | this._data.analyzer = analyzer;
|
175 | return this;
|
176 | }
|
177 | prefixLength(prefixLength) {
|
178 | this._data.prefix_length = prefixLength;
|
179 | return this;
|
180 | }
|
181 | fuzziness(fuzziness) {
|
182 | this._data.fuzziness = fuzziness;
|
183 | return this;
|
184 | }
|
185 | boost(boost) {
|
186 | this._data.boost = boost;
|
187 | return this;
|
188 | }
|
189 | }
|
190 | exports.MatchSearchQuery = MatchSearchQuery;
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | class MatchPhraseSearchQuery extends SearchQuery {
|
197 | |
198 |
|
199 |
|
200 | constructor(phrase) {
|
201 | super({
|
202 | match_phrase: phrase,
|
203 | });
|
204 | }
|
205 | field(field) {
|
206 | this._data.field = field;
|
207 | return this;
|
208 | }
|
209 | analyzer(analyzer) {
|
210 | this._data.analyzer = analyzer;
|
211 | return this;
|
212 | }
|
213 | boost(boost) {
|
214 | this._data.boost = boost;
|
215 | return this;
|
216 | }
|
217 | }
|
218 | exports.MatchPhraseSearchQuery = MatchPhraseSearchQuery;
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | class RegexpSearchQuery extends SearchQuery {
|
225 | |
226 |
|
227 |
|
228 | constructor(regexp) {
|
229 | super({
|
230 | regexp: regexp,
|
231 | });
|
232 | }
|
233 | field(field) {
|
234 | this._data.field = field;
|
235 | return this;
|
236 | }
|
237 | boost(boost) {
|
238 | this._data.boost = boost;
|
239 | return this;
|
240 | }
|
241 | }
|
242 | exports.RegexpSearchQuery = RegexpSearchQuery;
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | class QueryStringSearchQuery extends SearchQuery {
|
249 | |
250 |
|
251 |
|
252 | constructor(query) {
|
253 | super({
|
254 | query: query,
|
255 | });
|
256 | }
|
257 | boost(boost) {
|
258 | this._data.boost = boost;
|
259 | return this;
|
260 | }
|
261 | }
|
262 | exports.QueryStringSearchQuery = QueryStringSearchQuery;
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | class NumericRangeSearchQuery extends SearchQuery {
|
269 | |
270 |
|
271 |
|
272 | constructor() {
|
273 | super({});
|
274 | }
|
275 | min(min, inclusive) {
|
276 | if (inclusive === undefined) {
|
277 | inclusive = true;
|
278 | }
|
279 | this._data.min = min;
|
280 | this._data.inclusive_min = inclusive;
|
281 | return this;
|
282 | }
|
283 | max(max, inclusive) {
|
284 | if (inclusive === undefined) {
|
285 | inclusive = false;
|
286 | }
|
287 | this._data.max = max;
|
288 | this._data.inclusive_max = inclusive;
|
289 | return this;
|
290 | }
|
291 | field(field) {
|
292 | this._data.field = field;
|
293 | return this;
|
294 | }
|
295 | boost(boost) {
|
296 | this._data.boost = boost;
|
297 | return this;
|
298 | }
|
299 | }
|
300 | exports.NumericRangeSearchQuery = NumericRangeSearchQuery;
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | class DateRangeSearchQuery extends SearchQuery {
|
307 | |
308 |
|
309 |
|
310 | constructor() {
|
311 | super({});
|
312 | }
|
313 | start(start, inclusive) {
|
314 | if (inclusive === undefined) {
|
315 | inclusive = true;
|
316 | }
|
317 | if (start instanceof Date) {
|
318 | this._data.start = start.toISOString();
|
319 | }
|
320 | else {
|
321 | this._data.start = start;
|
322 | }
|
323 | this._data.inclusive_start = inclusive;
|
324 | return this;
|
325 | }
|
326 | end(end, inclusive) {
|
327 | if (inclusive === undefined) {
|
328 | inclusive = false;
|
329 | }
|
330 | if (end instanceof Date) {
|
331 | this._data.end = end.toISOString();
|
332 | }
|
333 | else {
|
334 | this._data.end = end;
|
335 | }
|
336 | this._data.inclusive_end = inclusive;
|
337 | return this;
|
338 | }
|
339 | field(field) {
|
340 | this._data.field = field;
|
341 | return this;
|
342 | }
|
343 | dateTimeParser(parser) {
|
344 | this._data.datetime_parser = parser;
|
345 | return this;
|
346 | }
|
347 | boost(boost) {
|
348 | this._data.boost = boost;
|
349 | return this;
|
350 | }
|
351 | }
|
352 | exports.DateRangeSearchQuery = DateRangeSearchQuery;
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | class ConjunctionSearchQuery extends SearchQuery {
|
359 | |
360 |
|
361 |
|
362 | constructor(...queries) {
|
363 | super({
|
364 | conjuncts: [],
|
365 | });
|
366 | this.and(...queries);
|
367 | }
|
368 | |
369 |
|
370 |
|
371 | and(...args) {
|
372 | const queries = _unpackListArgs(args);
|
373 | for (let i = 0; i < queries.length; ++i) {
|
374 | this._data.conjuncts.push(queries[i]);
|
375 | }
|
376 | return this;
|
377 | }
|
378 | boost(boost) {
|
379 | this._data.boost = boost;
|
380 | return this;
|
381 | }
|
382 | }
|
383 | exports.ConjunctionSearchQuery = ConjunctionSearchQuery;
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | class DisjunctionSearchQuery extends SearchQuery {
|
390 | |
391 |
|
392 |
|
393 | constructor(...queries) {
|
394 | super({
|
395 | disjuncts: [],
|
396 | });
|
397 | this.or(...queries);
|
398 | }
|
399 | |
400 |
|
401 |
|
402 | or(...args) {
|
403 | const queries = _unpackListArgs(args);
|
404 | for (let i = 0; i < queries.length; ++i) {
|
405 | this._data.disjuncts.push(queries[i]);
|
406 | }
|
407 | return this;
|
408 | }
|
409 | boost(boost) {
|
410 | this._data.boost = boost;
|
411 | return this;
|
412 | }
|
413 | }
|
414 | exports.DisjunctionSearchQuery = DisjunctionSearchQuery;
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | class BooleanSearchQuery extends SearchQuery {
|
421 | |
422 |
|
423 |
|
424 | constructor() {
|
425 | super({});
|
426 | this._shouldMin = undefined;
|
427 | }
|
428 | must(query) {
|
429 | if (!SearchQuery.hasProp(query, 'conjuncts')) {
|
430 | query = new ConjunctionSearchQuery(query);
|
431 | }
|
432 | this._data.must = query;
|
433 | return this;
|
434 | }
|
435 | should(query) {
|
436 | if (!SearchQuery.hasProp(query, 'disjuncts')) {
|
437 | query = new DisjunctionSearchQuery(query);
|
438 | }
|
439 | this._data.should = query;
|
440 | return this;
|
441 | }
|
442 | mustNot(query) {
|
443 | if (!SearchQuery.hasProp(query, 'disjuncts')) {
|
444 | query = new DisjunctionSearchQuery(query);
|
445 | }
|
446 | this._data.must_not = query;
|
447 | return this;
|
448 | }
|
449 | shouldMin(shouldMin) {
|
450 | this._shouldMin = shouldMin;
|
451 | return this;
|
452 | }
|
453 | boost(boost) {
|
454 | this._data.boost = boost;
|
455 | return this;
|
456 | }
|
457 | toJSON() {
|
458 | const out = {};
|
459 | if (this._data.must) {
|
460 | out.must = SearchQuery.toJSON(this._data.must);
|
461 | }
|
462 | if (this._data.should) {
|
463 | out.should = SearchQuery.toJSON(this._data.should);
|
464 | if (this._shouldMin) {
|
465 | out.should.min = this._shouldMin;
|
466 | }
|
467 | }
|
468 | if (this._data.must_not) {
|
469 | out.must_not = SearchQuery.toJSON(this._data.must_not);
|
470 | }
|
471 | return out;
|
472 | }
|
473 | }
|
474 | exports.BooleanSearchQuery = BooleanSearchQuery;
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | class WildcardSearchQuery extends SearchQuery {
|
481 | |
482 |
|
483 |
|
484 | constructor(wildcard) {
|
485 | super({
|
486 | wildcard: wildcard,
|
487 | });
|
488 | }
|
489 | field(field) {
|
490 | this._data.field = field;
|
491 | return this;
|
492 | }
|
493 | boost(boost) {
|
494 | this._data.boost = boost;
|
495 | return this;
|
496 | }
|
497 | }
|
498 | exports.WildcardSearchQuery = WildcardSearchQuery;
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 | class DocIdSearchQuery extends SearchQuery {
|
505 | |
506 |
|
507 |
|
508 | constructor(...ids) {
|
509 | super({
|
510 | ids: [],
|
511 | });
|
512 | this.addDocIds(...ids);
|
513 | }
|
514 | |
515 |
|
516 |
|
517 | addDocIds(...args) {
|
518 | const ids = _unpackListArgs(args);
|
519 | for (let i = 0; i < ids.length; ++i) {
|
520 | this._data.ids.push(ids[i]);
|
521 | }
|
522 | return this;
|
523 | }
|
524 | field(field) {
|
525 | this._data.field = field;
|
526 | return this;
|
527 | }
|
528 | boost(boost) {
|
529 | this._data.boost = boost;
|
530 | return this;
|
531 | }
|
532 | }
|
533 | exports.DocIdSearchQuery = DocIdSearchQuery;
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 | class BooleanFieldSearchQuery extends SearchQuery {
|
540 | |
541 |
|
542 |
|
543 | constructor(val) {
|
544 | super({
|
545 | bool: val,
|
546 | });
|
547 | }
|
548 | field(field) {
|
549 | this._data.field = field;
|
550 | return this;
|
551 | }
|
552 | boost(boost) {
|
553 | this._data.boost = boost;
|
554 | return this;
|
555 | }
|
556 | }
|
557 | exports.BooleanFieldSearchQuery = BooleanFieldSearchQuery;
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | class TermSearchQuery extends SearchQuery {
|
564 | |
565 |
|
566 |
|
567 | constructor(term) {
|
568 | super({
|
569 | term: term,
|
570 | });
|
571 | }
|
572 | field(field) {
|
573 | this._data.field = field;
|
574 | return this;
|
575 | }
|
576 | prefixLength(prefixLength) {
|
577 | this._data.prefix_length = prefixLength;
|
578 | return this;
|
579 | }
|
580 | fuzziness(fuzziness) {
|
581 | this._data.fuzziness = fuzziness;
|
582 | return this;
|
583 | }
|
584 | boost(boost) {
|
585 | this._data.boost = boost;
|
586 | return this;
|
587 | }
|
588 | }
|
589 | exports.TermSearchQuery = TermSearchQuery;
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 | class PhraseSearchQuery extends SearchQuery {
|
596 | |
597 |
|
598 |
|
599 | constructor(terms) {
|
600 | super({
|
601 | terms: terms,
|
602 | });
|
603 | }
|
604 | field(field) {
|
605 | this._data.field = field;
|
606 | return this;
|
607 | }
|
608 | boost(boost) {
|
609 | this._data.boost = boost;
|
610 | return this;
|
611 | }
|
612 | }
|
613 | exports.PhraseSearchQuery = PhraseSearchQuery;
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | class PrefixSearchQuery extends SearchQuery {
|
620 | |
621 |
|
622 |
|
623 | constructor(prefix) {
|
624 | super({
|
625 | prefix: prefix,
|
626 | });
|
627 | }
|
628 | field(field) {
|
629 | this._data.field = field;
|
630 | return this;
|
631 | }
|
632 | boost(boost) {
|
633 | this._data.boost = boost;
|
634 | return this;
|
635 | }
|
636 | }
|
637 | exports.PrefixSearchQuery = PrefixSearchQuery;
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 | class MatchAllSearchQuery extends SearchQuery {
|
644 | |
645 |
|
646 |
|
647 | constructor() {
|
648 | super({
|
649 | match_all: null,
|
650 | });
|
651 | }
|
652 | }
|
653 | exports.MatchAllSearchQuery = MatchAllSearchQuery;
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 | class MatchNoneSearchQuery extends SearchQuery {
|
660 | |
661 |
|
662 |
|
663 | constructor() {
|
664 | super({
|
665 | match_none: true,
|
666 | });
|
667 | }
|
668 | }
|
669 | exports.MatchNoneSearchQuery = MatchNoneSearchQuery;
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 | class GeoDistanceSearchQuery extends SearchQuery {
|
676 | |
677 |
|
678 |
|
679 | constructor(lon, lat, distance) {
|
680 | super({
|
681 | location: [lon, lat],
|
682 | distance: distance,
|
683 | });
|
684 | }
|
685 | field(field) {
|
686 | this._data.field = field;
|
687 | return this;
|
688 | }
|
689 | boost(boost) {
|
690 | this._data.boost = boost;
|
691 | return this;
|
692 | }
|
693 | }
|
694 | exports.GeoDistanceSearchQuery = GeoDistanceSearchQuery;
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 | class GeoBoundingBoxSearchQuery extends SearchQuery {
|
701 | |
702 |
|
703 |
|
704 | constructor(tl_lon, tl_lat, br_lon, br_lat) {
|
705 | super({
|
706 | top_left: [tl_lon, tl_lat],
|
707 | bottom_right: [br_lon, br_lat],
|
708 | });
|
709 | }
|
710 | field(field) {
|
711 | this._data.field = field;
|
712 | return this;
|
713 | }
|
714 | boost(boost) {
|
715 | this._data.boost = boost;
|
716 | return this;
|
717 | }
|
718 | }
|
719 | exports.GeoBoundingBoxSearchQuery = GeoBoundingBoxSearchQuery;
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 | class GeoPolygonSearchQuery extends SearchQuery {
|
726 | |
727 |
|
728 |
|
729 | constructor(points) {
|
730 | const mappedPoints = points.map((v) => _parseGeoPoint(v));
|
731 | super({
|
732 | polygon_points: mappedPoints,
|
733 | });
|
734 | }
|
735 | field(field) {
|
736 | this._data.field = field;
|
737 | return this;
|
738 | }
|
739 | boost(boost) {
|
740 | this._data.boost = boost;
|
741 | return this;
|
742 | }
|
743 | }
|
744 | exports.GeoPolygonSearchQuery = GeoPolygonSearchQuery;
|