Skip to content

Commit

Permalink
fix: Support script types source and id
Browse files Browse the repository at this point in the history
`source` should be used instead of `inline` and `id` instead of `stored`
on elasticsearch >= 5.0.
  • Loading branch information
sudo-suhas committed Oct 1, 2018
1 parent e171ee4 commit 0ff9f99
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 18 deletions.
79 changes: 68 additions & 11 deletions src/core/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const isNil = require('lodash.isnil');
*
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html)
*
* @param {string=} type One of `inline`, `stored`, `file`
* Note: `inline` script type was deprecated in [elasticsearch v5.0](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/breaking_50_scripting.html).
* `source` should be used instead. And similarly for `stored` scripts, type
* `id` must be used instead. `file` scripts were removed as part of the
* breaking changes in [elasticsearch v6.0](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/breaking_60_scripting_changes.html#_file_scripts_removed)
*
* @param {string=} type One of `inline`, `stored`, `file`, `source`, `id`.
* @param {string=} source Source of the script.
* This needs to be specified if optional argument `type` is passed.
*
Expand Down Expand Up @@ -39,10 +44,18 @@ class Script {
this.inline(source);
break;

case 'source':
this.source(source);
break;

case 'stored':
this.stored(source);
break;

case 'id':
this.id(source);
break;

case 'file':
this.file(source);
break;
Expand Down Expand Up @@ -71,21 +84,26 @@ class Script {
* @private
*/
_checkMixedRepr() {
if (this._isTypeSet) {
this._warn(
'Script source(`inline`/`stored`/`file`) was already specified!'
);
this._warn('Overwriting.');

delete this._body.file;
delete this._body.stored;
delete this._body.file;
}
if (!this._isTypeSet) return;

this._warn(
'Script source(`inline`/`source`/`stored`/`id`/`file`) was already specified!'
);
this._warn('Overwriting.');

delete this._body.inline;
delete this._body.source;
delete this._body.stored;
delete this._body.id;
delete this._body.file;
}

/**
* Sets the type of script to be `inline` and specifies the source of the script.
*
* Note: This type was deprecated in elasticsearch v5.0. Use `source`
* instead if you are using elasticsearch `>= 5.0`.
*
* @param {string} scriptCode
* @returns {Script} returns `this` so that calls can be chained.
*/
Expand All @@ -97,9 +115,30 @@ class Script {
return this;
}

/**
* Sets the type of script to be `source` and specifies the source of the script.
*
* Note: `source` is an alias for the `inline` type which was deprecated
* in elasticsearch v5.0. So this type is supported only in versions
* `>= 5.0`.
*
* @param {string} scriptCode
* @returns {Script} returns `this` so that calls can be chained.
*/
source(scriptCode) {
this._checkMixedRepr();

this._body.source = scriptCode;
this._isTypeSet = true;
return this;
}

/**
* Specify the `stored` script by `id` which will be retrieved from cluster state.
*
* Note: This type was deprecated in elasticsearch v5.0. Use `id`
* instead if you are using elasticsearch `>= 5.0`.
*
* @param {string} scriptId The unique identifier for the stored script.
* @returns {Script} returns `this` so that calls can be chained.
*/
Expand All @@ -111,6 +150,24 @@ class Script {
return this;
}

/**
* Specify the stored script to be used by it's `id` which will be retrieved
* from cluster state.
*
* Note: `id` is an alias for the `stored` type which was deprecated in
* elasticsearch v5.0. So this type is supported only in versions `>= 5.0`.
*
* @param {string} scriptId The unique identifier for the stored script.
* @returns {Script} returns `this` so that calls can be chained.
*/
id(scriptId) {
this._checkMixedRepr();

this._body.id = scriptId;
this._isTypeSet = true;
return this;
}

/**
* Specify the `file` script by stored as a file in the scripts folder.
*
Expand Down
48 changes: 41 additions & 7 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4724,7 +4724,11 @@ declare namespace esb {
* @extends ValuesSourceBase
*/
export class DateHistogramValuesSource extends ValuesSourceBase {
constructor(name: string, field?: string, interval?: string | number);
constructor(
name: string,
field?: string,
interval?: string | number
);

/**
* Sets the histogram interval. Buckets are generated based on this interval value.
Expand Down Expand Up @@ -4752,12 +4756,11 @@ declare namespace esb {
* in the field mapping.
*
* @param {string} fmt Format mask to apply on aggregation response.
* For Date Histograms, supports expressive [date format pattern](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html#date-format-pattern)
* For Date Histograms, supports expressive [date format pattern](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html#date-format-pattern)
*/
format(fmt: string): this;
}


/**
* `DateHistogramValuesSource` is a source for the `CompositeAggregation` that
* handles date histograms. It works very similar to a histogram aggregation
Expand Down Expand Up @@ -6560,9 +6563,7 @@ declare namespace esb {
*
* @param {string} name The name which will be used to refer to this aggregation.
*/
export function bucketSortAggregation(
name: string
): BucketSortAggregation;
export function bucketSortAggregation(name: string): BucketSortAggregation;

/**
* Serial differencing is a technique where values in a time series are
Expand Down Expand Up @@ -7934,7 +7935,12 @@ declare namespace esb {
/**
* Class supporting the Elasticsearch scripting API.
*
* @param {string=} type One of `inline`, `stored`, `file`
* Note: `inline` script type was deprecated in [elasticsearch v5.0](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/breaking_50_scripting.html).
* `source` should be used instead. And similarly for `stored` scripts, type
* `id` must be used instead. `file` scripts were removed as part of the
* breaking changes in [elasticsearch v6.0](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/breaking_60_scripting_changes.html#_file_scripts_removed)
*
* @param {string=} type One of `inline`, `stored`, `file`, `source`, `id`.
* @param {string=} source Source of the script.
* This needs to be specified if optional argument `type` is passed.
*/
Expand All @@ -7944,17 +7950,45 @@ declare namespace esb {
/**
* Sets the type of script to be `inline` and specifies the source of the script.
*
* Note: This type was deprecated in elasticsearch v5.0. Use `source`
* instead if you are using elasticsearch `>= 5.0`.
*
* @param {string} scriptCode
*/
inline(scriptCode: string): this;

/**
* Sets the type of script to be `source` and specifies the source of the script.
*
* Note: `source` is an alias for the `inline` type which was deprecated
* in elasticsearch v5.0. So this type is supported only in versions
* `>= 5.0`.
*
* @param {string} scriptCode
*/
source(scriptCode): this;

/**
* Specify the `stored` script by `id` which will be retrieved from cluster state.
*
* Note: This type was deprecated in elasticsearch v5.0. Use `id`
* instead if you are using elasticsearch `>= 5.0`.
*
* @param {string} scriptId The unique identifier for the stored script.
*/
stored(scriptId: string): this;

/**
* Specify the stored script to be used by it's `id` which will be retrieved
* from cluster state.
*
* Note: `id` is an alias for the `stored` type which was deprecated in
* elasticsearch v5.0. So this type is supported only in versions `>= 5.0`.
*
* @param {string} scriptId The unique identifier for the stored script.
*/
id(scriptId): this;

/**
* Specify the `stored` script by `id` which will be retrieved from cluster state.
*
Expand Down

0 comments on commit 0ff9f99

Please sign in to comment.