From d6b6a0bc099674bbf1debfaa7d9c6d8407552b85 Mon Sep 17 00:00:00 2001 From: Julia Rechkunova Date: Tue, 3 Jan 2023 12:34:04 +0100 Subject: [PATCH 1/2] [Discover] Fix field stats for epoch time format --- src/plugins/unified_field_list/common/utils/field_stats_utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/unified_field_list/common/utils/field_stats_utils.ts b/src/plugins/unified_field_list/common/utils/field_stats_utils.ts index 89bf8a44139cfd..9fac4c04946c97 100644 --- a/src/plugins/unified_field_list/common/utils/field_stats_utils.ts +++ b/src/plugins/unified_field_list/common/utils/field_stats_utils.ts @@ -55,6 +55,7 @@ export function buildSearchParams({ [timeFieldName]: { gte: fromDate, lte: toDate, + format: 'strict_date_optional_time', }, }, }, From a5e42024d6752e03a4244a48486d892df0d98c99 Mon Sep 17 00:00:00 2001 From: Julia Rechkunova Date: Thu, 5 Jan 2023 10:38:29 +0100 Subject: [PATCH 2/2] [Discover] Add tests --- .../field_stats_utils.test.ts.snap | 184 ++++++++++++++++++ .../common/utils/field_stats_utils.test.ts | 153 +++++++++++++++ 2 files changed, 337 insertions(+) create mode 100644 src/plugins/unified_field_list/common/utils/__snapshots__/field_stats_utils.test.ts.snap create mode 100644 src/plugins/unified_field_list/common/utils/field_stats_utils.test.ts diff --git a/src/plugins/unified_field_list/common/utils/__snapshots__/field_stats_utils.test.ts.snap b/src/plugins/unified_field_list/common/utils/__snapshots__/field_stats_utils.test.ts.snap new file mode 100644 index 00000000000000..4cd6bdc44ae7fe --- /dev/null +++ b/src/plugins/unified_field_list/common/utils/__snapshots__/field_stats_utils.test.ts.snap @@ -0,0 +1,184 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`fieldStatsUtils buildSearchParams() should work correctly for aggregations and a data view time field 1`] = ` +Object { + "body": Object { + "_source": undefined, + "aggs": Object { + "sample": Object { + "aggs": Object { + "sample_count": Object { + "value_count": Object { + "field": "extension.keyword", + }, + }, + "top_values": Object { + "terms": Object { + "field": "extension.keyword", + "shard_size": 25, + "size": 10, + }, + }, + }, + "sampler": Object { + "shard_size": 5000, + }, + }, + }, + "fields": undefined, + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "range": Object { + "timestamp": Object { + "format": "strict_date_optional_time", + "gte": "2022-12-05T23:00:00.000Z", + "lte": "2023-01-05T09:33:05.359Z", + }, + }, + }, + Object { + "bool": Object { + "filter": Array [ + Object { + "match_phrase": Object { + "geo.src": "US", + }, + }, + ], + "must": Array [], + "must_not": Array [], + "should": Array [], + }, + }, + ], + }, + }, + "runtime_mappings": Object { + "hour_of_day": Object { + "script": Object { + "source": "emit(doc['timestamp'].value.getHour());", + }, + "type": "long", + }, + }, + }, + "index": "kibana_sample_data_logs", + "size": 0, + "track_total_hits": true, +} +`; + +exports[`fieldStatsUtils buildSearchParams() should work correctly for aggregations without a data view time field 1`] = ` +Object { + "body": Object { + "_source": undefined, + "aggs": Object { + "sample": Object { + "aggs": Object { + "sample_count": Object { + "value_count": Object { + "field": "extension.keyword", + }, + }, + "top_values": Object { + "terms": Object { + "field": "extension.keyword", + "shard_size": 25, + "size": 10, + }, + }, + }, + "sampler": Object { + "shard_size": 5000, + }, + }, + }, + "fields": undefined, + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "bool": Object { + "filter": Array [ + Object { + "match_phrase": Object { + "geo.src": "US", + }, + }, + ], + "must": Array [], + "must_not": Array [], + "should": Array [], + }, + }, + ], + }, + }, + "runtime_mappings": Object {}, + }, + "index": "kibana_sample*", + "size": 0, + "track_total_hits": true, +} +`; + +exports[`fieldStatsUtils buildSearchParams() should work correctly for fetching field examples 1`] = ` +Object { + "body": Object { + "_source": false, + "aggs": undefined, + "fields": Array [ + Object { + "field": "_id", + }, + ], + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "range": Object { + "timestamp": Object { + "format": "strict_date_optional_time", + "gte": "2022-12-05T23:00:00.000Z", + "lte": "2023-01-05T09:35:24.109Z", + }, + }, + }, + Object { + "bool": Object { + "filter": Array [ + Object { + "match_phrase": Object { + "geo.src": "US", + }, + }, + ], + "must": Array [], + "must_not": Array [], + "should": Array [], + }, + }, + Object { + "exists": Object { + "field": "_id", + }, + }, + ], + }, + }, + "runtime_mappings": Object { + "hour_of_day": Object { + "script": Object { + "source": "emit(doc['timestamp'].value.getHour());", + }, + "type": "long", + }, + }, + }, + "index": "kibana_sample_data_logs", + "size": 100, + "track_total_hits": true, +} +`; diff --git a/src/plugins/unified_field_list/common/utils/field_stats_utils.test.ts b/src/plugins/unified_field_list/common/utils/field_stats_utils.test.ts new file mode 100644 index 00000000000000..1135f028f1702b --- /dev/null +++ b/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(); + }); + }); +});