diff --git a/.github/workflows/docs-scraper.config.json b/.github/workflows/docs-scraper.config.json index 3c71142b5..708999cb3 100644 --- a/.github/workflows/docs-scraper.config.json +++ b/.github/workflows/docs-scraper.config.json @@ -1,5 +1,5 @@ { - "index_uid": "resumos-leic-v2", + "index_uid": "resumos-leic", "start_urls": ["https://resumos.leic.pt/"], "selectors": { "lvl0": { diff --git a/.github/workflows/gh-page-scraping.yml b/.github/workflows/gh-page-scraping.yml index fab82f786..d2b01b33a 100644 --- a/.github/workflows/gh-page-scraping.yml +++ b/.github/workflows/gh-page-scraping.yml @@ -7,7 +7,7 @@ jobs: scrape-site-for-search: name: 'Scrape Site for Search' if: github.event.deployment_status.state == 'success' && github.event.deployment.environment == 'production' - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@master - name: Run docs-scraper @@ -20,4 +20,4 @@ jobs: -e MEILISEARCH_HOST_URL=$HOST_URL \ -e MEILISEARCH_API_KEY=$API_KEY \ -v $CONFIG_FILE_PATH:/docs-scraper/config.json \ - getmeili/docs-scraper:v0.12.1 pipenv run ./docs_scraper config.json + getmeili/docs-scraper:v0.12.12 pipenv run ./docs_scraper config.json diff --git a/.meilisearch/README.md b/.meilisearch/README.md new file mode 100644 index 000000000..5bcfe328d --- /dev/null +++ b/.meilisearch/README.md @@ -0,0 +1,15 @@ +# Meilisearch + +## Commands to generate keys + +```sh +curl https://meilisearch.diogotc.com/keys \ + -H "Authorization: Bearer $MEILI_TOKEN" \ + --json '{"actions": ["search"], "indexes": ["resumos-leic"], "expiresAt": null, "description": "Search key for Resumos LEIC"}' +``` + +```sh +curl https://meilisearch.diogotc.com/keys \ + -H "Authorization: Bearer $MEILI_TOKEN" \ + --json '{"actions": ["documents.*", "indexes.*", "settings.*"], "indexes": ["resumos-leic"], "expiresAt": null, "description": "Scraping key for Resumos LEIC"}' +``` diff --git a/.meilisearch/docker-compose.yml b/.meilisearch/docker-compose.yml index 912f8b73d..1a4a64cdc 100644 --- a/.meilisearch/docker-compose.yml +++ b/.meilisearch/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.8' services: meilisearch: - image: getmeili/meilisearch:v0.20.0 + image: getmeili/meilisearch:v1.7.6 ports: - 7700:7700 volumes: diff --git a/gatsby-config.js b/gatsby-config.js index aeec9d926..1649a01fd 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -42,6 +42,11 @@ module.exports = { contributorsLink: 'https://github.com/leic-pt/resumos-leic/graphs/contributors', vercelLink: 'https://vercel.com/?utm_source=leic-pt&utm_campaign=oss', }, + search: { + host: 'https://meilisearch.diogotc.com', + apiKey: 'a66ec2f3c48d2f827a81e850de53d2a764b5d5f420111e15c686eec6885480f5', + indexName: 'resumos-leic', + }, }, plugins: [ { diff --git a/package.json b/package.json index c6a735d40..92b725cbc 100755 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "gatsby-source-filesystem": "^5.13.0", "gatsby-transformer-remark": "^6.13.0", "katex": "^0.16.9", - "meilisearch": "^0.30.0", + "meilisearch": "^0.41.0", "mermaid": "^10.9.0", "mini-svg-data-uri": "^1.4.4", "prismjs": "^1.29.0", diff --git a/src/components/SearchBar/SearchBar.js b/src/components/SearchBar/SearchBar.js index f12e1aaca..dd206a00f 100644 --- a/src/components/SearchBar/SearchBar.js +++ b/src/components/SearchBar/SearchBar.js @@ -1,15 +1,11 @@ import MeiliSearch from 'meilisearch'; -import React, { useCallback, useEffect, useState } from 'react'; +import { useStaticQuery, graphql } from 'gatsby'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import Dialog from '../Dialog/Dialog'; import Search from '../icons/Search'; import './SearchBar.css'; import SearchModal from './SearchModal'; -const searchClient = new MeiliSearch({ - host: 'https://meilisearch.diogotc.com', - apiKey: 'S3goii63d54d41ee506eb2bdfea46f62cb3b90a3141ec34ee3e546db99dbffd73a7872e9', -}); - const SearchBar = () => { const [open, setOpen] = useState(false); const [filterBySection, setFilterBySection] = useState(true); @@ -22,6 +18,29 @@ const SearchBar = () => { [setFilterBySection] ); + const data = useStaticQuery(graphql` + query SearchConfigQuery { + site { + siteMetadata { + search { + host + apiKey + indexName + } + } + } + } + `); + const { host, apiKey, indexName } = data.site.siteMetadata.search; + const searchClient = useMemo( + () => + new MeiliSearch({ + host, + apiKey, + }), + [host, apiKey] + ); + // Global keybinds useEffect(() => { const handleKeyPress = (event) => { @@ -75,6 +94,7 @@ const SearchBar = () => { { +const SearchModal = ({ + searchClient, + indexName, + onClose, + filterBySection, + handleToggleFilterBySection, +}) => { // Refs to elements of search, to use with autocomplete-core const formElementRef = React.useRef(null); const inputRef = React.useRef(null); @@ -44,6 +50,7 @@ const SearchModal = ({ searchClient, onClose, filterBySection, handleToggleFilte }, getSources: createGetSources({ searchClient, + indexName, onClose, section: filterBySection && currentSection, }), diff --git a/src/components/SearchBar/autocomplete.js b/src/components/SearchBar/autocomplete.js index 6892e9767..0f7ddf7b2 100644 --- a/src/components/SearchBar/autocomplete.js +++ b/src/components/SearchBar/autocomplete.js @@ -1,6 +1,6 @@ import { navigate } from 'gatsby'; -export function createGetSources({ searchClient, onClose, section }) { +export function createGetSources({ searchClient, indexName, onClose, section }) { return async ({ query, setContext, setStatus }) => { if (!query) { // Return no results if query is empty @@ -8,7 +8,7 @@ export function createGetSources({ searchClient, onClose, section }) { } try { - const { hits, nbHits } = await searchClient.index('resumos-leic-v2').search(query, { + const { hits, nbHits } = await searchClient.index(indexName).search(query, { attributesToHighlight: [ 'hierarchy_lvl1', 'hierarchy_lvl2', diff --git a/yarn.lock b/yarn.lock index 0eaface45..687b24ba8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4080,7 +4080,7 @@ create-gatsby@^3.13.1: dependencies: "@babel/runtime" "^7.20.13" -cross-fetch@^3.1.5: +cross-fetch@^3.1.5, cross-fetch@^3.1.6: version "3.1.8" resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.8.tgz#0327eba65fd68a7d119f8fb2bf9334a1a7956f82" integrity sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg== @@ -8313,12 +8313,12 @@ media-typer@0.3.0: resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ== -meilisearch@^0.30.0: - version "0.30.0" - resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.30.0.tgz#707f9a6b07440c496b965379616e084f112160ae" - integrity sha512-3y1hALOwTDpquYar+gDREqRasFPWKxkWAhk6h+RF+nKObPVf9N77wcTNvukGwOKbxRyJnKge0OPgAB1BkB9VVw== +meilisearch@^0.41.0: + version "0.41.0" + resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.41.0.tgz#98fc50dd8ce4f2446aa4445e3cecd47e3c9afde5" + integrity sha512-5KcGLxEXD7E+uNO7R68rCbGSHgCqeM3Q3RFFLSsN7ZrIgr8HPDXVAIlP4LHggAZfk0FkSzo8VSXifHCwa2k80g== dependencies: - cross-fetch "^3.1.5" + cross-fetch "^3.1.6" mem@^8.1.1: version "8.1.1"