diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index cce9d59b0e76e601fc080358c1ba1bfe03e872a5..dd80b6bc074af2e84371fbc1cf363d861b79dde2 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 e1cb232eed112e76dc63e370017564cef63471ba..d829616add2fa1e7e2fd30bad06aabd8d6ecaad2 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