From 9c26a732328940f3ac59ec566cf7cdf30be8d12d Mon Sep 17 00:00:00 2001 From: Martin Marosi Date: Mon, 6 Nov 2023 12:41:03 +0100 Subject: [PATCH] Combine suggester restuls. --- src/components/Search/SearchInput.tsx | 41 +++++++++++++++++---------- src/components/Search/SearchTypes.ts | 6 ++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/components/Search/SearchInput.tsx b/src/components/Search/SearchInput.tsx index bb308840c9..8cada9fb21 100644 --- a/src/components/Search/SearchInput.tsx +++ b/src/components/Search/SearchInput.tsx @@ -10,7 +10,7 @@ import uniq from 'lodash/uniq'; import uniqWith from 'lodash/uniqWith'; import './SearchInput.scss'; -import { AUTOSUGGEST_TERM_DELIMITER, SearchAutoSuggestionResponseType, SearchResponseType } from './SearchTypes'; +import { AUTOSUGGEST_TERM_DELIMITER, SearchAutoSuggestionResponseType, SearchAutoSuggestionResultItem, SearchResponseType } from './SearchTypes'; import EmptySearchState from './EmptySearchState'; import { isProd } from '../../utils/common'; import { useSegment } from '../../analytics/useSegment'; @@ -82,6 +82,21 @@ type SearchInputListener = { onStateChange: (isOpen: boolean) => void; }; +function parseSuggestions(suggestions: SearchAutoSuggestionResultItem[] = []) { + return suggestions.map((suggestion) => { + const [allTitle, bundleTitle, abstract] = suggestion.term.split(AUTOSUGGEST_TERM_DELIMITER); + const url = new URL(suggestion.payload); + const item = { + title: allTitle, + bundleTitle, + description: abstract, + pathname: url.pathname, + }; + // wrap multiple terms in quotes - otherwise search treats each as an individual term to search + return { item, allTitle }; + }); +} + const SearchInput = ({ onStateChange }: SearchInputListener) => { const [isOpen, setIsOpen] = useState(false); const [searchValue, setSearchValue] = useState(''); @@ -182,25 +197,21 @@ const SearchInput = ({ onStateChange }: SearchInputListener) => { const handleFetch = async (value = '') => { const response = (await fetch(SUGGEST_SEARCH_QUERY.replaceAll(REPLACE_TAG, value)).then((r) => r.json())) as SearchAutoSuggestionResponseType; - const items = (response?.suggest?.default[value]?.suggestions || []).map((suggestion) => { - const [allTitle, bundleTitle, abstract] = suggestion.term.split(AUTOSUGGEST_TERM_DELIMITER); - const url = new URL(suggestion.payload); - const pathname = url.pathname; - const item = { - title: allTitle, - bundleTitle, - description: abstract, - pathname, - }; - // wrap multiple terms in quotes - otherwise search treats each as an individual term to search - return { item, allTitle }; - }); + // parse default suggester + // parse improved suggester + let items: { item: SearchItem; allTitle: string }[] = []; + items = items + .concat( + parseSuggestions(response?.suggest?.default[value]?.suggestions), + parseSuggestions(response?.suggest?.improvedInfixSuggester[value]?.suggestions) + ) + .slice(0, 10); const suggests = uniq(items.map(({ allTitle }) => allTitle.replace(/(|<\/b>)/gm, '').trim())); let searchItems = items.map(({ item }) => item); if (items.length < 10) { const altTitleResults = (await fetch( BASE_URL.toString() - .replaceAll(REPLACE_TAG, `(${suggests.join(' OR ')} OR ${value})`) + .replaceAll(REPLACE_TAG, `(${suggests.length > 0 ? suggests.join(' OR ') + ' OR ' : ''}${value})`) .replaceAll(REPLACE_COUNT_TAG, '10') ).then((r) => r.json())) as { response: SearchResponseType }; searchItems = searchItems.concat( diff --git a/src/components/Search/SearchTypes.ts b/src/components/Search/SearchTypes.ts index 8eb2867209..7b44d7b20d 100644 --- a/src/components/Search/SearchTypes.ts +++ b/src/components/Search/SearchTypes.ts @@ -24,6 +24,12 @@ export type SearchResponseType = { export type SearchAutoSuggestionResponseType = { suggest: { + improvedInfixSuggester: { + [recordId: string]: { + numFound: number; + suggestions: SearchAutoSuggestionResultItem[]; + }; + }; default: { [recordId: string]: { numFound: number;