diff --git a/CHANGELOG.md b/CHANGELOG.md index 88edecb21..5edffc138 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added +- Added Rollups API ([744](https://github.com/opensearch-project/opensearch-js/issues/744)) +- Added Transforms API ([744](https://github.com/opensearch-project/opensearch-js/issues/744)) ### Dependencies - Bumps `@types/node` from 20.14.0 to 20.14.2 - Bumps `@babel/eslint-parser` from 7.24.6 to 7.24.7 @@ -247,4 +249,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Bumps `tsd` from 0.22.0 to 0.24.1 - Bumps `semver` from 7.3.7 to 7.3.8 ### Fixed -- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) \ No newline at end of file +- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) diff --git a/api/api/rollups.js b/api/api/rollups.js new file mode 100644 index 000000000..b18660c8a --- /dev/null +++ b/api/api/rollups.js @@ -0,0 +1,249 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * This file was generated from OpenSearch API Spec. Do not edit it + * manually. If you want to make changes, either update the spec or + * the API generator. + */ + +'use strict'; + +/* eslint camelcase: 0 */ +/* eslint no-unused-vars: 0 */ + +const { + handleError, + encodePathParam, + normalizeArguments, + kConfigurationError, +} = require('../utils'); + +/** @namespace API-Rollups */ + +function RollupsApi(transport, ConfigurationError) { + this.transport = transport; + this[kConfigurationError] = ConfigurationError; +} +/** + * Delete index rollup. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#delete-an-index-rollup-job - rollups.delete} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {string} params.id - Rollup to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.delete = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id].filter((c) => c != null).join('/'); + method = 'DELETE'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Get an index rollup. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#get-an-index-rollup-job - rollups.get} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {string} params.id - Rollup to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.get = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Create or update index rollup. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#create-or-update-an-index-rollup-job - rollups.put} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {number} [params.if_seq_no] - Only perform the operation if the document has this sequence number. + * @param {number} [params.if_primary_term] - Only perform the operation if the document has this primary term. + * @param {string} params.id - Rollup to access + * @param {object} [params.body] + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.put = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id].filter((c) => c != null).join('/'); + method = 'PUT'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Get a rollup's current status. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#explain-an-index-rollup-job - rollups.explain} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {string} params.id - Rollup to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.explain = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id, '_explain'].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Start rollup. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#start-or-stop-an-index-rollup-job - rollups.start} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {string} params.id - Rollup to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.start = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id, '_start'].filter((c) => c != null).join('/'); + method = 'POST'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Stop rollup. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-rollups/rollup-api/#start-or-stop-an-index-rollup-job - rollups.stop} + * + * @memberOf API-Rollups + * + * @param {object} params + * @param {string} params.id - Rollup to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +RollupsApi.prototype.stop = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_rollup', 'jobs', id, '_stop'].filter((c) => c != null).join('/'); + method = 'POST'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +module.exports = RollupsApi; diff --git a/api/api/transforms.js b/api/api/transforms.js new file mode 100644 index 000000000..007138530 --- /dev/null +++ b/api/api/transforms.js @@ -0,0 +1,304 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + */ + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * This file was generated from OpenSearch API Spec. Do not edit it + * manually. If you want to make changes, either update the spec or + * the API generator. + */ + +'use strict'; + +/* eslint camelcase: 0 */ +/* eslint no-unused-vars: 0 */ + +const { + handleError, + encodePathParam, + normalizeArguments, + kConfigurationError, +} = require('../utils'); + +/** @namespace API-Transforms */ + +function TransformsApi(transport, ConfigurationError) { + this.transport = transport; + this[kConfigurationError] = ConfigurationError; +} + +/** + * Returns the details of all transform jobs. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#get-a-transform-jobs-details - transforms.search} + * + * @memberOf API-Transforms + * + * @param {object} [params] + * @param {number} [params.size] - Specifies the number of transforms to return. Default is 10. + * @param {number} [params.from] - The starting transform to return. Default is 0. + * @param {string} [params.search] - The search term to use to filter results. + * @param {string} [params.sortField] - The field to sort results with. + * @param {string} [params.sortDirection] - Specifies the direction to sort results in. Can be ASC or DESC. Default is ASC. + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.search = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + let { method, body, ...querystring } = params; + + let path = ['_plugins', '_transform'].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Returns a preview of what a transformed index would look like. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#preview-a-transform-jobs-results - transforms.preview} + * + * @memberOf API-Transforms + * + * @param {object} [params] - (Unused) + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.preview = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + let { method, body, ...querystring } = params; + + let path = ['_plugins', '_transform', '_preview'].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Delete an index transform. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#delete-a-transform-job - transforms.delete} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {string} params.id - Transform to delete + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.delete = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id].filter((c) => c != null).join('/'); + method = 'DELETE'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Returns the status and metadata of a transform job. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#get-a-transform-jobs-details - transforms.get} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {string} params.id - Transform to access + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.get = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Create an index transform, or update a transform if if_seq_no and if_primary_term are provided. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#create-a-transform-job - transforms.put} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {number} [params.if_seq_no] - Only perform the operation if the document has this sequence number. + * @param {number} [params.if_primary_term] - Only perform the operation if the document has this primary term. + * @param {string} params.id - Transform to create/update + * @param {object} [params.body] + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.put = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id].filter((c) => c != null).join('/'); + method = 'PUT'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Returns the status and metadata of a transform job. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#get-the-status-of-a-transform-job - transforms.explain} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {string} params.id - Transform to explain + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.explain = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id, '_explain'].filter((c) => c != null).join('/'); + method = 'GET'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * Start transform. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#start-a-transform-job - transforms.start} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {string} params.id - Transform to start + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.start = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id, '_start'].filter((c) => c != null).join('/'); + method = 'POST'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +/** + * stop transform. + *
See Also: {@link https://opensearch.org/docs/latest/im-plugin/index-transforms/transforms-apis/#stop-a-transform-job - transforms.stop} + * + * @memberOf API-Transforms + * + * @param {object} params + * @param {string} params.id - Transform to stop + * + * @param {TransportRequestOptions} [options] - Options for {@link Transport#request} + * @param {function} [callback] - Callback that handles errors and response + * + * @returns {{abort: function(), then: function(), catch: function()}|Promise|*} + */ +TransformsApi.prototype.stop = function (params, options, callback) { + [params, options, callback] = normalizeArguments(params, options, callback); + + if (params.id == null) { + const err = new this[kConfigurationError]('Missing required parameter: id'); + return handleError(err, callback); + } + + let { method, body, id, ...querystring } = params; + id = encodePathParam(id); + + let path = ['_plugins', '_transform', id, '_stop'].filter((c) => c != null).join('/'); + method = 'POST'; + body = body || ''; + + return this.transport.request({ method, path, querystring, body }, options, callback); +}; + +module.exports = TransformsApi; diff --git a/api/index.js b/api/index.js index 63bed5eec..cd250e5d2 100644 --- a/api/index.js +++ b/api/index.js @@ -70,6 +70,7 @@ const rankEvalApi = require('./api/rank_eval'); const reindexApi = require('./api/reindex'); const reindexRethrottleApi = require('./api/reindex_rethrottle'); const renderSearchTemplateApi = require('./api/render_search_template'); +const RollupsApi = require('./api/rollups'); const scriptsPainlessExecuteApi = require('./api/scripts_painless_execute'); const scrollApi = require('./api/scroll'); const SecurityApi = require('./api/security'); @@ -81,6 +82,7 @@ const SnapshotApi = require('./api/snapshot'); const TasksApi = require('./api/tasks'); const termsEnumApi = require('./api/terms_enum'); const termvectorsApi = require('./api/termvectors'); +const TransformsAPi = require('./api/transforms'); const updateApi = require('./api/update'); const updateByQueryApi = require('./api/update_by_query'); const updateByQueryRethrottleApi = require('./api/update_by_query_rethrottle'); @@ -94,10 +96,12 @@ const kHttp = Symbol('Http'); const kIndices = Symbol('Indices'); const kIngest = Symbol('Ingest'); const kNodes = Symbol('Nodes'); +const kRollups = Symbol('Rollups'); const kSecurity = Symbol('Security'); const kShutdown = Symbol('Shutdown'); const kSnapshot = Symbol('Snapshot'); const kTasks = Symbol('Tasks'); +const kTransforms = Symbol('Transforms'); function OpenSearchAPI(opts) { this[kConfigurationError] = opts.ConfigurationError; @@ -109,10 +113,12 @@ function OpenSearchAPI(opts) { this[kIndices] = null; this[kIngest] = null; this[kNodes] = null; + this[kRollups] = null; this[kSecurity] = null; this[kShutdown] = null; this[kSnapshot] = null; this[kTasks] = null; + this[kTransforms] = null; } OpenSearchAPI.prototype.bulk = bulkApi; @@ -324,6 +330,14 @@ Object.defineProperties(OpenSearchAPI.prototype, { return this.renderSearchTemplate; }, }, + rollups: { + get() { + if (this[kRollups] === null) { + this[kRollups] = new RollupsApi(this.transport, this[kConfigurationError]); + } + return this[kRollups]; + }, + }, scripts_painless_execute: { get() { return this.scriptsPainlessExecute; @@ -376,6 +390,14 @@ Object.defineProperties(OpenSearchAPI.prototype, { return this.termsEnum; }, }, + transforms: { + get() { + if (this[kTransforms] === null) { + this[kTransforms] = new TransformsAPi(this.transport, this[kConfigurationError]); + } + return this[kTransforms]; + }, + }, update_by_query: { get() { return this.updateByQuery; diff --git a/index.d.ts b/index.d.ts index 9a12e8c16..67752eddd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4621,6 +4621,104 @@ declare class Client { options: TransportRequestOptions, callback: callbackFn ): TransportRequestCallback; + rollups: { + delete, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + delete, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + delete, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + delete, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + get, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + put, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + explain, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + start, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + stop, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + }; scripts_painless_execute< TResponse = Record, TRequestBody extends RequestBody = Record, @@ -5473,6 +5571,136 @@ declare class Client { options: TransportRequestOptions, callback: callbackFn ): TransportRequestCallback; + transforms: { + search, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + search, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + search, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + search, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + preview, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + preview, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + preview, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + preview, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + delete, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + delete, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + delete, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + delete, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + get, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + get, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + put, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + put, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + explain, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + explain, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + start, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + start, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params?: Record, + options?: TransportRequestOptions + ): TransportRequestPromise>; + stop, TContext = Context>( + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params: Record, + callback: callbackFn + ): TransportRequestCallback; + stop, TContext = Context>( + params: Record, + options: TransportRequestOptions, + callback: callbackFn + ): TransportRequestCallback; + }; update< TResponse = Record, TRequestBody extends RequestBody = Record, diff --git a/package.json b/package.json index cc99e6414..3aed95bb8 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "eslint": "^8.30.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", + "faker": "^5.5.3", "fast-deep-equal": "^3.1.3", "into-stream": "^6.0.0", "js-yaml": "^4.1.0", @@ -98,9 +99,9 @@ "xmlbuilder2": "^3.0.2" }, "dependencies": { - "json11": "^1.1.2", "aws4": "^1.11.0", "debug": "^4.3.1", + "json11": "^1.1.2", "hpagent": "^1.2.0", "ms": "^2.1.3", "secure-json-parse": "^2.4.0" diff --git a/samples/rollups.js b/samples/rollups.js new file mode 100644 index 000000000..55e6345c0 --- /dev/null +++ b/samples/rollups.js @@ -0,0 +1,139 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + */ + +const { Client } = require('@opensearch-project/opensearch'); + +// Instantiate a client with basic auth as setup in the README.md file +const client = new Client({ + ssl: { + rejectUnauthorized: false, + }, + node: 'https://localhost:9200', + auth: { + username: 'admin', + password: 'myStrongPassword123!', + }, +}); + +const start = async () => { + const sourceIndex = 'sample-index'; + const rollupIndex = 'rollup-index'; + const jobId = 'my-rollup-job'; + + /////////////////// SETUP ////////////////// + // Create source index + await client.indices.create({ + index: sourceIndex, + body: { + mappings: { + properties: { + timestamp: { type: 'date' }, + field1: { type: 'keyword' }, + field2: { type: 'integer' }, + }, + }, + }, + }); + + // Add some documents to the index + const now = Date.now(); + for (let i = 0; i < 100; i++) { + await client.index({ + index: sourceIndex, + body: { + timestamp: new Date(now - i * 3600000), // Subtract 1 hour for each document + field1: `field1_value_${i % 10}`, // Cycle through 10 different values + field2: i, + }, + }); + } + + // Refresh the index to make sure all documents are searchable + await client.indices.refresh({ index: sourceIndex }); + //////////// ROLLUP OPERATIONS //////////// + // Create a rollup job + await client.rollups.put({ + id: jobId, + body: { + rollup: { + description: 'sample rollup', + source_index: sourceIndex, + target_index: rollupIndex, + schedule: { + interval: { + period: 1, + unit: 'Minutes', + start_time: 1602100553, + }, + }, + page_size: 10, + delay: '0', + continuous: false, + dimensions: [ + { + date_histogram: { + source_field: 'timestamp', + fixed_interval: '1440m', + timezone: 'UTC', + }, + }, + { + terms: { + source_field: 'field1', + }, + }, + ], + metrics: [ + { + source_field: 'field2', + metrics: [ + { + avg: {}, + }, + { + max: {}, + }, + ], + }, + ], + }, + }, + }); + + // Start the rollup job + await client.rollups.start({ id: jobId }); + + // Check the rollup job + const status = await client.rollups.get({ id: jobId }); + console.log('Running Rollup Job:'); + console.log(status.body); + + // Wait till the rollup index is created + while (!(await client.indices.exists({ index: rollupIndex })).body) { + await new Promise((resolve) => setTimeout(resolve, 5000)); + } + + // Explain the rollup job + const explain = await client.rollups.explain({ id: jobId }); + console.log('Rollup Job Explanation:'); + console.log(explain.body); + + // Stop the rollup job + await client.rollups.stop({ id: jobId }); + + // Delete the rollup job + await client.rollups.delete({ id: jobId }); + + ///////////////// TEAR DOWN /////////////// + await client.indices.delete({ index: sourceIndex }); + await client.indices.delete({ index: rollupIndex }); +}; + +start().then(() => console.log('done')); diff --git a/samples/transforms.ts b/samples/transforms.ts new file mode 100644 index 000000000..df86ade8c --- /dev/null +++ b/samples/transforms.ts @@ -0,0 +1,146 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + */ + +// const {Client} = require('../index.js') +const { Client } = require('@opensearch-project/opensearch'); + +const faker = require('faker'); + +// Instantiate a client with basic auth as setup in the README.md file +const client = new Client({ + ssl: { + rejectUnauthorized: false, + }, + node: 'https://localhost:9200', + auth: { + username: 'admin', + password: 'myStrongPassword123!', + }, +}); + +const start = async () => { + const sourceIndex = 'sample-index'; + const transformIndex = 'transform-index'; + const transformJob = 'my-transform-job'; + + /////////////////// SETUP ////////////////// + // Create source index + await client.indices.create({ + index: sourceIndex, + body: { + mappings: { + properties: { + name: { type: 'keyword' }, + age: { type: 'integer' }, + gender: { type: 'keyword' }, + salary: { type: 'integer' }, + }, + }, + }, + }); + + // Create 100 documents + for (let i = 0; i < 100; i++) { + // Generate random data for each field + const name = faker.name.findName(); // generates a random name + const age = faker.datatype.number({ min: 20, max: 60 }); // generates a random age between 20 and 60 + const gender = faker.random.arrayElement(['Male', 'Female']); // randomly selects 'Male' or 'Female' + const salary = faker.datatype.number({ min: 30000, max: 100000 }); // generates a random salary between 30000 and 100000 + + // Index the document + await client.index({ + index: sourceIndex, + body: { + name, + age, + gender, + salary, + }, + }); + } + + // Refresh the index to make sure all documents are searchable + await client.indices.refresh({ index: sourceIndex }); + + //////////// TRANSFORM OPERATIONS //////////// + // Create transform + await client.transforms.put({ + id: transformJob, + body: { + transform: { + enabled: true, + continuous: true, + schedule: { + interval: { + period: 1, + unit: 'Minutes', + start_time: 0 + } + }, + description: 'Sample transform job', + source_index: sourceIndex, + target_index: transformIndex, + data_selection_query: { + match_all: {} + }, + page_size: 1, + groups: [ + { + terms: { + source_field: 'gender', + target_field: 'gender' + } + }, + { + terms: { + source_field: 'age', + target_field: 'age' + } + } + ], + aggregations: { + quantity: { + sum: { + field: 'salary' + } + } + } + } + }, + }); + + // Start transform + await client.transforms.start({ id: transformJob }); + + // Get transform + const transform = await client.transforms.get({ id: transformJob }); + console.log(transform.body); + + // Wait till the transform index is created + while (!(await client.indices.exists({ index: transformIndex })).body) { + await new Promise((resolve) => setTimeout(resolve, 5000)); + } + + // Get transform status + const explain = await client.transforms.explain({ id: transformJob }); + console.log(explain.body); + + // Stop transform + await client.transforms.stop({ id: transformJob }); + + // Delete the transform job + await client.transforms.delete({ id: transformJob }); + + ///////////////// TEAR DOWN /////////////// + await client.indices.delete({ index: sourceIndex }); + // await client.indices.delete({ index: transformIndex }); +}; + +start().then(() => console.log('done'));