refactor: simplify geo queries

This commit is contained in:
Thea Schöbl
2022-05-13 08:26:23 +00:00
parent d3c509ccb4
commit 9ce8c58b98
5 changed files with 198 additions and 31 deletions

View File

@@ -32,7 +32,7 @@ import {
ESGeoDistanceFilter,
ESGeoDistanceSort,
ESTermFilter,
ScriptSort
ScriptSort,
} from '../../../src/storage/elasticsearch/types/elasticsearch';
import {configFile} from '../../../src/common';
import {buildBooleanFilter, buildFilter, buildQuery, buildSort} from '../../../src/storage/elasticsearch/query';
@@ -205,10 +205,37 @@ describe('Query', function () {
type: 'distance',
arguments: {
distance: 1000,
field: 'geo.point.coordinates',
field: 'geo',
position: [50.123, 8.123],
}
},
geoPoint: {
type: 'geo',
arguments: {
field: 'geo',
shape: {
type: 'envelope',
coordinates: [
[50.123, 8.123],
[50.123, 8.123],
]
}
}
},
geoShape: {
type: 'geo',
arguments: {
field: 'geo',
spatialRelation: 'contains',
shape: {
type: 'envelope',
coordinates: [
[50.123, 8.123],
[50.123, 8.123],
]
}
}
},
boolean: {
type: 'boolean',
arguments: {
@@ -447,6 +474,64 @@ describe('Query', function () {
expect(filter).to.be.eql(expectedFilter);
});
it('should build geo filter for shapes and points', function () {
const filter = buildFilter(searchFilters.geoPoint);
const expectedFilter = {
bool: {
minimum_should_match: 1,
should: [
{
geo_shape: {
'geo.polygon': {
relation: undefined,
shape: {
type: 'envelope',
coordinates: [
[50.123, 8.123],
[50.123, 8.123]
]
},
},
ignore_unmapped: true,
}
},
{
geo_bounding_box: {
'geo.point.coordinates': {
bottom_right: [50.123, 8.123],
top_left: [50.123, 8.123]
},
ignore_unmapped: true,
},
},
]
}
};
expect(filter).to.be.eql(expectedFilter);
});
it('should build geo filter for shapes only', function () {
const filter = buildFilter(searchFilters.geoShape);
const expectedFilter = {
geo_shape: {
'geo.polygon': {
relation: 'contains',
shape: {
type: 'envelope',
coordinates: [
[50.123, 8.123],
[50.123, 8.123]
]
},
},
ignore_unmapped: true,
}
};
expect(filter).to.be.eql(expectedFilter);
});
it('should build boolean filter', function () {
const filter = buildFilter(searchFilters.boolean);
const expectedFilter: ESBooleanFilter<any> = {
@@ -497,7 +582,7 @@ describe('Query', function () {
type: 'distance',
order: 'desc',
arguments: {
field: 'geo.point',
field: 'geo',
position: [8.123, 50.123]
},
},
@@ -523,7 +608,7 @@ describe('Query', function () {
mode: 'avg',
order: 'desc',
unit: 'm',
'geo.point': {
'geo.point.coordinates': {
lat: 50.123,
lon: 8.123
}