From 431c4510135e9b42f76fcbe0108e41aa83434544 Mon Sep 17 00:00:00 2001 From: Benjamin Piouffle Date: Tue, 31 Dec 2024 13:32:54 +0100 Subject: [PATCH] enhancement(search): only highlight searched fields (#10596) --- server/lib/elastic-search/search.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/lib/elastic-search/search.ts b/server/lib/elastic-search/search.ts index 338e0860756..dc517085254 100644 --- a/server/lib/elastic-search/search.ts +++ b/server/lib/elastic-search/search.ts @@ -101,11 +101,13 @@ const buildQuery = ( host: Collective, ): { query: QueryDslQueryContainer; - fields: Set; + /** All fields for which the search term was used. Does not include account constraints */ + searchedFields: Set; + /** All indexes that were fetched */ indexes: Set; } => { const accountConditions = getAccountFilterConditions(account, host); - const fetchedFields = new Set(); + const searchedFields = new Set(); const fetchedIndexes = new Set(); const adminOfAccountIds = !remoteUser ? [] : remoteUser.getAdministratedCollectiveIds(); const isRoot = remoteUser && remoteUser.isRoot(); @@ -131,7 +133,7 @@ const buildQuery = ( const publicFields = searchableFields.filter(field => !permissions['fields']?.[field]); // Register fetched fields and indexes for later reuse in the aggregation - allFields.forEach(field => fetchedFields.add(field)); + searchableFields.forEach(field => searchedFields.add(field)); fetchedIndexes.add(index); // Build the query for this index @@ -178,7 +180,7 @@ const buildQuery = ( /* eslint-enable camelcase */ }; - return { query, fields: fetchedFields, indexes: fetchedIndexes }; + return { query, searchedFields, indexes: fetchedIndexes }; }; export const elasticSearchGlobalSearch = async ( @@ -199,7 +201,7 @@ export const elasticSearchGlobalSearch = async ( } = {}, ) => { const client = getElasticSearchClient({ throwIfUnavailable: true }); - const { query, fields, indexes } = buildQuery(searchTerm, requestedIndexes, user, account, host); + const { query, searchedFields, indexes } = buildQuery(searchTerm, requestedIndexes, user, account, host); // Due to permissions, we may end up searching on no index at all (e.g. trying to search for comments while unauthenticated) if (indexes.size === 0) { @@ -235,7 +237,7 @@ export const elasticSearchGlobalSearch = async ( post_tags: [''], fragment_size: 40, number_of_fragments: 1, - fields: Array.from(fields).reduce((acc, field) => { + fields: Array.from(searchedFields).reduce((acc, field) => { acc[field] = {}; return acc; }, {}),