-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Meilisearch for improved search results and filters (#60)
- Loading branch information
1 parent
87c59df
commit e65c4af
Showing
14 changed files
with
294 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Read the documentation (https://strapi.io/documentation/3.0.0-beta.x/concepts/services.html#core-services) | ||
* to customize this service | ||
*/ | ||
const escapeRegex = (str) => str.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); | ||
|
||
module.exports = {}; | ||
module.exports = { | ||
getCategoriesIds(categoryName) { | ||
if (!categoryName) return []; | ||
|
||
return strapi | ||
.query('category') | ||
.model.find({ path: { $regex: `,${escapeRegex(categoryName)},` } }) | ||
.select('_id') | ||
.then((result) => result.map((v) => (v ? v.toObject()._id : null))); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const { MeiliSearch } = require('meilisearch'); | ||
const _ = require('lodash'); | ||
|
||
const meili = new MeiliSearch({ | ||
host: strapi.config.get('custom.meiliHost', 'http://127.0.0.1:7700'), | ||
apiKey: strapi.config.get('custom.meiliApiKey', ''), | ||
}); | ||
|
||
const PUBLIC_FIELDS = [ | ||
'_id', | ||
'slug', | ||
'stockStatus', | ||
'language', | ||
'publishedDate', | ||
'bookPages', | ||
'bookPublisher', | ||
'bookEdition', | ||
'bookAuthor', | ||
'reference', | ||
'price', | ||
'description', | ||
'name', | ||
'createdAt', | ||
'updatedAt', | ||
'category', | ||
]; | ||
|
||
const extractCategory = (product) => ({ | ||
...product, | ||
category: product.category && product.category._id, | ||
}); | ||
|
||
const updateProduct = async (product) => { | ||
if (product.show) { | ||
await meili.index('product').addDocuments([_.pick(extractCategory(product), PUBLIC_FIELDS)]); | ||
} else { | ||
await deleteProduct(product); | ||
} | ||
}; | ||
|
||
const partialUpdateProduct = async (product) => { | ||
if (product.show === false) { | ||
await deleteProduct(product); | ||
} else { | ||
await meili.index('product').updateDocuments([_.pick(extractCategory(product), PUBLIC_FIELDS)]); | ||
} | ||
}; | ||
|
||
const deleteProduct = async (product) => { | ||
try { | ||
await meili.index('product').deleteDocument(product._id); | ||
} catch (e) { | ||
console.error(e); | ||
console.error('Failed to delete product from meilisearch'); | ||
} | ||
}; | ||
|
||
const searchProduct = (query, properties) => meili.index('product').search(query, properties); | ||
|
||
module.exports = { | ||
updateProduct, | ||
partialUpdateProduct, | ||
deleteProduct, | ||
searchProduct, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { MeiliSearch } = require('meilisearch'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const _ = require('lodash'); | ||
|
||
const PUBLIC_FIELDS = [ | ||
'_id', | ||
'slug', | ||
'stockStatus', | ||
'language', | ||
'publishedDate', | ||
'bookPages', | ||
'bookPublisher', | ||
'bookEdition', | ||
'bookAuthor', | ||
'reference', | ||
'price', | ||
'description', | ||
'name', | ||
'createdAt', | ||
'updatedAt', | ||
'category', | ||
]; | ||
|
||
const createStrapiApp = async (projectPath) => { | ||
if (!projectPath) { | ||
throw new Error(` | ||
-> Path to strapi project is missing. | ||
-> Usage: node meilisearch.js [path]`); | ||
} | ||
|
||
let strapi; | ||
let app; | ||
try { | ||
strapi = require(require.resolve('strapi', { paths: [projectPath] })); | ||
const pkgJSON = require(path.resolve(projectPath, 'package.json')); | ||
if (!pkgJSON || !pkgJSON.dependencies || !pkgJSON.dependencies.strapi) { | ||
throw new Error(); | ||
} | ||
} catch (e) { | ||
throw new Error(` | ||
-> Strapi lib couldn\'t be found. Are the node_modules installed? | ||
-> Fix: yarn install or npm install`); | ||
} | ||
|
||
try { | ||
app = await strapi({ dir: projectPath }).load(); | ||
} catch (e) { | ||
throw new Error(` | ||
-> The migration couldn't be proceed because Strapi app couldn't start. | ||
-> ${e.message}`); | ||
} | ||
|
||
return app; | ||
}; | ||
|
||
const extractCategory = (product) => ({ | ||
...product, | ||
category: product.category && product.category._id, | ||
}); | ||
|
||
const run = async () => { | ||
const projectPath = process.argv[2]; | ||
const app = await createStrapiApp(projectPath); | ||
|
||
const meili = new MeiliSearch({ | ||
host: app.config.get('custom.meiliHost', 'http://127.0.0.1:7700'), | ||
apiKey: app.config.get('custom.meiliApiKey', ''), | ||
}); | ||
|
||
const index = meili.index('product'); | ||
|
||
try { | ||
await index.deleteAllDocuments(); | ||
} catch (e) { | ||
// Ignore | ||
} | ||
|
||
const count = await app.services.product.count(); | ||
let products; | ||
let i = 0; | ||
while (i < count) { | ||
products = await app.services.product.find({ _start: i }); | ||
i += products.length; | ||
await index.addDocuments( | ||
products | ||
.filter((prod) => prod.show) | ||
.map((prod) => _.pick(extractCategory(prod), PUBLIC_FIELDS)) | ||
); | ||
} | ||
}; | ||
|
||
run() | ||
.catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}) | ||
.then(() => { | ||
console.log('Migration successfully finished! 🎉'); | ||
process.exit(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3722,6 +3722,13 @@ cross-fetch@^3.0.4: | |
dependencies: | ||
node-fetch "2.6.1" | ||
|
||
cross-fetch@^3.0.5: | ||
version "3.1.1" | ||
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.1.tgz#a7ed5a9201d46223d805c5e9ecdc23ea600219eb" | ||
integrity sha512-eIF+IHQpRzoGd/0zPrwQmHwDC90mdvjk+hcbYhKoaRrEk4GEIDqdjs/MljmdPPoHTQudbmWS+f0hZsEpFaEvWw== | ||
dependencies: | ||
node-fetch "2.6.1" | ||
|
||
cross-spawn@^6.0.0, cross-spawn@^6.0.5: | ||
version "6.0.5" | ||
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" | ||
|
@@ -7435,6 +7442,13 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" | ||
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= | ||
|
||
meilisearch@^0.18.1: | ||
version "0.18.1" | ||
resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.18.1.tgz#6b12da60628ec5d6438ba677a8295971e1035938" | ||
integrity sha512-eGdcx5/0ktj5I2vC/bNbHg3ORuq9nh++fA39t03nG31J1SoFJmnx3Z0r83Q/yrfA+hAP/qhCI/6B8dHMEcCQJg== | ||
dependencies: | ||
cross-fetch "^3.0.5" | ||
|
||
memoize-one@^5.0.0: | ||
version "5.1.1" | ||
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0" | ||
|
Oops, something went wrong.