Skip to content

Commit

Permalink
[Maps] spatially filter by all geo fields (elastic#100735)
Browse files Browse the repository at this point in the history
* [Maps] spatial filter by all geo fields

* replace geoFields with geoFieldNames

* update mapSpatialFilter to be able to reconize multi field filters

* add check for geoFieldNames

* i18n fixes and fix GeometryFilterForm jest test

* tslint

* tslint

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
nreese and kibanamachine committed Jun 2, 2021
1 parent 4202cb4 commit baa4c92
Show file tree
Hide file tree
Showing 32 changed files with 942 additions and 1,091 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,61 @@ describe('mapSpatialFilter()', () => {
expect(result).toHaveProperty('type', FILTERS.SPATIAL_FILTER);
});

test('should return the key for matching multi field filter', async () => {
const filter = {
meta: {
alias: 'my spatial filter',
isMultiIndex: true,
type: FILTERS.SPATIAL_FILTER,
} as FilterMeta,
query: {
bool: {
should: [
{
bool: {
must: [
{
exists: {
field: 'geo.coordinates',
},
},
{
geo_distance: {
distance: '1000km',
'geo.coordinates': [120, 30],
},
},
],
},
},
{
bool: {
must: [
{
exists: {
field: 'location',
},
},
{
geo_distance: {
distance: '1000km',
location: [120, 30],
},
},
],
},
},
],
},
},
} as Filter;
const result = mapSpatialFilter(filter);

expect(result).toHaveProperty('key', 'query');
expect(result).toHaveProperty('value', '');
expect(result).toHaveProperty('type', FILTERS.SPATIAL_FILTER);
});

test('should return undefined for none matching', async (done) => {
const filter = {
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ export const mapSpatialFilter = (filter: Filter) => {
value: '',
};
}

if (
filter.meta &&
filter.meta.type === FILTERS.SPATIAL_FILTER &&
filter.meta.isMultiIndex &&
filter.query?.bool?.should
) {
return {
key: 'query',
type: filter.meta.type,
value: '',
};
}
throw filter;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ReactNode } from 'react';
import { GeoJsonProperties } from 'geojson';
import { Geometry } from 'geojson';
import { Query } from '../../../../../src/plugins/data/common';
import { DRAW_TYPE, ES_GEO_FIELD_TYPE, ES_SPATIAL_RELATIONS } from '../constants';
import { DRAW_TYPE, ES_SPATIAL_RELATIONS } from '../constants';

export type MapExtent = {
minLon: number;
Expand Down Expand Up @@ -70,9 +70,6 @@ export type DrawState = {
actionId: string;
drawType: DRAW_TYPE;
filterLabel?: string; // point radius filter alias
geoFieldName?: string;
geoFieldType?: ES_GEO_FIELD_TYPE;
geometryLabel?: string;
indexPatternId?: string;
relation?: ES_SPATIAL_RELATIONS;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
hitsToGeoJson,
geoPointToGeometry,
geoShapeToGeometry,
createExtentFilter,
roundCoordinates,
extractFeaturesFromFilters,
makeESBbox,
scaleBounds,
} from './elasticsearch_geo_utils';
Expand Down Expand Up @@ -388,94 +386,6 @@ describe('geoShapeToGeometry', () => {
});
});

describe('createExtentFilter', () => {
it('should return elasticsearch geo_bounding_box filter', () => {
const mapExtent = {
maxLat: 39,
maxLon: -83,
minLat: 35,
minLon: -89,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-89, 39],
bottom_right: [-83, 35],
},
});
});

it('should clamp longitudes to -180 to 180 and latitudes to -90 to 90', () => {
const mapExtent = {
maxLat: 120,
maxLon: 200,
minLat: -100,
minLon: -190,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-180, 89],
bottom_right: [180, -89],
},
});
});

it('should make left longitude greater than right longitude when area crosses 180 meridian east to west', () => {
const mapExtent = {
maxLat: 39,
maxLon: 200,
minLat: 35,
minLon: 100,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
const leftLon = filter.geo_bounding_box.location.top_left[0];
const rightLon = filter.geo_bounding_box.location.bottom_right[0];
expect(leftLon).toBeGreaterThan(rightLon);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [100, 39],
bottom_right: [-160, 35],
},
});
});

it('should make left longitude greater than right longitude when area crosses 180 meridian west to east', () => {
const mapExtent = {
maxLat: 39,
maxLon: -100,
minLat: 35,
minLon: -200,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
const leftLon = filter.geo_bounding_box.location.top_left[0];
const rightLon = filter.geo_bounding_box.location.bottom_right[0];
expect(leftLon).toBeGreaterThan(rightLon);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [160, 39],
bottom_right: [-100, 35],
},
});
});

it('should clamp longitudes to -180 to 180 when longitude wraps globe', () => {
const mapExtent = {
maxLat: 39,
maxLon: 209,
minLat: 35,
minLon: -191,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-180, 39],
bottom_right: [180, 35],
},
});
});
});

describe('roundCoordinates', () => {
it('should set coordinates precision', () => {
const coordinates = [
Expand All @@ -492,134 +402,6 @@ describe('roundCoordinates', () => {
});
});

describe('extractFeaturesFromFilters', () => {
it('should ignore non-spatial filers', () => {
const phraseFilter = {
meta: {
alias: null,
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'machine.os',
negate: false,
params: {
query: 'ios',
},
type: 'phrase',
},
query: {
match_phrase: {
'machine.os': 'ios',
},
},
};
expect(extractFeaturesFromFilters([phraseFilter])).toEqual([]);
});

it('should convert geo_distance filter to feature', () => {
const spatialFilter = {
geo_distance: {
distance: '1096km',
'geo.coordinates': [-89.87125, 53.49454],
},
meta: {
alias: 'geo.coordinates within 1096km of -89.87125,53.49454',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

const features = extractFeaturesFromFilters([spatialFilter]);
expect(features[0].geometry.coordinates[0][0]).toEqual([-89.87125, 63.35109118642093]);
expect(features[0].properties).toEqual({
filter: 'geo.coordinates within 1096km of -89.87125,53.49454',
});
});

it('should convert geo_shape filter to feature', () => {
const spatialFilter = {
geo_shape: {
'geo.coordinates': {
relation: 'INTERSECTS',
shape: {
coordinates: [
[
[-101.21639, 48.1413],
[-101.21639, 41.84905],
[-90.95149, 41.84905],
[-90.95149, 48.1413],
[-101.21639, 48.1413],
],
],
type: 'Polygon',
},
},
ignore_unmapped: true,
},
meta: {
alias: 'geo.coordinates in bounds',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

expect(extractFeaturesFromFilters([spatialFilter])).toEqual([
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[-101.21639, 48.1413],
[-101.21639, 41.84905],
[-90.95149, 41.84905],
[-90.95149, 48.1413],
[-101.21639, 48.1413],
],
],
},
properties: {
filter: 'geo.coordinates in bounds',
},
},
]);
});

it('should ignore geo_shape filter with pre-index shape', () => {
const spatialFilter = {
geo_shape: {
'geo.coordinates': {
indexed_shape: {
id: 's5gldXEBkTB2HMwpC8y0',
index: 'world_countries_v1',
path: 'coordinates',
},
relation: 'INTERSECTS',
},
ignore_unmapped: true,
},
meta: {
alias: 'geo.coordinates in multipolygon',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

expect(extractFeaturesFromFilters([spatialFilter])).toEqual([]);
});
});

describe('makeESBbox', () => {
it('Should invert Y-axis', () => {
const bbox = makeESBbox({
Expand Down
Loading

0 comments on commit baa4c92

Please sign in to comment.