[Maps] display documents when location field name contains a dot (#30774) (#30904)

This commit is contained in:
Nathan Reese 2019-02-12 19:48:04 -07:00 committed by GitHub
parent 91fa01a173
commit 17d4b72ec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 7 deletions

View file

@ -19,17 +19,17 @@ import _ from 'lodash';
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
const features = [];
hits.forEach(hit => {
const value = _.get(hit, `_source[${geoFieldName}]`);
const properties = flattenHit(hit);
let geometries;
if (geoFieldType === 'geo_point') {
geometries = geoPointToGeometry(value);
geometries = geoPointToGeometry(properties[geoFieldName]);
} else if (geoFieldType === 'geo_shape') {
geometries = geoShapeToGeometry(value);
geometries = geoShapeToGeometry(properties[geoFieldName]);
} else {
throw new Error(`Unsupported field type, expected: geo_shape or geo_point, you provided: ${geoFieldType}`);
}
const properties = flattenHit(hit);
// don't include geometry field value in properties
delete properties[geoFieldName];

View file

@ -12,6 +12,8 @@ import {
convertMapExtentToPolygon,
} from './elasticsearch_geo_utils';
import { IndexPatternsFlattenHitProvider } from 'ui/index_patterns/_flatten_hit';
const geoFieldName = 'location';
const mapExtent = {
maxLat: 39,
@ -24,9 +26,7 @@ const flattenHitMock = hit => {
const properties = {};
for (const fieldName in hit._source) {
if (hit._source.hasOwnProperty(fieldName)) {
if (fieldName !== geoFieldName) {
properties[fieldName] = hit._source[fieldName];
}
properties[fieldName] = hit._source[fieldName];
}
}
return properties;
@ -129,6 +129,69 @@ describe('hitsToGeoJson', () => {
type: 'Feature',
});
});
describe('dot in geoFieldName', () => {
const configMock = {
get: (key) => {
if (key === 'metaFields') {
return [];
}
throw new Error(`Unexpected config key: ${key}`);
},
watch: () => {}
};
const flattenHitWrapper = IndexPatternsFlattenHitProvider(configMock); // eslint-disable-line new-cap
const indexPatternMock = {
fields: {
byName: {
['my.location']: {
type: 'geo_point'
}
}
}
};
const indexPatternFlattenHit = flattenHitWrapper(indexPatternMock);
it('Should handle geoField being an object', () => {
const hits = [
{
_source: {
my: {
location: { lat: 20, lon: 100 },
}
}
}
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
expect(geojson.features[0]).toEqual({
geometry: {
coordinates: [100, 20],
type: 'Point',
},
properties: {},
type: 'Feature',
});
});
it('Should handle geoField containing dot in the name', () => {
const hits = [
{
_source: {
['my.location']: { lat: 20, lon: 100 },
}
}
];
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
expect(geojson.features[0]).toEqual({
geometry: {
coordinates: [100, 20],
type: 'Point',
},
properties: {},
type: 'Feature',
});
});
});
});
describe('geoPointToGeometry', () => {