-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Move sort out of top hits configuration for ES documents source (
#47361) (#47732) * [Maps] Move sort out of top hits configuration for ES documents source * add migration script to convert topHitsTimeField to sortField * update i18n translations * add jest test for es docs source UpdateSourceEditor component * remove time configuration from top hits docs * update migrations integration expect statement * review feedback * reverse hits list so top documents by sort are drawn on top * update functional test expect to account for reversing hits order * update another functional test expect clause for reversing hits
- Loading branch information
Showing
13 changed files
with
566 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}; | ||
} |
60 changes: 60 additions & 0 deletions
60
x-pack/legacy/plugins/maps/common/migrations/top_hits_time_to_sort.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.