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..bd6f53beef23ba2148b6325e9ac713ecb5e75af1 100644 --- a/src/structures/services/structures-search.service.ts +++ b/src/structures/services/structures-search.service.ts @@ -27,7 +27,7 @@ export class StructuresSearchService { } public async createStructureIndex(): Promise<any> { - return await this.elasticsearchService.indices.create({ + return this.elasticsearchService.indices.create({ index: this.index, }); } @@ -65,14 +65,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> {