-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] [Geospatial] Add new geo shape filter field to support…
… geospatial search query (#3681) * Add geo shape filter field to supported filter formats (#3605) Add new filter query, 'geo_shape' to search geospatial field types . This new filter query will replace geo_polygon query later. With this fiter, dashboard can perform spatial relationssips with shape or predefined shape from an index against an index. Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com> (cherry picked from commit 71eb3f2) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Josh Romero <rmerqg@amazon.com> Co-authored-by: Anan Zhuang <ananzh@amazon.com>
- Loading branch information
1 parent
36f64cd
commit 9ef6622
Showing
6 changed files
with
127 additions
and
0 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
67 changes: 67 additions & 0 deletions
67
src/plugins/data/common/opensearch_query/filters/geo_shape_filter.test.ts
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,67 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { GeoShapeFilter, getGeoShapeFilterField, Polygon, ShapeFilter } from './geo_shape_filter'; | ||
import { GeoShapeRelation } from '@opensearch-project/opensearch/api/types'; | ||
|
||
describe('geo shape filter', function () { | ||
describe('getGeoShapeFilterField', function () { | ||
it('should return the name of the field a geo_shape query is targeting', () => { | ||
const polygon: Polygon = { | ||
coordinates: [ | ||
[ | ||
[74.006, 40.7128], | ||
[71.0589, 42.3601], | ||
[73.7562, 42.6526], | ||
[74.006, 40.7128], | ||
], | ||
[ | ||
[72.6734, 41.7658], | ||
[72.6506, 41.5623], | ||
[73.0515, 41.5582], | ||
[72.6734, 41.7658], | ||
], | ||
], | ||
type: 'Polygon', | ||
}; | ||
const geoShapeQuery: { | ||
shape: ShapeFilter; | ||
relation: GeoShapeRelation; | ||
} = { | ||
shape: polygon, | ||
relation: 'intersects', | ||
}; | ||
const filter: GeoShapeFilter = { | ||
geo_shape: { | ||
geoPointField: geoShapeQuery, | ||
ignore_unmapped: true, | ||
}, | ||
meta: { | ||
disabled: false, | ||
negate: false, | ||
alias: null, | ||
params: geoShapeQuery, | ||
}, | ||
}; | ||
const result = getGeoShapeFilterField(filter); | ||
expect(result).toBe('geoPointField'); | ||
}); | ||
it('should return undefined if filter.geo_shape is undefined', () => { | ||
const filter: GeoShapeFilter = { | ||
geo_shape: undefined, | ||
meta: { | ||
disabled: false, | ||
negate: false, | ||
alias: null, | ||
params: { | ||
shape: undefined, | ||
}, | ||
}, | ||
}; | ||
const result = getGeoShapeFilterField(filter); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
src/plugins/data/common/opensearch_query/filters/geo_shape_filter.ts
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,53 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { GeoShapeRelation } from '@opensearch-project/opensearch/api/types'; | ||
import { Filter, FilterMeta } from './meta_filter'; | ||
|
||
export type Position = number[]; | ||
|
||
export interface PreIndexedShapeFilter { | ||
index: string; | ||
id: string; | ||
path: string; | ||
routing?: string; | ||
} | ||
|
||
export interface Polygon { | ||
type: 'Polygon'; | ||
coordinates: Position[][]; | ||
} | ||
|
||
export interface MultiPolygon { | ||
type: 'MultiPolygon'; | ||
coordinates: Position[][][]; | ||
} | ||
|
||
// TODO: support other geometries too. | ||
export type ShapeFilter = Polygon | MultiPolygon; | ||
|
||
export type GeoShapeFilterMeta = FilterMeta & { | ||
params: { | ||
shape?: ShapeFilter; | ||
indexed_shape?: PreIndexedShapeFilter; | ||
relation?: GeoShapeRelation; | ||
}; | ||
}; | ||
|
||
export type GeoShapeFilter = Filter & { | ||
meta: GeoShapeFilterMeta; | ||
geo_shape: any; | ||
}; | ||
|
||
export const isGeoShapeFilter = (filter: any): filter is GeoShapeFilter => filter?.geo_shape; | ||
|
||
export const getGeoShapeFilterField = (filter: GeoShapeFilter): string | undefined => { | ||
if (filter?.geo_shape === undefined) { | ||
return undefined; | ||
} | ||
return ( | ||
filter?.geo_shape && Object.keys(filter.geo_shape).find((key) => key !== 'ignore_unmapped') | ||
); | ||
}; |
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
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