diff --git a/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.js b/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.js new file mode 100644 index 00000000000000..7a72533005f478 --- /dev/null +++ b/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.js @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import _ from 'lodash'; +import { ES_SEARCH, SORT_ORDER } from '../constants'; + +function isEsDocumentSource(layerDescriptor) { + const sourceType = _.get(layerDescriptor, 'sourceDescriptor.type'); + return sourceType === ES_SEARCH; +} + +export function topHitsTimeToSort({ attributes }) { + if (!attributes.layerListJSON) { + return attributes; + } + + const layerList = JSON.parse(attributes.layerListJSON); + layerList.forEach((layerDescriptor) => { + if (isEsDocumentSource(layerDescriptor)) { + if (_.has(layerDescriptor, 'sourceDescriptor.topHitsTimeField')) { + layerDescriptor.sourceDescriptor.sortField = layerDescriptor.sourceDescriptor.topHitsTimeField; + layerDescriptor.sourceDescriptor.sortOrder = SORT_ORDER.DESC; + delete layerDescriptor.sourceDescriptor.topHitsTimeField; + } + } + }); + + return { + ...attributes, + layerListJSON: JSON.stringify(layerList), + }; +} diff --git a/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.test.js b/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.test.js new file mode 100644 index 00000000000000..09913e83cbb430 --- /dev/null +++ b/x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.test.js @@ -0,0 +1,60 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +/* eslint max-len: 0 */ + +import { topHitsTimeToSort } from './top_hits_time_to_sort'; + +describe('topHitsTimeToSort', () => { + + test('Should handle missing layerListJSON attribute', () => { + const attributes = { + title: 'my map', + }; + expect(topHitsTimeToSort({ attributes })).toEqual({ + title: 'my map', + }); + }); + + test('Should move topHitsTimeField to sortField for ES documents sources', () => { + const layerListJSON = JSON.stringify([ + { + sourceDescriptor: { + type: 'ES_SEARCH', + topHitsSplitField: 'gpsId', + topHitsTimeField: '@timestamp', + } + } + ]); + const attributes = { + title: 'my map', + layerListJSON + }; + expect(topHitsTimeToSort({ attributes })).toEqual({ + title: 'my map', + layerListJSON: '[{\"sourceDescriptor\":{\"type\":\"ES_SEARCH\",\"topHitsSplitField\":\"gpsId\",\"sortField\":\"@timestamp\",\"sortOrder\":\"desc\"}}]', + }); + }); + + test('Should handle ES documents sources without topHitsTimeField', () => { + const layerListJSON = JSON.stringify([ + { + sourceDescriptor: { + type: 'ES_SEARCH', + topHitsSplitField: 'gpsId', + } + } + ]); + const attributes = { + title: 'my map', + layerListJSON + }; + expect(topHitsTimeToSort({ attributes })).toEqual({ + title: 'my map', + layerListJSON, + }); + }); +}); diff --git a/x-pack/legacy/plugins/maps/migrations.js b/x-pack/legacy/plugins/maps/migrations.js index 6a97636e679c42..cdec1f25ce2471 100644 --- a/x-pack/legacy/plugins/maps/migrations.js +++ b/x-pack/legacy/plugins/maps/migrations.js @@ -6,6 +6,7 @@ import { extractReferences } from './common/migrations/references'; import { emsRasterTileToEmsVectorTile } from './common/migrations/ems_raster_tile_to_ems_vector_tile'; +import { topHitsTimeToSort } from './common/migrations/top_hits_time_to_sort'; export const migrations = { 'map': { @@ -21,6 +22,14 @@ export const migrations = { '7.4.0': (doc) => { const attributes = emsRasterTileToEmsVectorTile(doc); + return { + ...doc, + attributes, + }; + }, + '7.5.0': (doc) => { + const attributes = topHitsTimeToSort(doc); + return { ...doc, attributes,