From 115ab1d17cee59b44303ed725b1424292237c771 Mon Sep 17 00:00:00 2001 From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com> Date: Tue, 18 May 2021 14:49:31 +0200 Subject: [PATCH] fix: update search of ES and max score to optimize --- .../structure-search-response.interface.ts | 1 + .../services/structures-search.service.ts | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/structures/interfaces/structure-search-response.interface.ts b/src/structures/interfaces/structure-search-response.interface.ts index 749734382..79c5e31df 100644 --- a/src/structures/interfaces/structure-search-response.interface.ts +++ b/src/structures/interfaces/structure-search-response.interface.ts @@ -4,6 +4,7 @@ export interface StructureSearchResult { hits: { total: number; hits: Array<{ + _score: number; _source: StructureSearchBody; }>; }; diff --git a/src/structures/services/structures-search.service.ts b/src/structures/services/structures-search.service.ts index f75cb5a69..210fc8f91 100644 --- a/src/structures/services/structures-search.service.ts +++ b/src/structures/services/structures-search.service.ts @@ -4,6 +4,7 @@ import { structureDto } from '../dto/structure.dto'; import { StructureDocument } from '../schemas/structure.schema'; import { StructureSearchBody } from '../interfaces/structure-search-body.interface'; import { StructureSearchResult } from '../interfaces/structure-search-response.interface'; +import { elementAt } from 'rxjs/operators'; @Injectable() export class StructuresSearchService { @@ -65,14 +66,22 @@ export class StructuresSearchService { query_string: { analyze_wildcard: 'true', query: searchString, - fields: ['structureName^5', 'structureType^5', 'address.street', 'address.commune^5', 'description'], + fields: ['structureName^5', 'structureType^5', 'address.commune^10', 'description'], fuzziness: 'AUTO', }, }, }, }); - const hits = body.hits.hits; - return hits.map((item) => item._source); + const maxScore = Math.max.apply( + Math, + body.hits.hits.map(function (hit) { + return hit._score; + }) + ); + const sortedHits = body.hits.hits.filter(function (elem) { + return elem._score >= maxScore / 1.5; + }); + return sortedHits.map((item) => item._source); } public async update(structure: structureDto, id: string): Promise<any> { -- GitLab