UNPKG

couchbase

Version:

The official Couchbase Node.js Client Library.

536 lines (535 loc) 21.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SearchIndexManager = exports.SearchIndex = void 0; const utilities_1 = require("./utilities"); const observability_1 = require("./observability"); const observabilityhandler_1 = require("./observabilityhandler"); const observabilitytypes_1 = require("./observabilitytypes"); /** * This class is currently incomplete and must be casted to `any` in * TypeScript to be used. * * @category Management */ class SearchIndex { /** * @internal */ constructor(data) { this.uuid = data.uuid; this.name = data.name; this.sourceName = data.sourceName; this.type = data.type; this.params = data.params; this.sourceUuid = data.sourceUuid; this.sourceParams = data.sourceParams; this.sourceType = data.sourceType; this.planParams = data.planParams; } /** * @internal */ static _toCppData(data) { return { uuid: data.uuid, name: data.name, type: data.type, params_json: JSON.stringify(data.params), source_uuid: data.sourceUuid, source_name: data.sourceName, source_type: data.sourceType, source_params_json: JSON.stringify(data.sourceParams), plan_params_json: JSON.stringify(data.planParams), }; } /** * @internal */ static _fromCppData(data) { const idx = new SearchIndex({ uuid: data.uuid, name: data.name, type: data.type, params: {}, sourceUuid: data.source_uuid, sourceName: data.source_name, sourceType: data.source_type, sourceParams: {}, planParams: {}, }); if (data.params_json) { idx.params = JSON.parse(data.params_json); } if (data.source_params_json) { idx.sourceParams = JSON.parse(data.source_params_json); } if (data.plan_params_json) { idx.planParams = JSON.parse(data.plan_params_json); } return idx; } } exports.SearchIndex = SearchIndex; /** * SearchIndexManager provides an interface for managing the * search indexes on the cluster. * * @category Management */ class SearchIndexManager { /** * @internal */ constructor(cluster) { this._cluster = cluster; } /** * @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(); 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, timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return 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(); 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), { timeout: timeout, }, obsReqHandler); if (err) { obsReqHandler.endWithError(err); throw err; } obsReqHandler.end(); return resp.indexes.map((indexData) => 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(); 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: SearchIndex._toCppData(indexDefinition), 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(); 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, 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(); 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, 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(); 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, 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(); 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, 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(); 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, 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(); 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, 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(); 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, 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(); 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, 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(); 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), 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.SearchIndexManager = SearchIndexManager;