Skip to content

Commit

Permalink
Add date_nanos support to Time field (elastic#151374)
Browse files Browse the repository at this point in the history
Fixes: elastic#145855

`Time field` in rule update/create forms shows only the fields with
`date` data type. This PR adds `date_nanos` type as well.

## To verify:

- Create an index that has fields with `date` and `date_nanos` types.
```
PUT test-index-date
{
  "mappings": {
    "properties": {
      "date": {
        "type": "date"
      },
      "timestamp": {
        "type": "date_nanos"
      }
    }
  }
}
```
- Add data with `date` and `date_nanos` data types
Note: You can use
https://www.site24x7.com/tools/time-stamp-converter.html address to
generate date_nanos timestamps with a date you desired.
```
PUT test-index-date/_bulk?refresh
{ "index" : { "_id" : "1" } }
{ "date": "2023-02-15T18:00:00.000Z", "timestamp": 1676480400000}
{ "index" : { "_id" : "2" } }
{ "date": "2023-02-15T18:00:00.000Z", "timestamp": 1676480400000 }
```
- Open a Rule create form and select `Elasticsearch query`
- Select `Query DSL`
- Click on `select an index`
- Type the above index name (test-index-date) in `Indices to query`
input
- Select `timestamp` from time field
- Click on `Test Query` button and expect to see `Query matched 2
documents in the last 5m.`

(cherry picked from commit 58204cf)
  • Loading branch information
ersin-erdal committed Feb 21, 2023
1 parent 272471d commit dc16d40
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down Expand Up @@ -160,6 +162,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down Expand Up @@ -210,6 +213,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down Expand Up @@ -272,6 +276,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down Expand Up @@ -320,6 +325,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'desc',
},
},
Expand Down Expand Up @@ -368,6 +374,7 @@ describe('buildSortedEventsQuery', () => {
sort: [
{
timefield: {
format: 'strict_date_optional_time||epoch_millis',
order: 'asc',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const buildSortedEventsQuery = ({
{
[sortField]: {
order: sortOrder ?? 'asc',
format: 'strict_date_optional_time||epoch_millis',
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ describe('fetchEsQuery', () => {
sort: [
{
'@timestamp': {
format: 'strict_date_optional_time||epoch_millis',
order: 'desc',
},
},
Expand Down Expand Up @@ -194,6 +195,7 @@ describe('fetchEsQuery', () => {
sort: [
{
'@timestamp': {
format: 'strict_date_optional_time||epoch_millis',
order: 'desc',
},
},
Expand Down Expand Up @@ -258,6 +260,7 @@ describe('fetchEsQuery', () => {
sort: [
{
'@timestamp': {
format: 'strict_date_optional_time||epoch_millis',
order: 'desc',
},
},
Expand Down Expand Up @@ -349,6 +352,7 @@ describe('fetchEsQuery', () => {
sort: [
{
'@timestamp': {
format: 'strict_date_optional_time||epoch_millis',
order: 'desc',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ export function updateSearchSource(

const searchSourceChild = searchSource.createChild();
searchSourceChild.setField('filter', filters as Filter[]);
searchSourceChild.setField('sort', [{ [timeFieldName]: SortDirection.desc }]);
searchSourceChild.setField('sort', [
{
[timeFieldName]: {
order: SortDirection.desc,
format: 'strict_date_optional_time||epoch_millis',
},
},
]);
searchSourceChild.setField(
'aggs',
buildAggregation({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ describe('get_time_options', () => {
test('if getTimeFieldOptions return only date type fields', () => {
const timeOnlyTypeFields = getTimeFieldOptions([
{ type: 'date', name: 'order_date' },
{ type: 'date_nanos', name: 'order_date_nanos' },
{ type: 'number', name: 'sum' },
]);
expect(timeOnlyTypeFields).toMatchObject([{ text: 'order_date', value: 'order_date' }]);
expect(timeOnlyTypeFields).toMatchObject([
{ text: 'order_date', value: 'order_date' },
{ text: 'order_date_nanos', value: 'order_date_nanos' },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getTimeFieldOptions = (
const options: TimeFieldOptions[] = [];

fields.forEach((field: { type: string; name: string }) => {
if (field.type === 'date') {
if (field.type === 'date' || field.type === 'date_nanos') {
options.push({
text: field.name,
value: field.name,
Expand Down

0 comments on commit dc16d40

Please sign in to comment.