From ce60314f100900669157b5a77d110c43e6b92e7d Mon Sep 17 00:00:00 2001 From: florian-lackner365 <115142134+florian-lackner365@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:26:39 +0200 Subject: [PATCH] feat: Add support for format option to sort (#187) format option was added to sort in Elasticsearch v7.13 --- package.json | 3 +- src/core/sort.js | 19 ++++++- src/index.d.ts | 99 +++++++++++++++++++++++++++++++++++-- test/core-test/sort.test.js | 14 ++++++ 4 files changed, 129 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 82bb32ef..c990dc16 100644 --- a/package.json +++ b/package.json @@ -105,6 +105,7 @@ "ochan12 ", "kennylindahl ", "foxstarius ", - "sandeep952 " + "sandeep952 ", + "florian-lackner365 " ] } diff --git a/src/core/sort.js b/src/core/sort.js index 588fd8c2..5bb5a6a9 100644 --- a/src/core/sort.js +++ b/src/core/sort.js @@ -297,7 +297,11 @@ class Sort { } /** - * Sets the data type for field generated by script. + * Sets the format of the date when sorting a date field. + * + * [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-date-format.html#built-in-date-formats) + * + * Note: The format argument is [supported since version 7.13](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/release-notes-7.13.0.html) of ElasticSearch. * * @param {string} type * @returns {Sort} returns `this` so that calls can be chained @@ -307,6 +311,19 @@ class Sort { return this; } + /** + * Sets the format of the date when sorting a date field. + * + * [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-date-format.html#built-in-date-formats) + * + * @param {string} fmt + * @returns {Sort} returns `this` so that calls can be chained + */ + format(fmt) { + this._opts.format = fmt; + return this; + } + /** * Reverse the sort order. Valid during sort types: field, geo distance, and script. * diff --git a/src/index.d.ts b/src/index.d.ts index 98dd16a0..a5fe4306 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1342,17 +1342,16 @@ declare namespace esb { * @param {string|number|boolean} queryVal */ value(queryVal: string | number | boolean): this; - + /** * Allows ASCII case insensitive matching of the value with the indexed - * field values when set to true. + * field values when set to true. * * NOTE: Only available in Elasticsearch v7.10.0+ * - * @param enable + * @param enable */ caseInsensitive(enable: boolean): this; - } /** @@ -9115,6 +9114,98 @@ declare namespace esb { */ type(type: string): this; + /** + * Sets the format of the date when sorting a date field. + * + * [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-date-format.html#built-in-date-formats) + * + * Note: The format argument is [supported since version 7.13](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/release-notes-7.13.0.html) of ElasticSearch. + * + * @param {string} fmt + */ + format( + fmt: + | 'epoch_millis' + | 'epoch_second' + | 'date_optional_time' + | 'strict_date_optional_time' + | 'strict_date_optional_time_nanos' + | 'basic_date' + | 'basic_date_time' + | 'basic_date_time_no_millis' + | 'basic_ordinal_date' + | 'basic_ordinal_date_time' + | 'basic_ordinal_date_time_no_millis' + | 'basic_time' + | 'basic_time_no_millis' + | 'basic_t_time' + | 'basic_t_time_no_millis' + | 'basic_week_date' + | 'strict_basic_week_date' + | 'basic_week_date_time' + | 'strict_basic_week_date_time' + | 'basic_week_date_time_no_millis' + | 'strict_basic_week_date_time_no_millis' + | 'date' + | 'strict_date' + | 'date_hour' + | 'strict_date_hour' + | 'date_hour_minute' + | 'strict_date_hour_minute' + | 'date_hour_minute_second' + | 'strict_date_hour_minute_second' + | 'date_hour_minute_second_fraction' + | 'strict_date_hour_minute_second_fraction' + | 'date_hour_minute_second_millis' + | 'strict_date_hour_minute_second_millis' + | 'date_time' + | 'strict_date_time' + | 'date_time_no_millis' + | 'strict_date_time_no_millis' + | 'hour' + | 'strict_hour' + | 'hour_minute' + | 'strict_hour_minute' + | 'hour_minute_second' + | 'strict_hour_minute_second' + | 'hour_minute_second_fraction' + | 'strict_hour_minute_second_fraction' + | 'hour_minute_second_millis' + | 'strict_hour_minute_second_millis' + | 'ordinal_date' + | 'strict_ordinal_date' + | 'ordinal_date_time' + | 'strict_ordinal_date_time' + | 'ordinal_date_time_no_millis' + | 'strict_ordinal_date_time_no_millis' + | 'time' + | 'strict_time' + | 'time_no_millis' + | 'strict_time_no_millis' + | 't_time' + | 'strict_t_time' + | 't_time_no_millis' + | 'strict_t_time_no_millis' + | 'week_date' + | 'strict_week_date' + | 'week_date_time' + | 'strict_week_date_time' + | 'week_date_time_no_millis' + | 'strict_week_date_time_no_millis' + | 'weekyear' + | 'strict_weekyear' + | 'weekyear_week' + | 'strict_weekyear_week' + | 'weekyear_week_day' + | 'strict_weekyear_week_day' + | 'year' + | 'strict_year' + | 'year_month' + | 'strict_year_month' + | 'year_month_day' + | 'strict_year_month_day' + ): this; + /** * Reverse the sort order. Valid during sort types: field, geo distance, and script. * diff --git a/test/core-test/sort.test.js b/test/core-test/sort.test.js index f127ed7e..6e93ffd9 100644 --- a/test/core-test/sort.test.js +++ b/test/core-test/sort.test.js @@ -115,3 +115,17 @@ test('_script sort', t => { }; t.deepEqual(value, expected); }); + +test('_format sort', t => { + const value = new Sort('date_field') + .order('asc') + .format('epoch_millis') + .toJSON(); + const expected = { + date_field: { + order: 'asc', + format: 'epoch_millis' + } + }; + t.deepEqual(value, expected); +});