From eb34bb7e2afcf968238ace9a6012d71401baa9b7 Mon Sep 17 00:00:00 2001 From: Lionel Date: Wed, 18 Dec 2019 16:04:43 +0100 Subject: [PATCH] fix(api): remove current item from relatedItems (#2000) fix #1874 --- .../__snapshots__/items.test.js.snap | 17 ------- .../server/routes/items/getRelatedItems.js | 51 ++++++++----------- 2 files changed, 20 insertions(+), 48 deletions(-) diff --git a/packages/code-du-travail-api/src/server/routes/__tests__/__snapshots__/items.test.js.snap b/packages/code-du-travail-api/src/server/routes/__tests__/__snapshots__/items.test.js.snap index 5e1b926b42..7963163e4c 100644 --- a/packages/code-du-travail-api/src/server/routes/__tests__/__snapshots__/items.test.js.snap +++ b/packages/code-du-travail-api/src/server/routes/__tests__/__snapshots__/items.test.js.snap @@ -605,23 +605,6 @@ Object { }, "_type": "_doc", "relatedItems": Array [ - Object { - "breadcrumbs": Array [ - Object { - "slug": "8-depart-de-lentreprise", - "title": "Départ de l'entreprise", - }, - Object { - "slug": "81-demission", - "title": "Démission", - }, - ], - "description": "La démission permet au salarié, à son initiative, de rompre son contrat de travail, sous conditions.", - "slug": "demission-dun-salarie", - "source": "fiches_service_public", - "title": "Démission d'un salarié", - "url": "https://www.service-public.fr/particuliers/vosdroits/F2883", - }, Object { "breadcrumbs": Array [ Object { diff --git a/packages/code-du-travail-api/src/server/routes/items/getRelatedItems.js b/packages/code-du-travail-api/src/server/routes/items/getRelatedItems.js index 43f990d90d..a5e60fd1cb 100644 --- a/packages/code-du-travail-api/src/server/routes/items/getRelatedItems.js +++ b/packages/code-du-travail-api/src/server/routes/items/getRelatedItems.js @@ -18,46 +18,35 @@ async function getRelatedItems({ queryVector, settings, slug }) { SOURCES.LETTERS, SOURCES.CONTRIBUTIONS ]; - const { - body: { - responses: [esResponse, semResponse] + + const requestBodies = [ + { index }, + { + ...getRelatedItemsBody({ + settings, + sources + }) } - } = await elasticsearchClient.msearch({ - body: [ - { index }, - { - ...getRelatedItemsBody({ - settings, - sources - }) - }, + ]; + if (queryVector) { + requestBodies.push( { index }, // we +1 the size to remove the document source that should match perfectly for the given vector { ...getSemBody({ query_vector: queryVector, size: size + 1, sources }) } - ] - }); + ); + } + const { + body: { + responses: [esResponse = [], semResponse = []] + } + } = await elasticsearchClient.msearch({ body: requestBodies }); const { hits: { hits: semanticHits } = { hits: [] } } = semResponse; const { hits: { hits: fullTextHits } = { hits: [] } } = esResponse; - const filteredFullTextHits = fullTextHits.filter( - ({ _source }) => !_source.slug.includes(slug) - ); - - if (!queryVector) { - return filteredFullTextHits - .slice(0, MAX_RESULTS) - .map(({ _source }) => _source); - } - - const referenceId = settings[0].id; - return utils - .mergePipe( - semanticHits.filter(doc => doc._id !== referenceId), - filteredFullTextHits, - MAX_RESULTS - ) + .mergePipe(semanticHits, fullTextHits, MAX_RESULTS) + .filter(({ _source }) => !_source.slug.includes(slug)) .map(({ _source }) => _source); }