Skip to content

Commit

Permalink
Include geo_shape filter
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Mar 14, 2023
1 parent 50a6879 commit be85917
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,46 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { getGeoShapeFilterField } from './geo_shape_filter';
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 filter = {
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: {
shape: {
coordinates: [{ lat: 1, lon: 1 }],
type: 'Polygon',
},
relation: 'intersects',
},
geoPointField: geoShapeQuery,
ignore_unmapped: true,
},
meta: {
disabled: false,
negate: false,
alias: null,
params: {
points: [{ lat: 1, lon: 1 }],
},
params: geoShapeQuery,
},
};
const result = getGeoShapeFilterField(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
* 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 ShapeFilter {
type: string;
coordinates: Position[][];
}

export interface PreIndexedShapeFilter {
index: string;
id: string;
path: string;
routing: string;
routing?: string;
}

export interface Polygon {
type: 'Polygon';
coordinates: Position[][];
}

export interface MultiPolygon {
type: 'MultiPolygon';
coordinates: Position[][][];
}

// Will support polygon & multi polygon at this moment
export type ShapeFilter = Polygon | MultiPolygon;

export type GeoShapeFilterMeta = FilterMeta & {
params: {
shape?: ShapeFilter;
indexed_shape?: PreIndexedShapeFilter;
relation?: string;
relation?: GeoShapeRelation;
};
};

Expand All @@ -32,8 +41,7 @@ export type GeoShapeFilter = Filter & {
geo_shape: any;
};

export const isGeoShapeFilter = (filter: any): filter is GeoShapeFilter =>
filter && filter.geo_shape;
export const isGeoShapeFilter = (filter: any): filter is GeoShapeFilter => filter?.geo_shape;

export const getGeoShapeFilterField = (filter: GeoShapeFilter) => {
return filter.geo_shape && Object.keys(filter.geo_shape).find((key) => key !== 'ignore_unmapped');
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/opensearch_query/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from './build_filters';
export * from './custom_filter';
export * from './exists_filter';
export * from './geo_bounding_box_filter';
export * from './geo_shape_filter';
export * from './geo_polygon_filter';
export * from './get_display_value';
export * from './get_filter_field';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/opensearch_query/filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ export enum FILTERS {
RANGE = 'range',
GEO_BOUNDING_BOX = 'geo_bounding_box',
GEO_POLYGON = 'geo_polygon',
GEO_SHAPE = 'geo_shape',
SPATIAL_FILTER = 'spatial_filter',
}

0 comments on commit be85917

Please sign in to comment.