Skip to content

Commit

Permalink
feat: Add support for format option to sort (#187)
Browse files Browse the repository at this point in the history
format option was added to sort in Elasticsearch v7.13
  • Loading branch information
florian-lackner365 authored Oct 26, 2023
1 parent 122cd79 commit ce60314
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"ochan12 <[email protected]>",
"kennylindahl <[email protected]>",
"foxstarius <[email protected]>",
"sandeep952 <[email protected]>"
"sandeep952 <[email protected]>",
"florian-lackner365 <[email protected]>"
]
}
19 changes: 18 additions & 1 deletion src/core/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
99 changes: 95 additions & 4 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions test/core-test/sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit ce60314

Please sign in to comment.