UNPKG

2.89 kBJavaScriptView Raw
1"use strict";
2/* eslint jsdoc/require-jsdoc: off */
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.GeoDistanceSearchSort = exports.FieldSearchSort = exports.IdSearchSort = exports.ScoreSearchSort = exports.SearchSort = void 0;
5/**
6 * Provides the ability to specify sorting for a search query.
7 *
8 * @category Full Text Search
9 */
10class SearchSort {
11 constructor(data) {
12 if (!data) {
13 data = {};
14 }
15 this._data = data;
16 }
17 toJSON() {
18 return this._data;
19 }
20 static score() {
21 return new ScoreSearchSort();
22 }
23 static id() {
24 return new IdSearchSort();
25 }
26 static field(field) {
27 return new FieldSearchSort(field);
28 }
29 static geoDistance(field, lat, lon) {
30 return new GeoDistanceSearchSort(field, lat, lon);
31 }
32}
33exports.SearchSort = SearchSort;
34/**
35 * Provides sorting for a search query by score.
36 *
37 * @category Full Text Search
38 */
39class ScoreSearchSort extends SearchSort {
40 /**
41 * @internal
42 */
43 constructor() {
44 super({
45 by: 'score',
46 });
47 }
48 descending(descending) {
49 this._data.desc = descending;
50 return this;
51 }
52}
53exports.ScoreSearchSort = ScoreSearchSort;
54/**
55 * Provides sorting for a search query by document id.
56 *
57 * @category Full Text Search
58 */
59class IdSearchSort extends SearchSort {
60 /**
61 * @internal
62 */
63 constructor() {
64 super({
65 by: 'id',
66 });
67 }
68 descending(descending) {
69 this._data.desc = descending;
70 return this;
71 }
72}
73exports.IdSearchSort = IdSearchSort;
74/**
75 * Provides sorting for a search query by a specified field.
76 *
77 * @category Full Text Search
78 */
79class FieldSearchSort extends SearchSort {
80 /**
81 * @internal
82 */
83 constructor(field) {
84 super({
85 by: 'field',
86 field: field,
87 });
88 }
89 type(type) {
90 this._data.type = type;
91 return this;
92 }
93 mode(mode) {
94 this._data.mode = mode;
95 return this;
96 }
97 missing(missing) {
98 this._data.missing = missing;
99 return this;
100 }
101 descending(descending) {
102 this._data.desc = descending;
103 return this;
104 }
105}
106exports.FieldSearchSort = FieldSearchSort;
107/**
108 * Provides sorting for a search query by geographic distance from a point.
109 *
110 * @category Full Text Search
111 */
112class GeoDistanceSearchSort extends SearchSort {
113 /**
114 * @internal
115 */
116 constructor(field, lat, lon) {
117 super({
118 by: 'geo_distance',
119 field: field,
120 location: [lon, lat],
121 });
122 }
123 unit(unit) {
124 this._data.unit = unit;
125 return this;
126 }
127 descending(descending) {
128 this._data.desc = descending;
129 return this;
130 }
131}
132exports.GeoDistanceSearchSort = GeoDistanceSearchSort;