Skip to content

Commit

Permalink
[Maps] Move sort out of top hits configuration for ES documents source (
Browse files Browse the repository at this point in the history
#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
nreese authored Oct 9, 2019
1 parent cb63f5e commit b05c68b
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 90 deletions.
2 changes: 0 additions & 2 deletions docs/maps/maps-aggregations.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ To enable most recent entities, click "Show most recent documents by entity" and

. Set *Entity* to the field that identifies entities in your documents.
This field will be used in the terms aggregation to group your documents into entity buckets.
. Set *Time* to the date field that puts your documents in chronological order.
This field will be used to sort your documents in the top hits aggregation.
. Set *Documents per entity* to configure the maximum number of documents accumulated per entity.

[role="xpack"]
Expand Down
5 changes: 5 additions & 0 deletions x-pack/legacy/plugins/maps/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const LAYER_TYPE = {
HEATMAP: 'HEATMAP'
};

export const SORT_ORDER = {
ASC: 'asc',
DESC: 'desc',
};

export const EMS_TMS = 'EMS_TMS';
export const EMS_FILE = 'EMS_FILE';
export const ES_GEO_GRID = 'ES_GEO_GRID';
Expand Down
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),
};
}
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,
});
});
});
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/maps/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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,
Expand Down
Loading

0 comments on commit b05c68b

Please sign in to comment.