UNPKG

couchbase

Version:

The official Couchbase Node.js Client Library.

534 lines (533 loc) 22.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScopeSearchIndexManager = void 0; const utilities_1 = require("./utilities"); const searchindexmanager_1 = require("./searchindexmanager"); const observability_1 = require("./observability"); const observabilityhandler_1 = require("./observabilityhandler"); const observabilitytypes_1 = require("./observabilitytypes"); /** * SearchIndexManager provides an interface for managing the * search indexes on the cluster. * * @category Management */ class ScopeSearchIndexManager { /** * @internal */ constructor(cluster, bucketName, scopeName) { this._cluster = cluster; this._bucketName = bucketName; this._scopeName = scopeName; } /** * @internal */ get observabilityInstruments() { return this._cluster.observabilityInstruments; } /** * Returns an index by it's name. * * @param indexName The index to retrieve. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async getIndex(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexGet, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexGet.bind(this._cluster.conn), { index_name: indexName, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return searchindexmanager_1.SearchIndex._fromCppData(resp.index); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Returns a list of all existing indexes. * * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async getAllIndexes(options, callback) { if (options instanceof Function) { callback = arguments[0]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexGetAll, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexGetAll.bind(this._cluster.conn), { bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return resp.indexes.map((indexData) => searchindexmanager_1.SearchIndex._fromCppData(indexData)); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Creates or updates an existing index. * * @param indexDefinition The index to update. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async upsertIndex(indexDefinition, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexUpsert, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexUpsert.bind(this._cluster.conn), { index: searchindexmanager_1.SearchIndex._toCppData(indexDefinition), bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Drops an index. * * @param indexName The name of the index to drop. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async dropIndex(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexDrop, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexDrop.bind(this._cluster.conn), { index_name: indexName, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Returns the number of documents that have been indexed. * * @param indexName The name of the index to return the count for. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async getIndexedDocumentsCount(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexGetDocumentsCount, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexGetDocumentsCount.bind(this._cluster.conn), { index_name: indexName, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return resp.count; }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Pauses the ingestion of documents into an index. * * @param indexName The name of the index to pause. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async pauseIngest(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexPauseIngest, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlIngest.bind(this._cluster.conn), { index_name: indexName, pause: true, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Resumes the ingestion of documents into an index. * * @param indexName The name of the index to resume. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async resumeIngest(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexResumeIngest, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlIngest.bind(this._cluster.conn), { index_name: indexName, pause: false, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Enables querying of an index. * * @param indexName The name of the index to enable querying for. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async allowQuerying(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexAllowQuerying, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlQuery.bind(this._cluster.conn), { index_name: indexName, allow: true, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Disables querying of an index. * * @param indexName The name of the index to disable querying for. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async disallowQuerying(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexDisallowQuerying, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlQuery.bind(this._cluster.conn), { index_name: indexName, allow: false, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Freezes the indexing plan for execution of queries. * * @param indexName The name of the index to freeze the plan of. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async freezePlan(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexFreezePlan, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlPlanFreeze.bind(this._cluster.conn), { index_name: indexName, freeze: true, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Unfreezes the indexing plan for execution of queries. * * @param indexName The name of the index to freeze the plan of. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async unfreezePlan(indexName, options, callback) { if (options instanceof Function) { callback = arguments[1]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexUnfreezePlan, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, _] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexControlPlanFreeze.bind(this._cluster.conn), { index_name: indexName, freeze: false, bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } /** * Performs analysis of a specific document by an index. * * @param indexName The name of the index to use for the analysis. * @param document The document to analyze. * @param options Optional parameters for this operation. * @param callback A node-style callback to be invoked after execution. */ async analyzeDocument(indexName, document, options, callback) { if (options instanceof Function) { callback = arguments[2]; options = undefined; } if (!options) { options = {}; } const obsReqHandler = new observabilityhandler_1.ObservableRequestHandler(observabilitytypes_1.SearchIndexMgmtOp.SearchIndexAnalyzeDocument, this.observabilityInstruments, options === null || options === void 0 ? void 0 : options.parentSpan); obsReqHandler.setRequestHttpAttributes({ bucketName: this._bucketName, scopeName: this._scopeName, }); try { const timeout = options.timeout || this._cluster.managementTimeout; return utilities_1.PromiseHelper.wrapAsync(async () => { const [err, resp] = await (0, observability_1.wrapObservableBindingCall)(this._cluster.conn.managementSearchIndexAnalyzeDocument.bind(this._cluster.conn), { index_name: indexName, encoded_document: JSON.stringify(document), bucket_name: this._bucketName, scope_name: this._scopeName, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return JSON.parse(resp.analysis); }, callback); } catch (err) { obsReqHandler.endWithError(err); throw err; } } } exports.ScopeSearchIndexManager = ScopeSearchIndexManager;