Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Geospatial] Add new geo shape filter field to support geospatial search query #3605

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Vis Builder] Add metric to metric, bucket to bucket aggregation persistence ([#3495](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3495))
- Use mirrors to download Node.js binaries to escape sporadic 404 errors ([#3619](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3619))
- [Multiple DataSource] Refactor dev tool console to use opensearch-js client to send requests ([#3544](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3544))
- [Data] Add geo shape filter field ([#3605](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3605))

### 🐛 Bug Fixes

Expand Down
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();
});
});
ananzh marked this conversation as resolved.
Show resolved Hide resolved
});
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we consider define a more specific interface or type for the geo_shape property?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geo_shape will have dynamic field name and "ignored_unmapped" as well. I couldn't find a way to define both in same structure. Do you know how to do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure about what should be geo_shape, but it is similar to meta, will this work?

export type GeoShapeFilter = Filter & {
  meta: GeoShapeFilterMeta;
  geo_shape: {
    [fieldName: string]: {
      shape?: ShapeFilter;
      indexed_shape?: PreIndexedShapeFilter;
      relation?: GeoShapeRelation;
      ignore_unmapped?: boolean;
    };
  };
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker. I will just approve it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here ignore_unmapped should be inside geo_shape , something like below

geo_shape: {
    [fieldName: string]: {
      shape?: ShapeFilter;
      indexed_shape?: PreIndexedShapeFilter;
      relation?: GeoShapeRelation;
    },
   ignore_unmapped?: boolean;
}

but typescript don't like this, since ignore_unmapped is also string and it can't differentiate whether is it part of fieldName or ignore_mapped.

};

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 (
ananzh marked this conversation as resolved.
Show resolved Hide resolved
filter?.geo_shape && Object.keys(filter.geo_shape).find((key) => key !== 'ignore_unmapped')
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { getPhraseFilterField, isPhraseFilter } from './phrase_filter';
import { getPhrasesFilterField, isPhrasesFilter } from './phrases_filter';
import { getRangeFilterField, isRangeFilter } from './range_filter';
import { getMissingFilterField, isMissingFilter } from './missing_filter';
import { getGeoShapeFilterField, isGeoShapeFilter } from './geo_shape_filter';

export const getFilterField = (filter: Filter) => {
if (isExistsFilter(filter)) {
Expand All @@ -59,6 +60,9 @@ export const getFilterField = (filter: Filter) => {
if (isMissingFilter(filter)) {
return getMissingFilterField(filter);
}
if (isGeoShapeFilter(filter)) {
return getGeoShapeFilterField(filter);
}

return;
};
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
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',
}