diff --git a/app/api/search.v2/buildQuery.ts b/app/api/search.v2/buildQuery.ts index dbbd0a02ac..3a5b2b936a 100644 --- a/app/api/search.v2/buildQuery.ts +++ b/app/api/search.v2/buildQuery.ts @@ -53,7 +53,9 @@ const fullTextSearch = ( type: 'fullText', score_mode: 'max', inner_hits: { - _source: false, + _source: { + excludes: ['fullText*'], + }, ...snippetsHighlight(query, [{ 'fullText_*': {} }]), }, query: { diff --git a/app/api/search.v2/searchResponse.ts b/app/api/search.v2/searchResponse.ts index 89e6a62370..7afcfb8df7 100644 --- a/app/api/search.v2/searchResponse.ts +++ b/app/api/search.v2/searchResponse.ts @@ -15,7 +15,7 @@ function extractFullTextSnippets(hit: ElasticHit) { const fullTextSnippets: { text: string; page: number; filename: string }[] = []; if (hit.inner_hits && hit.inner_hits.fullText.hits.hits[0]) { - const { highlight, _id } = hit.inner_hits.fullText.hits.hits[0]; + const { highlight, _source } = hit.inner_hits.fullText.hits.hits[0]; const regex = /\[{2}(\d+)]{2}/g; Object.values(highlight).forEach(snippets => { @@ -24,7 +24,7 @@ function extractFullTextSnippets(hit: ElasticHit) { fullTextSnippets.push({ text: snippet.replace(regex, ''), page: matches ? Number(matches[1]) : 0, - filename: _id.split('_')?.[2] || '', + filename: _source.filename, }); }); }); diff --git a/app/api/search/elasticTypes.ts b/app/api/search/elasticTypes.ts index f0960b696e..aed328a343 100644 --- a/app/api/search/elasticTypes.ts +++ b/app/api/search/elasticTypes.ts @@ -26,7 +26,11 @@ export interface ElasticHit { fields?: any; highlight?: any; // eslint-disable-next-line camelcase - inner_hits?: { fullText: { hits: { hits: [{ highlight: {}; [k: string]: any }] } } }; + inner_hits?: { + fullText: { + hits: { hits: [{ _source: { [k: string]: any }; highlight: {}; [k: string]: any }] }; + }; + }; // eslint-disable-next-line camelcase matched_queries?: string[]; sort?: string[]; diff --git a/app/api/search/entitiesIndex.js b/app/api/search/entitiesIndex.js index 92a7ec3af4..3a76cb0642 100644 --- a/app/api/search/entitiesIndex.js +++ b/app/api/search/entitiesIndex.js @@ -54,6 +54,7 @@ function setFullTextSettings(defaultDocument, id, body, doc) { } const fullTextObject = { [`fullText_${language}`]: fullText, + filename: defaultDocument.filename, fullText: { name: 'fullText', parent: id }, }; body.push(fullTextObject); @@ -85,7 +86,7 @@ const bulkIndex = async (docs, _action = 'index') => { if (defaultDocument.fullText) { body.push({ - [_action]: { _id: `${id}_fullText_${defaultDocument.filename}`, routing: id }, + [_action]: { _id: `${id}_fullText`, routing: id }, }); setFullTextSettings(defaultDocument, id, body, doc); }