UNPKG

17.2 kBJavaScriptView Raw
1"use strict";
2/* eslint jsdoc/require-jsdoc: off */
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.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 * Specifies how the individual match terms should be logically concatenated.
7 *
8 * @experimental This API is subject to change without notice.
9 * @category Full Text Search
10 */
11var MatchOperator;
12(function (MatchOperator) {
13 /**
14 * Specifies that individual match terms are concatenated with a logical OR - this is the default if not provided.
15 */
16 MatchOperator["Or"] = "or";
17 /**
18 * Specifies that individual match terms are concatenated with a logical AND.
19 */
20 MatchOperator["And"] = "and";
21})(MatchOperator || (exports.MatchOperator = MatchOperator = {}));
22function _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 * @internal
39 */
40function _unpackListArgs(args) {
41 if (Array.isArray(args[0])) {
42 return args[0];
43 }
44 return args;
45}
46/**
47 * Provides the ability to specify the query for a search query.
48 *
49 * @category Full Text Search
50 */
51class SearchQuery {
52 constructor(data) {
53 if (!data) {
54 data = {};
55 }
56 this._data = data;
57 }
58 toJSON() {
59 return this._data;
60 }
61 /**
62 * @internal
63 */
64 static toJSON(query) {
65 if (query.toJSON) {
66 return query.toJSON();
67 }
68 return query;
69 }
70 /**
71 * @internal
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 * @internal
97 */
98 static conjuncts(...args) {
99 const queries = _unpackListArgs(args);
100 return new ConjunctionSearchQuery(...queries);
101 }
102 /**
103 * @internal
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 * @internal
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}
150exports.SearchQuery = SearchQuery;
151/**
152 * Represents a match search query.
153 *
154 * @category Full Text Search
155 */
156class MatchSearchQuery extends SearchQuery {
157 /**
158 * @internal
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}
190exports.MatchSearchQuery = MatchSearchQuery;
191/**
192 * Represents a match-phrase search query.
193 *
194 * @category Full Text Search
195 */
196class MatchPhraseSearchQuery extends SearchQuery {
197 /**
198 * @internal
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}
218exports.MatchPhraseSearchQuery = MatchPhraseSearchQuery;
219/**
220 * Represents a regexp search query.
221 *
222 * @category Full Text Search
223 */
224class RegexpSearchQuery extends SearchQuery {
225 /**
226 * @internal
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}
242exports.RegexpSearchQuery = RegexpSearchQuery;
243/**
244 * Represents a query-string search query.
245 *
246 * @category Full Text Search
247 */
248class QueryStringSearchQuery extends SearchQuery {
249 /**
250 * @internal
251 */
252 constructor(query) {
253 super({
254 query: query,
255 });
256 }
257 boost(boost) {
258 this._data.boost = boost;
259 return this;
260 }
261}
262exports.QueryStringSearchQuery = QueryStringSearchQuery;
263/**
264 * Represents a numeric-range search query.
265 *
266 * @category Full Text Search
267 */
268class NumericRangeSearchQuery extends SearchQuery {
269 /**
270 * @internal
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}
300exports.NumericRangeSearchQuery = NumericRangeSearchQuery;
301/**
302 * Represents a date-range search query.
303 *
304 * @category Full Text Search
305 */
306class DateRangeSearchQuery extends SearchQuery {
307 /**
308 * @internal
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}
352exports.DateRangeSearchQuery = DateRangeSearchQuery;
353/**
354 * Represents a conjunction search query.
355 *
356 * @category Full Text Search
357 */
358class ConjunctionSearchQuery extends SearchQuery {
359 /**
360 * @internal
361 */
362 constructor(...queries) {
363 super({
364 conjuncts: [],
365 });
366 this.and(...queries);
367 }
368 /**
369 * @internal
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}
383exports.ConjunctionSearchQuery = ConjunctionSearchQuery;
384/**
385 * Represents a disjunction search query.
386 *
387 * @category Full Text Search
388 */
389class DisjunctionSearchQuery extends SearchQuery {
390 /**
391 * @internal
392 */
393 constructor(...queries) {
394 super({
395 disjuncts: [],
396 });
397 this.or(...queries);
398 }
399 /**
400 * @internal
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}
414exports.DisjunctionSearchQuery = DisjunctionSearchQuery;
415/**
416 * Represents a boolean search query.
417 *
418 * @category Full Text Search
419 */
420class BooleanSearchQuery extends SearchQuery {
421 /**
422 * @internal
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}
474exports.BooleanSearchQuery = BooleanSearchQuery;
475/**
476 * Represents a wildcard search query.
477 *
478 * @category Full Text Search
479 */
480class WildcardSearchQuery extends SearchQuery {
481 /**
482 * @internal
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}
498exports.WildcardSearchQuery = WildcardSearchQuery;
499/**
500 * Represents a document-id search query.
501 *
502 * @category Full Text Search
503 */
504class DocIdSearchQuery extends SearchQuery {
505 /**
506 * @internal
507 */
508 constructor(...ids) {
509 super({
510 ids: [],
511 });
512 this.addDocIds(...ids);
513 }
514 /**
515 * @internal
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}
533exports.DocIdSearchQuery = DocIdSearchQuery;
534/**
535 * Represents a boolean-field search query.
536 *
537 * @category Full Text Search
538 */
539class BooleanFieldSearchQuery extends SearchQuery {
540 /**
541 * @internal
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}
557exports.BooleanFieldSearchQuery = BooleanFieldSearchQuery;
558/**
559 * Represents a term search query.
560 *
561 * @category Full Text Search
562 */
563class TermSearchQuery extends SearchQuery {
564 /**
565 * @internal
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}
589exports.TermSearchQuery = TermSearchQuery;
590/**
591 * Represents a phrase search query.
592 *
593 * @category Full Text Search
594 */
595class PhraseSearchQuery extends SearchQuery {
596 /**
597 * @internal
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}
613exports.PhraseSearchQuery = PhraseSearchQuery;
614/**
615 * Represents a prefix search query.
616 *
617 * @category Full Text Search
618 */
619class PrefixSearchQuery extends SearchQuery {
620 /**
621 * @internal
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}
637exports.PrefixSearchQuery = PrefixSearchQuery;
638/**
639 * Represents a match-all search query.
640 *
641 * @category Full Text Search
642 */
643class MatchAllSearchQuery extends SearchQuery {
644 /**
645 * @internal
646 */
647 constructor() {
648 super({
649 match_all: null,
650 });
651 }
652}
653exports.MatchAllSearchQuery = MatchAllSearchQuery;
654/**
655 * Represents a match-none search query.
656 *
657 * @category Full Text Search
658 */
659class MatchNoneSearchQuery extends SearchQuery {
660 /**
661 * @internal
662 */
663 constructor() {
664 super({
665 match_none: true,
666 });
667 }
668}
669exports.MatchNoneSearchQuery = MatchNoneSearchQuery;
670/**
671 * Represents a geo-distance search query.
672 *
673 * @category Full Text Search
674 */
675class GeoDistanceSearchQuery extends SearchQuery {
676 /**
677 * @internal
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}
694exports.GeoDistanceSearchQuery = GeoDistanceSearchQuery;
695/**
696 * Represents a geo-bounding-box search query.
697 *
698 * @category Full Text Search
699 */
700class GeoBoundingBoxSearchQuery extends SearchQuery {
701 /**
702 * @internal
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}
719exports.GeoBoundingBoxSearchQuery = GeoBoundingBoxSearchQuery;
720/**
721 * Represents a geo-polygon search query.
722 *
723 * @category Full Text Search
724 */
725class GeoPolygonSearchQuery extends SearchQuery {
726 /**
727 * @internal
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}
744exports.GeoPolygonSearchQuery = GeoPolygonSearchQuery;