UNPKG

15.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ScopeSearchIndexManager = void 0;
4const utilities_1 = require("./utilities");
5const bindingutilities_1 = require("./bindingutilities");
6const searchindexmanager_1 = require("./searchindexmanager");
7/**
8 * SearchIndexManager provides an interface for managing the
9 * search indexes on the cluster.
10 *
11 * @category Management
12 */
13class ScopeSearchIndexManager {
14 /**
15 * @internal
16 */
17 constructor(cluster, bucketName, scopeName) {
18 this._cluster = cluster;
19 this._bucketName = bucketName;
20 this._scopeName = scopeName;
21 }
22 /**
23 * Returns an index by it's name.
24 *
25 * @param indexName The index to retrieve.
26 * @param options Optional parameters for this operation.
27 * @param callback A node-style callback to be invoked after execution.
28 */
29 async getIndex(indexName, options, callback) {
30 if (options instanceof Function) {
31 callback = arguments[1];
32 options = undefined;
33 }
34 if (!options) {
35 options = {};
36 }
37 const timeout = options.timeout || this._cluster.managementTimeout;
38 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
39 this._cluster.conn.managementSearchIndexGet({
40 index_name: indexName,
41 timeout: timeout,
42 bucket_name: this._bucketName,
43 scope_name: this._scopeName,
44 }, (cppErr, resp) => {
45 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
46 if (err) {
47 return wrapCallback(err, null);
48 }
49 const index = searchindexmanager_1.SearchIndex._fromCppData(resp.index);
50 wrapCallback(null, index);
51 });
52 }, callback);
53 }
54 /**
55 * Returns a list of all existing indexes.
56 *
57 * @param options Optional parameters for this operation.
58 * @param callback A node-style callback to be invoked after execution.
59 */
60 async getAllIndexes(options, callback) {
61 if (options instanceof Function) {
62 callback = arguments[0];
63 options = undefined;
64 }
65 if (!options) {
66 options = {};
67 }
68 const timeout = options.timeout || this._cluster.managementTimeout;
69 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
70 this._cluster.conn.managementSearchIndexGetAll({
71 timeout: timeout,
72 bucket_name: this._bucketName,
73 scope_name: this._scopeName,
74 }, (cppErr, resp) => {
75 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
76 if (err) {
77 return wrapCallback(err, null);
78 }
79 const indexes = resp.indexes.map((indexData) => searchindexmanager_1.SearchIndex._fromCppData(indexData));
80 wrapCallback(null, indexes);
81 });
82 }, callback);
83 }
84 /**
85 * Creates or updates an existing index.
86 *
87 * @param indexDefinition The index to update.
88 * @param options Optional parameters for this operation.
89 * @param callback A node-style callback to be invoked after execution.
90 */
91 async upsertIndex(indexDefinition, options, callback) {
92 if (options instanceof Function) {
93 callback = arguments[1];
94 options = undefined;
95 }
96 if (!options) {
97 options = {};
98 }
99 const timeout = options.timeout || this._cluster.managementTimeout;
100 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
101 this._cluster.conn.managementSearchIndexUpsert({
102 index: searchindexmanager_1.SearchIndex._toCppData(indexDefinition),
103 timeout: timeout,
104 bucket_name: this._bucketName,
105 scope_name: this._scopeName,
106 }, (cppErr) => {
107 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
108 if (err) {
109 return wrapCallback(err, null);
110 }
111 wrapCallback(err);
112 });
113 }, callback);
114 }
115 /**
116 * Drops an index.
117 *
118 * @param indexName The name of the index to drop.
119 * @param options Optional parameters for this operation.
120 * @param callback A node-style callback to be invoked after execution.
121 */
122 async dropIndex(indexName, options, callback) {
123 if (options instanceof Function) {
124 callback = arguments[1];
125 options = undefined;
126 }
127 if (!options) {
128 options = {};
129 }
130 const timeout = options.timeout || this._cluster.managementTimeout;
131 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
132 this._cluster.conn.managementSearchIndexDrop({
133 index_name: indexName,
134 timeout: timeout,
135 bucket_name: this._bucketName,
136 scope_name: this._scopeName,
137 }, (cppErr) => {
138 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
139 if (err) {
140 return wrapCallback(err, null);
141 }
142 wrapCallback(err);
143 });
144 }, callback);
145 }
146 /**
147 * Returns the number of documents that have been indexed.
148 *
149 * @param indexName The name of the index to return the count for.
150 * @param options Optional parameters for this operation.
151 * @param callback A node-style callback to be invoked after execution.
152 */
153 async getIndexedDocumentsCount(indexName, options, callback) {
154 if (options instanceof Function) {
155 callback = arguments[1];
156 options = undefined;
157 }
158 if (!options) {
159 options = {};
160 }
161 const timeout = options.timeout || this._cluster.managementTimeout;
162 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
163 this._cluster.conn.managementSearchIndexGetDocumentsCount({
164 index_name: indexName,
165 timeout: timeout,
166 bucket_name: this._bucketName,
167 scope_name: this._scopeName,
168 }, (cppErr, resp) => {
169 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
170 if (err) {
171 return wrapCallback(err, null);
172 }
173 wrapCallback(null, resp.count);
174 });
175 }, callback);
176 }
177 /**
178 * Pauses the ingestion of documents into an index.
179 *
180 * @param indexName The name of the index to pause.
181 * @param options Optional parameters for this operation.
182 * @param callback A node-style callback to be invoked after execution.
183 */
184 async pauseIngest(indexName, options, callback) {
185 if (options instanceof Function) {
186 callback = arguments[1];
187 options = undefined;
188 }
189 if (!options) {
190 options = {};
191 }
192 const timeout = options.timeout || this._cluster.managementTimeout;
193 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
194 this._cluster.conn.managementSearchIndexControlIngest({
195 index_name: indexName,
196 pause: true,
197 timeout: timeout,
198 bucket_name: this._bucketName,
199 scope_name: this._scopeName,
200 }, (cppErr) => {
201 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
202 if (err) {
203 return wrapCallback(err, null);
204 }
205 wrapCallback(err);
206 });
207 }, callback);
208 }
209 /**
210 * Resumes the ingestion of documents into an index.
211 *
212 * @param indexName The name of the index to resume.
213 * @param options Optional parameters for this operation.
214 * @param callback A node-style callback to be invoked after execution.
215 */
216 async resumeIngest(indexName, options, callback) {
217 if (options instanceof Function) {
218 callback = arguments[1];
219 options = undefined;
220 }
221 if (!options) {
222 options = {};
223 }
224 const timeout = options.timeout || this._cluster.managementTimeout;
225 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
226 this._cluster.conn.managementSearchIndexControlIngest({
227 index_name: indexName,
228 pause: false,
229 timeout: timeout,
230 bucket_name: this._bucketName,
231 scope_name: this._scopeName,
232 }, (cppErr) => {
233 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
234 if (err) {
235 return wrapCallback(err, null);
236 }
237 wrapCallback(err);
238 });
239 }, callback);
240 }
241 /**
242 * Enables querying of an index.
243 *
244 * @param indexName The name of the index to enable querying for.
245 * @param options Optional parameters for this operation.
246 * @param callback A node-style callback to be invoked after execution.
247 */
248 async allowQuerying(indexName, options, callback) {
249 if (options instanceof Function) {
250 callback = arguments[1];
251 options = undefined;
252 }
253 if (!options) {
254 options = {};
255 }
256 const timeout = options.timeout || this._cluster.managementTimeout;
257 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
258 this._cluster.conn.managementSearchIndexControlQuery({
259 index_name: indexName,
260 allow: true,
261 timeout: timeout,
262 bucket_name: this._bucketName,
263 scope_name: this._scopeName,
264 }, (cppErr) => {
265 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
266 if (err) {
267 return wrapCallback(err, null);
268 }
269 wrapCallback(err);
270 });
271 }, callback);
272 }
273 /**
274 * Disables querying of an index.
275 *
276 * @param indexName The name of the index to disable querying for.
277 * @param options Optional parameters for this operation.
278 * @param callback A node-style callback to be invoked after execution.
279 */
280 async disallowQuerying(indexName, options, callback) {
281 if (options instanceof Function) {
282 callback = arguments[1];
283 options = undefined;
284 }
285 if (!options) {
286 options = {};
287 }
288 const timeout = options.timeout || this._cluster.managementTimeout;
289 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
290 this._cluster.conn.managementSearchIndexControlQuery({
291 index_name: indexName,
292 allow: false,
293 timeout: timeout,
294 bucket_name: this._bucketName,
295 scope_name: this._scopeName,
296 }, (cppErr) => {
297 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
298 if (err) {
299 return wrapCallback(err, null);
300 }
301 wrapCallback(err);
302 });
303 }, callback);
304 }
305 /**
306 * Freezes the indexing plan for execution of queries.
307 *
308 * @param indexName The name of the index to freeze the plan of.
309 * @param options Optional parameters for this operation.
310 * @param callback A node-style callback to be invoked after execution.
311 */
312 async freezePlan(indexName, options, callback) {
313 if (options instanceof Function) {
314 callback = arguments[1];
315 options = undefined;
316 }
317 if (!options) {
318 options = {};
319 }
320 const timeout = options.timeout || this._cluster.managementTimeout;
321 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
322 this._cluster.conn.managementSearchIndexControlPlanFreeze({
323 index_name: indexName,
324 freeze: true,
325 timeout: timeout,
326 bucket_name: this._bucketName,
327 scope_name: this._scopeName,
328 }, (cppErr) => {
329 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
330 if (err) {
331 return wrapCallback(err, null);
332 }
333 wrapCallback(err);
334 });
335 }, callback);
336 }
337 /**
338 * Unfreezes the indexing plan for execution of queries.
339 *
340 * @param indexName The name of the index to freeze the plan of.
341 * @param options Optional parameters for this operation.
342 * @param callback A node-style callback to be invoked after execution.
343 */
344 async unfreezePlan(indexName, options, callback) {
345 if (options instanceof Function) {
346 callback = arguments[1];
347 options = undefined;
348 }
349 if (!options) {
350 options = {};
351 }
352 const timeout = options.timeout || this._cluster.managementTimeout;
353 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
354 this._cluster.conn.managementSearchIndexControlPlanFreeze({
355 index_name: indexName,
356 freeze: false,
357 timeout: timeout,
358 bucket_name: this._bucketName,
359 scope_name: this._scopeName,
360 }, (cppErr) => {
361 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
362 if (err) {
363 return wrapCallback(err, null);
364 }
365 wrapCallback(err);
366 });
367 }, callback);
368 }
369 /**
370 * Performs analysis of a specific document by an index.
371 *
372 * @param indexName The name of the index to use for the analysis.
373 * @param document The document to analyze.
374 * @param options Optional parameters for this operation.
375 * @param callback A node-style callback to be invoked after execution.
376 */
377 async analyzeDocument(indexName, document, options, callback) {
378 if (options instanceof Function) {
379 callback = arguments[2];
380 options = undefined;
381 }
382 if (!options) {
383 options = {};
384 }
385 const timeout = options.timeout || this._cluster.managementTimeout;
386 return utilities_1.PromiseHelper.wrap((wrapCallback) => {
387 this._cluster.conn.managementSearchIndexAnalyzeDocument({
388 index_name: indexName,
389 encoded_document: JSON.stringify(document),
390 timeout: timeout,
391 bucket_name: this._bucketName,
392 scope_name: this._scopeName,
393 }, (cppErr, resp) => {
394 const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
395 if (err) {
396 return wrapCallback(err, null);
397 }
398 const result = JSON.parse(resp.analysis);
399 wrapCallback(result, null);
400 });
401 }, callback);
402 }
403}
404exports.ScopeSearchIndexManager = ScopeSearchIndexManager;