Skip to content

Commit

Permalink
alterando a query da busca básica para usar bool query também
Browse files Browse the repository at this point in the history
  • Loading branch information
jesielviana committed Dec 14, 2023
1 parent 5180820 commit 82e5921
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
// Add those two lines:
"editor.formatOnSave": true, // Tell VSCode to format files on save
Expand Down
106 changes: 53 additions & 53 deletions src/components/BasicSearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,62 +41,62 @@ const BasicSearchBox = ({
}, []);

return (
<SearchBox
autocompleteMinimumCharacters={3}
searchAsYouType={false}
autocompleteResults={{
linkTarget: '_blank',
sectionTitle: t('Open link') || '',
titleField: titleFieldName,
urlField: '',
shouldTrackClickThrough: true,
}}
autocompleteSuggestions={true}
debounceLength={0}
onSubmit={(searchTerm) => {
updateOpetatorConfig('OR');
router.query.q = searchTerm;
router.push(router);
}}
onSelectAutocomplete={(selection: any, item: any, defaultOnSelectAutocomplete: any) => {
if (selection.suggestion) {
updateOpetatorConfig('AND');
defaultOnSelectAutocomplete(selection);
} else {
router.push(`${VIVO_URL_ITEM_BASE}/${itemLinkPrefix}${selection.id.raw}&lang=${router.locale}`);
}
}}
view={({ value, onChange, onSubmit }) => (
<div>
<form
onSubmit={value.trim().length > 2 ? onSubmit : undefined}
className="d-flex flex-gap-8 align-items-center"
>
<input
className="sui-search-box__text-input"
type="text"
value={value}
placeholder={`${t('Enter at least 3 characters and search among')} ${t('numberFormat', {
value: docsCount || 0,
})} ${t('documents')}`}
onChange={(e) => onChange(e.target.value)}
/>
<>
<SearchBox
autocompleteMinimumCharacters={3}
searchAsYouType={false}
autocompleteResults={{
linkTarget: '_blank',
sectionTitle: t('Open link') || '',
titleField: titleFieldName,
urlField: '',
shouldTrackClickThrough: true,
}}
autocompleteSuggestions={true}
debounceLength={0}
onSubmit={(searchTerm) => {
updateOpetatorConfig('OR');
router.query.q = searchTerm;
router.push(router);
}}
onSelectAutocomplete={(selection: any, item: any, defaultOnSelectAutocomplete: any) => {
if (selection.suggestion) {
updateOpetatorConfig('AND');
selection.suggestion = `\"${selection.suggestion}\"`;
console.log('selection', selection);
defaultOnSelectAutocomplete(selection);
} else {
router.push(`${VIVO_URL_ITEM_BASE}/${itemLinkPrefix}${selection.id.raw}&lang=${router.locale}`);
}
}}
inputView={({ getAutocomplete, getInputProps, getButtonProps }) => (
<>
<div className="sui-search-box__wrapper">
<input
{...getInputProps({
placeholder: `${t('Enter at least 3 characters and search among')} ${t('numberFormat', {
value: docsCount || 0,
})} ${t('documents')}`,
})}
/>
{getAutocomplete()}
</div>
<button
disabled={value.trim().length < 3}
className="button sui-search-box__submit d-flex align-items-center flex-gap-8"
type="submit"
{...getButtonProps({
disabled: getInputProps()?.value?.trim().length < 3,
className: 'button sui-search-box__submit d-flex align-items-center flex-gap-8',
})}
>
<IoSearch />
{t('Search')}
<IoSearch /> {t('Search')}
</button>
</form>
<span onClick={() => toogleAdvancedConfig(true)} className="link-color d-flex align-items-center flex-gap-8">
<IoArrowRedoOutline />
{t('Advanced search')}
</span>
</div>
)}
></SearchBox>
</>
)}
></SearchBox>
<span onClick={() => toogleAdvancedConfig(true)} className="link-color d-flex align-items-center flex-gap-8">
<IoArrowRedoOutline />
{t('Advanced search')}
</span>
</>
);
};

Expand Down
21 changes: 6 additions & 15 deletions src/pages/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,13 @@ function builConnector(index: string) {
// transforming the query before sending to Elasticsearch using the requestState and queryConfig
const searchFields: any = queryConfig.search_fields;
// @ts-ignore
if (requestState.searchTerm.indexOf('(') >= 0) {
const fullQuery = new QueryFormat().toElasticsearch(requestState.searchTerm, Object.keys(searchFields));
requestBody.query = fullQuery;
} else {
requestBody.query = {
multi_match: {
query: requestState.searchTerm,
// @ts-ignore
operator: queryConfig.operator,
fields: Object.keys(searchFields).map((fieldName) => {
const weight = searchFields[fieldName].weight || 1;
return `${fieldName}^${weight}`;
}),
},
};
if (requestState.searchTerm.indexOf('(') < 0) {
// @ts-ignore
requestState.searchTerm = `(all:${requestState.searchTerm})`;
}
console.log('requestState.searchTerm: ', requestState.searchTerm);
const fullQuery = new QueryFormat().toElasticsearch(requestState.searchTerm, Object.keys(searchFields));
requestBody.query = fullQuery;
return requestBody;
}
);
Expand Down

0 comments on commit 82e5921

Please sign in to comment.