Skip to content

Commit

Permalink
[Maps] fix date labels (#63909)
Browse files Browse the repository at this point in the history
* [Maps] fix date labels

* review feedback

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
nreese and elasticmachine authored Apr 20, 2020
1 parent 7e3de56 commit 635be64
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
12 changes: 11 additions & 1 deletion x-pack/plugins/maps/public/elasticsearch_geo_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function ensureGeometryType(type, expectedTypes) {
* @param {string} geoFieldType Geometry field type ["geo_point", "geo_shape"]
* @returns {number}
*/
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType, epochMillisFields) {
const features = [];
const tmpGeometriesAccumulator = [];

Expand All @@ -80,6 +80,16 @@ export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
geoShapeToGeometry(properties[geoFieldName], tmpGeometriesAccumulator);
}

// There is a bug in Elasticsearch API where epoch_millis are returned as a string instead of a number
// https://github.com/elastic/elasticsearch/issues/50622
// Convert these field values to integers.
for (let i = 0; i < epochMillisFields.length; i++) {
const fieldName = epochMillisFields[i];
if (typeof properties[fieldName] === 'string') {
properties[fieldName] = parseInt(properties[fieldName]);
}
}

// don't include geometry field value in properties
delete properties[geoFieldName];

Expand Down
29 changes: 23 additions & 6 deletions x-pack/plugins/maps/public/elasticsearch_geo_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(2);
expect(geojson.features[0]).toEqual({
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('hitsToGeoJson', () => {
_source: {},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(1);
});
Expand All @@ -111,7 +111,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.features.length).toBe(1);
const feature = geojson.features[0];
expect(feature.properties.myField).toBe(8);
Expand All @@ -128,7 +128,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point');
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', []);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(2);
expect(geojson.features[0]).toEqual({
Expand Down Expand Up @@ -159,6 +159,23 @@ describe('hitsToGeoJson', () => {
});
});

it('Should convert epoch_millis value from string to integer', () => {
const hits = [
{
_id: 'doc1',
_index: 'index1',
_source: {
[geoFieldName]: '20,100',
myDateField: '1587156257081',
},
},
];
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_point', ['myDateField']);
expect(geojson.type).toBe('FeatureCollection');
expect(geojson.features.length).toBe(1);
expect(geojson.features[0].properties.myDateField).toBe(1587156257081);
});

describe('dot in geoFieldName', () => {
const indexPatternMock = {
fields: {
Expand All @@ -184,7 +201,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point', []);
expect(geojson.features[0].geometry).toEqual({
coordinates: [100, 20],
type: 'Point',
Expand All @@ -199,7 +216,7 @@ describe('hitsToGeoJson', () => {
},
},
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point', []);
expect(geojson.features[0].geometry).toEqual({
coordinates: [100, 20],
type: 'Point',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,21 @@ export class ESSearchSource extends AbstractESSource {
});
return properties;
};
const epochMillisFields = searchFilters.fieldNames.filter(fieldName => {
const field = getField(indexPattern, fieldName);
return field.readFromDocValues && field.type === 'date';
});

let featureCollection;
try {
const geoField = await this._getGeoField();
featureCollection = hitsToGeoJson(hits, flattenHit, geoField.name, geoField.type);
featureCollection = hitsToGeoJson(
hits,
flattenHit,
geoField.name,
geoField.type,
epochMillisFields
);
} catch (error) {
throw new Error(
i18n.translate('xpack.maps.source.esSearch.convertToGeoJsonErrorMsg', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
}

getNumericalMbFeatureStateValue(value) {
if (typeof value === 'number') {
return value;
}

const valueAsFloat = parseFloat(value);
return isNaN(valueAsFloat) ? null : valueAsFloat;
}
Expand Down

0 comments on commit 635be64

Please sign in to comment.