├── public ├── robots.txt ├── cover.jpg ├── logo.png ├── showcase.png ├── wide_logo.png ├── manifest.json └── index.html ├── backend ├── package.json ├── docker-compose.yml ├── src │ ├── steps │ │ ├── create_index.js │ │ ├── delete_index.js │ │ ├── upload_documents.js │ │ └── change_settings.js │ ├── search.js │ └── index.js └── sample.json ├── README.md ├── src ├── index.css ├── index.js ├── AppInitial.js ├── App.js └── serviceWorker.js ├── package.json ├── LICENSE └── .gitignore /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riccardogiorato/meilisearch-react-demo/HEAD/public/cover.jpg -------------------------------------------------------------------------------- /public/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riccardogiorato/meilisearch-react-demo/HEAD/public/logo.png -------------------------------------------------------------------------------- /public/showcase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riccardogiorato/meilisearch-react-demo/HEAD/public/showcase.png -------------------------------------------------------------------------------- /public/wide_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riccardogiorato/meilisearch-react-demo/HEAD/public/wide_logo.png -------------------------------------------------------------------------------- /backend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "meili_backend", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "scripts": { 7 | "docker_install": "docker-compose up -d" 8 | } 9 | } -------------------------------------------------------------------------------- /backend/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.6' 2 | services: 3 | meilisearch_tutorial: 4 | image: getmeili/meilisearch:latest 5 | restart: always 6 | volumes: 7 | - ./data.ms:/data.ms 8 | ports: 9 | - "7700:7700" 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # meili_react_demo 2 | showcase of Meilisearch with a simple React Frontend 3 | 4 | [**Demo: https://meili-react-demo.netlify.app/**](https://meili-react-demo.netlify.app/) 5 | 6 | [![](/public/showcase.png)](https://meili-react-demo.netlify.app/) 7 | -------------------------------------------------------------------------------- /backend/src/steps/create_index.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700' 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | await meili.createIndex("decathlon", { primaryKey: "id" }); 12 | 13 | } catch (e) { 14 | console.log("Meili error: ", e.message); 15 | } 16 | })(); 17 | -------------------------------------------------------------------------------- /backend/src/steps/delete_index.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700' 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | const indexDelete = await meili.getIndex("decathlon"); 12 | indexDelete.deleteIndex(); 13 | 14 | } catch (e) { 15 | console.log("Meili error: ", e.message); 16 | } 17 | })(); 18 | -------------------------------------------------------------------------------- /backend/src/search.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700', 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | const index = await meili.getIndex("decathlon"); 12 | 13 | console.log(await index.search('dumbell')) 14 | 15 | } catch (e) { 16 | console.log("Meili error: ", e.message); 17 | } 18 | })(); 19 | -------------------------------------------------------------------------------- /backend/src/steps/upload_documents.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700' 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | const decathlon = require("../../decathlon.json"); 12 | 13 | const index = await meili.getIndex("decathlon"); 14 | 15 | await index.addDocuments(decathlon); 16 | 17 | } catch (e) { 18 | console.log("Meili error: ", e.message); 19 | } 20 | })(); 21 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | .header { 11 | min-height: 450px; 12 | background: linear-gradient(rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)), 13 | url("/cover.jpg"); 14 | background-size: cover !important; 15 | } 16 | 17 | .searchBox { 18 | height: 64px; 19 | max-width: 740px; 20 | } 21 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "Decathlon Meili", 3 | "name": "Decathlon Meili - Search", 4 | "icons": [ 5 | { 6 | "src": "logo.png", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /backend/src/steps/change_settings.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700' 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | const index = await meili.getIndex("decathlon"); 12 | 13 | const newSettings = { 14 | rankingRules: [ 15 | "typo", 16 | "words", 17 | "proximity", 18 | "attribute", 19 | "wordsPosition", 20 | "exactness", 21 | "desc(creation_date)" 22 | ] 23 | }; 24 | 25 | await index.updateSettings(newSettings); 26 | 27 | } catch (e) { 28 | console.log("Meili error: ", e.message); 29 | } 30 | })(); 31 | -------------------------------------------------------------------------------- /backend/sample.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": 100013768717, 3 | "name": "Fitness Foldable Shoe Bag", 4 | "url": "https://www.decathlon.com/products/gym-foldable-shoe-bag", 5 | "vendor": "Domyos", 6 | "category": "Sport bag", 7 | "tags": [ 8 | "2", 9 | "3", 10 | "5", 11 | "Artistic Gymnastics", 12 | "Boy's", 13 | "CARDIO_FITNESS_ACCESSORIES", 14 | "CARDIO_FITNESS_BAGS", 15 | "CODE_R3: 11782", 16 | "Design Code: 128373", 17 | "Family: 11782" 18 | ], 19 | "images": "https://cdn.shopify.com/s/files/1/1330/6287/products/sac_20a_20chaussure_20kaki_20_7C_20001_20_7C_20PSHOT_20_490180e6-44e4-4340-8e3d-c29eb70c6ac8.jpg?v=1584683232", 20 | "creation_date": "2020-04-03T15:58:48-07:00", 21 | "price": "2.49" 22 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "meili_react_demo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "cra-template": "1.0.3", 7 | "meilisearch": "^0.14.2", 8 | "react": "^16.13.1", 9 | "react-dom": "^16.13.1", 10 | "react-scripts": "3.4.1" 11 | }, 12 | "scripts": { 13 | "postinstall": "npm run setup_meili", 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "test": "react-scripts test", 17 | "eject": "react-scripts eject", 18 | "setup_meili": "cd backend && npm run docker_install && node src/index.js" 19 | }, 20 | "eslintConfig": { 21 | "extends": "react-app" 22 | }, 23 | "browserslist": { 24 | "production": [ 25 | ">0.2%", 26 | "not dead", 27 | "not op_mini all" 28 | ], 29 | "development": [ 30 | "last 1 chrome version", 31 | "last 1 firefox version", 32 | "last 1 safari version" 33 | ] 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /backend/src/index.js: -------------------------------------------------------------------------------- 1 | const MeiliSearch = require("meilisearch"); 2 | 3 | (async () => { 4 | try { 5 | const config = { 6 | host: 'http://127.0.0.1:7700' 7 | }; 8 | 9 | const meili = new MeiliSearch(config); 10 | 11 | await meili.isHealthy() 12 | await meili.createIndex('decathlon', { primaryKey: "id" }); 13 | 14 | const decathlon = require("../decathlon.json"); 15 | const index = await meili.getIndex("decathlon"); 16 | 17 | await index.addDocuments(decathlon); 18 | const newSettings = { 19 | rankingRules: [ 20 | "typo", 21 | "words", 22 | "proximity", 23 | "attribute", 24 | "wordsPosition", 25 | "exactness", 26 | "desc(creation_date)" 27 | ] 28 | }; 29 | 30 | await index.updateSettings(newSettings); 31 | 32 | console.dir(await index.getSettings()); 33 | } catch (e) { 34 | console.error(e); 35 | console.log("Meili error: ", e.message); 36 | } 37 | })(); 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Riccardo Giorato 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 18 | 19 | 28 | Decathlon - Search Meili 29 | 30 | 31 | 32 |
33 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/AppInitial.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | // TODO configure the MeiliSearch Client 4 | 5 | function App() { 6 | const [searchedWord, setSearch] = useState("dumbell"); 7 | const [resultSearch, setResults] = useState([]); 8 | const [resultCards, setCards] = useState([]); 9 | 10 | // TODO add function to send searchedWord to MeiliSearch when 11 | 12 | // TODO add function to parse the JSON object 13 | // of the resultSearch to resultCards components to display 14 | 15 | return ( 16 |
17 |
18 |
19 | 25 |

26 | Stop looking for an item — find it and work hard! 27 |

28 | 47 |
48 |
49 |
50 |
{resultCards}
51 |
52 |
53 | ); 54 | } 55 | 56 | export default App; 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 106 | 107 | # dependencies 108 | /node_modules 109 | /.pnp 110 | .pnp.js 111 | 112 | # testing 113 | /coverage 114 | 115 | # production 116 | /build 117 | 118 | # misc 119 | .DS_Store 120 | .env.local 121 | .env.development.local 122 | .env.test.local 123 | .env.production.local 124 | 125 | npm-debug.log* 126 | yarn-debug.log* 127 | yarn-error.log* 128 | backend/data.ms/**/**.** 129 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | import MeiliSearch from "meilisearch"; 4 | 5 | const client = new MeiliSearch({ 6 | host: "http://127.0.0.1:7700/", 7 | }); 8 | 9 | const index = client.getIndex("decathlon"); 10 | 11 | function App() { 12 | const [searchedWord, setSearch] = useState(""); 13 | const [resultSearch, setResults] = useState([]); 14 | const [resultCards, setCards] = useState([]); 15 | 16 | useEffect(() => { 17 | // Create an scoped async function in the hook 18 | async function searchWithMeili() { 19 | const search = await index.search(searchedWord, { 20 | limit: 24, 21 | attributesToHighlight: ["name"], 22 | }); 23 | setResults(search.hits); 24 | } 25 | // Execute the created function directly 26 | searchWithMeili(); 27 | }, [searchedWord]); 28 | 29 | useEffect(() => { 30 | let arrayItems = []; 31 | for (let i = 0; i < resultSearch.length; i++) { 32 | const product = resultSearch[i]; 33 | arrayItems.push( 34 |
35 | 39 | {product.name} { 44 | e.target.onerror = null; 45 | e.target.src = "/wide_logo.png"; 46 | }} 47 | /> 48 |
49 |
50 | {product.category} 51 |
52 |
53 | {product.vendor} - {product.name.substr(0, 20)} 54 |
55 |

56 | $ {product.price} 57 |

58 |
59 |
60 |
61 | ); 62 | } 63 | setCards(arrayItems); 64 | }, [resultSearch]); 65 | 66 | return ( 67 |
68 |
69 |
70 | 76 |

77 | Stop looking for an item — find it and work hard! 78 |

79 | 98 |
99 |
100 | 101 |
102 |
{resultCards}
103 |
104 |
105 | ); 106 | } 107 | 108 | export default App; 109 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | --------------------------------------------------------------------------------