From 82e5921fc521650edf4e202f3c0994c8fb60adb2 Mon Sep 17 00:00:00 2001 From: Jesiel Viana Date: Thu, 14 Dec 2023 17:06:25 -0300 Subject: [PATCH] =?UTF-8?q?alterando=20a=20query=20da=20busca=20b=C3=A1sic?= =?UTF-8?q?a=20para=20usar=20bool=20query=20tamb=C3=A9m?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 2 +- src/components/BasicSearchBox.tsx | 106 +++++++++++++++--------------- src/pages/api/search.ts | 21 ++---- 3 files changed, 60 insertions(+), 69 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c91ad1b..94550ba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 diff --git a/src/components/BasicSearchBox.tsx b/src/components/BasicSearchBox.tsx index caedd29..8dc6253 100644 --- a/src/components/BasicSearchBox.tsx +++ b/src/components/BasicSearchBox.tsx @@ -41,62 +41,62 @@ const BasicSearchBox = ({ }, []); return ( - { - 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 }) => ( -
-
2 ? onSubmit : undefined} - className="d-flex flex-gap-8 align-items-center" - > - onChange(e.target.value)} - /> + <> + { + 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 }) => ( + <> +
+ + {getAutocomplete()} +
- - toogleAdvancedConfig(true)} className="link-color d-flex align-items-center flex-gap-8"> - - {t('Advanced search')} - -
- )} - >
+ + )} + > + toogleAdvancedConfig(true)} className="link-color d-flex align-items-center flex-gap-8"> + + {t('Advanced search')} + + ); }; diff --git a/src/pages/api/search.ts b/src/pages/api/search.ts index 300a3df..9a01fee 100644 --- a/src/pages/api/search.ts +++ b/src/pages/api/search.ts @@ -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; } );