From 672da1cb3cfea963696cd55eaa153a9992e8f582 Mon Sep 17 00:00:00 2001 From: nl_0 Date: Fri, 4 Oct 2024 17:37:19 +0200 Subject: [PATCH] feat: truncate indexed content in search results --- .../containers/Search/AssistantContext.tsx | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/catalog/app/containers/Search/AssistantContext.tsx b/catalog/app/containers/Search/AssistantContext.tsx index a80de64d344..b3f1930ce02 100644 --- a/catalog/app/containers/Search/AssistantContext.tsx +++ b/catalog/app/containers/Search/AssistantContext.tsx @@ -169,6 +169,23 @@ function useSearchContextModel(): MaybeEitherSearchContext { ) } +const MAX_CONTENT_LENGTH = 10_000 + +function truncateIndexedContent(hit: FirstPageHits[number]) { + switch (hit.__typename) { + case 'SearchHitObject': + return (hit.indexedContent?.length ?? 0) > MAX_CONTENT_LENGTH + ? { + ...hit, + indexedContent: hit.indexedContent?.slice(0, MAX_CONTENT_LENGTH), + indexedContentTruncated: true, + } + : hit + default: + return hit + } +} + function useSearchContext() { const ctxO = useSearchContextModel() @@ -219,7 +236,11 @@ function useSearchContext() { 'page', { number: 1 }, ...ctx.firstPage.map((hit, index) => - XML.tag('search-result', { index }, JSON.stringify(hit, null, 2)), + XML.tag( + 'search-result', + { index }, + JSON.stringify(truncateIndexedContent(hit), null, 2), + ), ), ) : 'Search request returned no results',