Skip to content

Commit

Permalink
Merge pull request #2681 from Hyperkid123/use-improved-suggester
Browse files Browse the repository at this point in the history
Combine suggester restuls.
  • Loading branch information
Hyperkid123 authored Nov 6, 2023
2 parents 69e0e3c + 7aaa8f6 commit d2df52b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/components/Search/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -82,6 +82,22 @@ 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);
return {
item: {
title: allTitle,
bundleTitle,
description: abstract,
pathname: url.pathname,
},
allTitle,
};
});
}

const SearchInput = ({ onStateChange }: SearchInputListener) => {
const [isOpen, setIsOpen] = useState(false);
const [searchValue, setSearchValue] = useState('');
Expand Down Expand Up @@ -182,25 +198,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>|<\/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(
Expand Down
6 changes: 6 additions & 0 deletions src/components/Search/SearchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export type SearchResponseType = {

export type SearchAutoSuggestionResponseType = {
suggest: {
improvedInfixSuggester: {
[recordId: string]: {
numFound: number;
suggestions: SearchAutoSuggestionResultItem[];
};
};
default: {
[recordId: string]: {
numFound: number;
Expand Down

0 comments on commit d2df52b

Please sign in to comment.