From c4811fb22dfab9149d94e4b87dcb79570a895f26 Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Fri, 15 Jan 2021 18:32:49 +0100 Subject: [PATCH] feat: add address search for structure registration --- src/structures/structures.controller.ts | 5 +++ src/structures/structures.service.ts | 58 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index cce9d59b0..dd80b6bc0 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -63,6 +63,11 @@ export class StructuresController { return data.reduce((a, b) => [...a, ...b]); } + @Post('address') + public async searchAddress(@Body() data: { searchQuery: string }) { + return await this.structureService.searchAddress(data); + } + @Get(':id') public async find(@Param('id') id: number) { return this.structureService.findOne(id); diff --git a/src/structures/structures.service.ts b/src/structures/structures.service.ts index e1cb232ee..d829616ad 100644 --- a/src/structures/structures.service.ts +++ b/src/structures/structures.service.ts @@ -125,6 +125,64 @@ export class StructuresService { return false; } + /** + * Search structure address based on data search WS + */ + public async searchAddress(data: { searchQuery: string }): Promise<AxiosResponse<any>> { + const req = 'https://data.grandlyon.com/api/elasticsearch/_search'; + const queryString = data.searchQuery.replace(/\s/g, ' AND '); + const params = { + from: 0, + size: 30, + _source: ['data-fr'], + query: { + bool: { + filter: { + term: { + 'metadata-fr.geonet:info.uuid.keyword': '4cb035de-6ac3-4763-94d8-4c19b1d19607', + }, + }, + must: [ + { + query_string: { + query: queryString, + default_field: '*', + analyzer: 'my_search_analyzer', + fuzziness: 'AUTO', + minimum_should_match: '90%', + }, + }, + ], + }, + }, + sort: [ + { + 'data-fr.properties.commune_str.sort': { + order: 'asc', + unmapped_type: 'string', + }, + }, + ], + }; + return new Promise((resolve, reject) => { + this.httpService + .request({ + url: req, + method: 'GET', + headers: { 'Content-Type': 'application/json' }, + data: params, + }) + .subscribe( + (reply) => { + return resolve(reply.data); + }, + (err) => { + Logger.error(`Request error: ${err.config.url}`, 'StructureService - search'); + } + ); + }); + } + /** * Count every value occurence of a given key * @param key structure key -- GitLab