Skip to content

Commit

Permalink
persist filename within the fullText object on elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-vi committed Oct 16, 2024
1 parent 71eb12d commit 568e3a6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion app/api/search.v2/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ const fullTextSearch = (
type: 'fullText',
score_mode: 'max',
inner_hits: {
_source: false,
_source: {
excludes: ['fullText*'],
},
...snippetsHighlight(query, [{ 'fullText_*': {} }]),
},
query: {
Expand Down
4 changes: 2 additions & 2 deletions app/api/search.v2/searchResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function extractFullTextSnippets(hit: ElasticHit<EntitySchema>) {
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<string[]>(highlight).forEach(snippets => {
Expand All @@ -24,7 +24,7 @@ function extractFullTextSnippets(hit: ElasticHit<EntitySchema>) {
fullTextSnippets.push({
text: snippet.replace(regex, ''),
page: matches ? Number(matches[1]) : 0,
filename: _id.split('_')?.[2] || '',
filename: _source.filename,
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion app/api/search/elasticTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export interface ElasticHit<T> {
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[];
Expand Down
3 changes: 2 additions & 1 deletion app/api/search/entitiesIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 568e3a6

Please sign in to comment.