diff --git a/src/structures/interfaces/structure-search-response.interface.ts b/src/structures/interfaces/structure-search-response.interface.ts index 7497343827756c4a16df4e15a861709604a00ce0..79c5e31df2bfc3265023440dd17fc69c7ffae8c6 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 f75cb5a69ab837ac26563c770649802e6c84342a..210fc8f9167e1080d2d5e4cc7c608b04ca818455 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> {