diff --git a/src/location/DAL/queries.ts b/src/location/DAL/queries.ts index a3ae876..42a680a 100644 --- a/src/location/DAL/queries.ts +++ b/src/location/DAL/queries.ts @@ -73,32 +73,32 @@ export const geotextQuery = ( } } - if (!name && subPlaceTypes?.length) { - (esQuery.query?.function_score?.query?.bool?.must as QueryDslQueryContainer[]).push( - ...[ - { - terms: { - [SUB_PLACETYPE_FIELD]: subPlaceTypes, - }, - }, - { - term: { - text_language: textLanguage, - }, - }, - ] - ); - } else { - (esQuery.query?.function_score?.query?.bool?.must as QueryDslQueryContainer[]).push({ + const interpretations: QueryDslQueryContainer[] = [ + { match: { [TEXT_FIELD]: { query, fuzziness: disableFuzziness ? undefined : 'AUTO:3,4', }, }, + }, + ]; + + if (subPlaceTypes?.length) { + interpretations.push({ + bool: { + must: [{ terms: { [SUB_PLACETYPE_FIELD]: subPlaceTypes } }, { term: { text_language: textLanguage } }], + }, }); } + (esQuery.query?.function_score?.query?.bool?.must as QueryDslQueryContainer[]).push({ + bool: { + should: interpretations, + minimum_should_match: 1, + }, + }); + source?.length && (esQuery.query?.function_score?.query?.bool?.filter as QueryDslQueryContainer[]).push({ terms: { diff --git a/tests/integration/location/location.spec.ts b/tests/integration/location/location.spec.ts index bde71c0..9fe8856 100644 --- a/tests/integration/location/location.spec.ts +++ b/tests/integration/location/location.spec.ts @@ -27,7 +27,7 @@ import { LA_ROAD, } from '../../mockObjects/locations'; import { LocationRequestSender } from './helpers/requestSender'; -import { expectedResponse, hierarchiesWithAnyWieght } from './utils'; +import { expectedResponse, expectedResponseUnordered, hierarchiesWithAnyWieght } from './utils'; import { getBaseRegisterOptions } from './helpers'; let config: ConfigType; @@ -75,7 +75,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( { ...requestParams, }, @@ -90,7 +90,7 @@ describe('/search/location', function () { ...NY_JFK_AIRPORT.properties, names: { ...NY_JFK_AIRPORT.properties.names, - display: NY_JFK_AIRPORT_DISPLAY_NAMES[0], + display: NY_JFK_AIRPORT_DISPLAY_NAMES[1], }, }, }, @@ -127,7 +127,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( requestParams, { place_types: ['transportation'], @@ -140,7 +140,7 @@ describe('/search/location', function () { ...NY_JFK_AIRPORT.properties, names: { ...NY_JFK_AIRPORT.properties.names, - display: NY_JFK_AIRPORT_DISPLAY_NAMES[0], + display: NY_JFK_AIRPORT_DISPLAY_NAMES[1], }, }, }, @@ -176,7 +176,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( { ...requestParams, }, @@ -191,7 +191,7 @@ describe('/search/location', function () { ...NY_JFK_AIRPORT.properties, names: { ...NY_JFK_AIRPORT.properties.names, - display: NY_JFK_AIRPORT_DISPLAY_NAMES[0], + display: NY_JFK_AIRPORT_DISPLAY_NAMES[1], }, }, }, @@ -221,7 +221,7 @@ describe('/search/location', function () { ...NY_JFK_AIRPORT.properties, names: { ...NY_JFK_AIRPORT.properties.names, - display: NY_JFK_AIRPORT_DISPLAY_NAMES[0], + display: NY_JFK_AIRPORT_DISPLAY_NAMES[1], }, }, }, @@ -240,11 +240,13 @@ describe('/search/location', function () { ...NY_JFK_AIRPORT.properties, names: { ...NY_JFK_AIRPORT.properties.names, - display: NY_JFK_AIRPORT_DISPLAY_NAMES[0], + display: NY_JFK_AIRPORT_DISPLAY_NAMES[1], }, }, }, NY_POLICE_AIRPORT, + OSM_LA_PORT, + GOOGLE_LA_PORT, ], }, ])('it should test airports response with hierrarchy in %s', async ({ query, hierarchies, returnedFeatures }) => { @@ -265,7 +267,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( { ...requestParams, }, @@ -319,7 +321,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( requestParams, { name: query, @@ -402,7 +404,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( requestParams, { place_types: ['transportation'], @@ -433,7 +435,7 @@ describe('/search/location', function () { // expect(response).toSatisfyApiSpec(); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( requestParams, { place_types: ['education'], @@ -512,7 +514,7 @@ describe('/search/location', function () { expect(response.status).toBe(httpStatusCodes.OK); expect(response.body).toEqual>( - expectedResponse( + expectedResponseUnordered( requestParams, { name: 'los angeles', diff --git a/tests/integration/location/utils.ts b/tests/integration/location/utils.ts index 6f7068b..b82edbb 100644 --- a/tests/integration/location/utils.ts +++ b/tests/integration/location/utils.ts @@ -44,6 +44,19 @@ export const expectedResponse = ( bbox: expect.any(Array) as BBox, }); +export const expectedResponseUnordered = ( + requestParams: GetGeotextSearchParams, + responseParams: Partial['geocoding']['response']>, + arr: MockLocationQueryFeature[], + expect: jest.Expect +): GenericGeocodingResponse => { + const response = expectedResponse(requestParams, responseParams, arr, expect); + return { + ...response, + features: expect.arrayContaining(response.features) as GenericGeocodingResponse['features'], + }; +}; + export const hierarchiesWithAnyWieght = ( hierarchies: GenericGeocodingResponse['geocoding']['response']['hierarchies'], expect: jest.Expect