Skip to content

Commit

Permalink
[Discover] Fix field stats for epoch time format (#148288)
Browse files Browse the repository at this point in the history
Closes #148140

## Summary

This PR fixes time format so it can work also for epoch.

Before:
<img width="400" alt="Screenshot 2023-01-03 at 12 33 17"
src="https://user-images.githubusercontent.com/1415710/210350243-b3ae1d1a-4c76-46e7-930f-0ca9835dc121.png">

After:
<img width="400" alt="Screenshot 2023-01-03 at 12 32 49"
src="https://user-images.githubusercontent.com/1415710/210350274-af80f558-6a32-4da6-9cb9-857a8cb3de70.png">

Sample data for testing:
```
PUT test
PUT test/_mapping
{
    "properties": {
        "timestamp": {
            "type": "date",
            "format": "epoch_second"
        },
        "message":  {
          "type": "keyword"
        },
        "bytes": {
          "type": "long"
        }
    }
}
    
POST test/_doc/1
{
    "timestamp": 1669912088.9230318,
    "message": "Message 1",
    "bytes": 250
}
POST test/_doc/2
{
    "timestamp": 1669912088.9230319,
    "message": "Message 2",
    "bytes": 20
}
```
  • Loading branch information
jughosta committed Jan 5, 2023
1 parent 69d7791 commit 1b3769c
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 153 additions & 0 deletions src/plugins/unified_field_list/common/utils/field_stats_utils.test.ts
@@ -0,0 +1,153 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { buildSearchParams } from './field_stats_utils';

describe('fieldStatsUtils', function () {
describe('buildSearchParams()', () => {
it('should work correctly for aggregations and a data view time field', () => {
expect(
buildSearchParams({
dataViewPattern: 'kibana_sample_data_logs',
timeFieldName: 'timestamp',
fromDate: '2022-12-05T23:00:00.000Z',
toDate: '2023-01-05T09:33:05.359Z',
dslQuery: {
bool: {
must: [],
filter: [
{
match_phrase: {
'geo.src': 'US',
},
},
],
should: [],
must_not: [],
},
},
runtimeMappings: {
hour_of_day: {
type: 'long',
script: {
source: "emit(doc['timestamp'].value.getHour());",
},
},
},
aggs: {
sample: {
sampler: {
shard_size: 5000,
},
aggs: {
sample_count: {
value_count: {
field: 'extension.keyword',
},
},
top_values: {
terms: {
field: 'extension.keyword',
size: 10,
shard_size: 25,
},
},
},
},
},
})
).toMatchSnapshot();
});

it('should work correctly for aggregations without a data view time field', () => {
expect(
buildSearchParams({
dataViewPattern: 'kibana_sample*',
timeFieldName: '',
fromDate: '2022-12-05T23:00:00.000Z',
toDate: '2023-01-05T09:33:53.717Z',
dslQuery: {
bool: {
must: [],
filter: [
{
match_phrase: {
'geo.src': 'US',
},
},
],
should: [],
must_not: [],
},
},
runtimeMappings: {},
aggs: {
sample: {
sampler: {
shard_size: 5000,
},
aggs: {
sample_count: {
value_count: {
field: 'extension.keyword',
},
},
top_values: {
terms: {
field: 'extension.keyword',
size: 10,
shard_size: 25,
},
},
},
},
},
})
).toMatchSnapshot();
});

it('should work correctly for fetching field examples', () => {
expect(
buildSearchParams({
dataViewPattern: 'kibana_sample_data_logs',
timeFieldName: 'timestamp',
fromDate: '2022-12-05T23:00:00.000Z',
toDate: '2023-01-05T09:35:24.109Z',
dslQuery: {
bool: {
must: [],
filter: [
{
match_phrase: {
'geo.src': 'US',
},
},
],
should: [],
must_not: [],
},
},
runtimeMappings: {
hour_of_day: {
type: 'long',
script: {
source: "emit(doc['timestamp'].value.getHour());",
},
},
},
fields: [
{
field: '_id',
},
],
size: 100,
})
).toMatchSnapshot();
});
});
});
Expand Up @@ -55,6 +55,7 @@ export function buildSearchParams({
[timeFieldName]: {
gte: fromDate,
lte: toDate,
format: 'strict_date_optional_time',
},
},
},
Expand Down

0 comments on commit 1b3769c

Please sign in to comment.