From 3a10e3737746d6da9ed53688cca44648276226fb Mon Sep 17 00:00:00 2001 From: zakariaelas Date: Mon, 20 Sep 2021 14:44:58 +0100 Subject: [PATCH] adjustments rename search subscriber fix: comments in medusa-interfaces/search-service.js add: use cursor pagination when indexing all products to search engine fix: emit product_id on product-variant creation fix: move search API route under products/search fix: use meilisearchService instead of searchService in plugin loader --- .../medusa-interfaces/src/search-service.js | 22 ++--- .../src/loaders/index.js | 4 +- packages/medusa/src/api/routes/store/index.js | 2 - .../src/api/routes/store/products/index.js | 1 + .../store/{search => products}/search.js | 5 +- .../src/api/routes/store/search/index.js | 12 --- .../medusa/src/services/product-variant.js | 1 + packages/medusa/src/services/product.js | 80 ++++++++++++------- .../{search.js => product-search.js} | 4 +- 9 files changed, 68 insertions(+), 63 deletions(-) rename packages/medusa/src/api/routes/store/{search => products}/search.js (82%) delete mode 100644 packages/medusa/src/api/routes/store/search/index.js rename packages/medusa/src/subscribers/{search.js => product-search.js} (97%) diff --git a/packages/medusa-interfaces/src/search-service.js b/packages/medusa-interfaces/src/search-service.js index 7b42848afc..901dd00d3f 100644 --- a/packages/medusa-interfaces/src/search-service.js +++ b/packages/medusa-interfaces/src/search-service.js @@ -11,8 +11,8 @@ class SearchService extends BaseService { /** * Used to create an index - * @param indexName {string} - the index name. - * @param [options] {string} - the index name. + * @param indexName {string} - the index name + * @param [options] {string} - the index name * @return {Promise<{object}>} - returns response from search engine provider */ createIndex(indexName, options) { @@ -29,8 +29,8 @@ class SearchService extends BaseService { } /** - * Used to index documents by the search engine provider. - * @param indexName {string} - the index name. + * Used to index documents by the search engine provider + * @param indexName {string} - the index name * @param documents {Array.} - documents array to be indexed * @return {Promise<{object}>} - returns response from search engine provider */ @@ -50,8 +50,8 @@ class SearchService extends BaseService { /** * Used to delete document - * @param indexName {string} - the index name. - * @param document_id {string} - the id of the document. + * @param indexName {string} - the index name + * @param document_id {string} - the id of the document * @return {Promise<{object}>} - returns response from search engine provider */ deleteDocument(indexName, document_id) { @@ -60,7 +60,7 @@ class SearchService extends BaseService { /** * Used to delete all documents - * @param indexName {string} - the index name. + * @param indexName {string} - the index name * @return {Promise<{object}>} - returns response from search engine provider */ deleteAllDocuments(indexName) { @@ -69,10 +69,10 @@ class SearchService extends BaseService { /** * Used to search for a document in an index - * @param indexName {string} - the index name. - * @param query {string} - the search query. + * @param indexName {string} - the index name + * @param query {string} - the search query * @param options {object} - any options passed to the request object other than the query and indexName - * e.g. pagination options, filtering options, etc. + * e.g. pagination options, filtering options, etc * @return {Promise<{object}>} - returns response from search engine provider */ search(indexName, query, options) { @@ -81,7 +81,7 @@ class SearchService extends BaseService { /** * Used to update the settings of an index - * @param indexName {string} - the index name. + * @param indexName {string} - the index name * @param settings {object} - settings object * @return {Promise<{object}>} - returns response from search engine provider */ diff --git a/packages/medusa-plugin-meilisearch/src/loaders/index.js b/packages/medusa-plugin-meilisearch/src/loaders/index.js index 9093057245..16388cf689 100644 --- a/packages/medusa-plugin-meilisearch/src/loaders/index.js +++ b/packages/medusa-plugin-meilisearch/src/loaders/index.js @@ -2,11 +2,11 @@ const INDEX_NS = "medusa-commerce" export default async (container, options) => { try { - const searchService = container.resolve("searchService") + const meilisearchService = container.resolve("meilisearchService") await Promise.all( Object.entries(options.settings).map(([key, value]) => - searchService.updateSettings(`${INDEX_NS}_${key}`, value) + meilisearchService.updateSettings(`${INDEX_NS}_${key}`, value) ) ) } catch (err) { diff --git a/packages/medusa/src/api/routes/store/index.js b/packages/medusa/src/api/routes/store/index.js index adf675ab4f..413ea3237b 100644 --- a/packages/medusa/src/api/routes/store/index.js +++ b/packages/medusa/src/api/routes/store/index.js @@ -15,7 +15,6 @@ import returnReasonRoutes from "./return-reasons" import swapRoutes from "./swaps" import variantRoutes from "./variants" import giftCardRoutes from "./gift-cards" -import searchRoutes from "./search" const route = Router() @@ -44,7 +43,6 @@ export default (app, container, config) => { returnRoutes(route) giftCardRoutes(route) returnReasonRoutes(route) - searchRoutes(route) return app } diff --git a/packages/medusa/src/api/routes/store/products/index.js b/packages/medusa/src/api/routes/store/products/index.js index 8601550f3b..51748a9568 100644 --- a/packages/medusa/src/api/routes/store/products/index.js +++ b/packages/medusa/src/api/routes/store/products/index.js @@ -7,6 +7,7 @@ export default app => { app.use("/products", route) route.get("/", middlewares.wrap(require("./list-products").default)) + route.post("/search", middlewares.wrap(require("./search").default)) route.get("/:id", middlewares.wrap(require("./get-product").default)) return app diff --git a/packages/medusa/src/api/routes/store/search/search.js b/packages/medusa/src/api/routes/store/products/search.js similarity index 82% rename from packages/medusa/src/api/routes/store/search/search.js rename to packages/medusa/src/api/routes/store/products/search.js index 567fdac1fa..668e24047e 100644 --- a/packages/medusa/src/api/routes/store/search/search.js +++ b/packages/medusa/src/api/routes/store/products/search.js @@ -1,11 +1,10 @@ import { Validator, MedusaError } from "medusa-core-utils" -import { INDEX_NS } from "../../../../utils/index-ns" +import ProductService from "../../../../services/product" export default async (req, res) => { const schema = Validator.object() .keys({ q: Validator.string().required(), - indexName: Validator.string().required(), }) .options({ allowUnknown: true }) @@ -20,7 +19,7 @@ export default async (req, res) => { const searchService = req.scope.resolve("searchService") const results = await searchService.search( - `${INDEX_NS}_${indexName}`, + ProductService.IndexName, q, options ) diff --git a/packages/medusa/src/api/routes/store/search/index.js b/packages/medusa/src/api/routes/store/search/index.js deleted file mode 100644 index 3f71489d9f..0000000000 --- a/packages/medusa/src/api/routes/store/search/index.js +++ /dev/null @@ -1,12 +0,0 @@ -import { Router } from "express" -import middlewares from "../../../middlewares" - -const route = Router() - -export default app => { - app.use("/search", route) - - route.post("/", middlewares.wrap(require("./search").default)) - - return app -} diff --git a/packages/medusa/src/services/product-variant.js b/packages/medusa/src/services/product-variant.js index da16b6d00c..657956430d 100644 --- a/packages/medusa/src/services/product-variant.js +++ b/packages/medusa/src/services/product-variant.js @@ -203,6 +203,7 @@ class ProductVariantService extends BaseService { .withTransaction(manager) .emit(ProductVariantService.Events.CREATED, { id: result.id, + product_id: result.product_id, }) return result diff --git a/packages/medusa/src/services/product.js b/packages/medusa/src/services/product.js index 5521594be6..e06e13068f 100644 --- a/packages/medusa/src/services/product.js +++ b/packages/medusa/src/services/product.js @@ -749,41 +749,59 @@ class ProductService extends BaseService { return product } + /** + * Loads all products into the search engine + */ + async loadIntoSearchEngine() { if (this.searchService_.isDefault) return - const products = await this.list( - {}, - { - select: [ - "id", - "title", - "subtitle", - "description", - "handle", - "is_giftcard", - "discountable", - "thumbnail", - "profile_id", - "collection_id", - "type_id", - "origin_country", - "created_at", - "updated_at", - ], - relations: ["variants", "tags", "type", "collection"], - } - ) - const flattenSkus = product => { - product.sku = flattenField(product.variants, "sku").filter(Boolean) - return product - } - const productsWithSkus = products.map(product => flattenSkus(product)) + const TAKE = 20 + const totalCount = await this.count() + let iterCount = 0, + lastSeenId = "" - await this.searchService_.addDocuments( - ProductService.IndexName, - productsWithSkus - ) + while (iterCount < totalCount) { + console.log({ lastSeenId }) + const products = await this.list( + { id: { gte: lastSeenId } }, + { + select: [ + "id", + "title", + "subtitle", + "description", + "handle", + "is_giftcard", + "discountable", + "thumbnail", + "profile_id", + "collection_id", + "type_id", + "origin_country", + "created_at", + "updated_at", + ], + relations: ["variants", "tags", "type", "collection"], + take: TAKE, + order: { id: "ASC" }, + } + ) + const flattenSkus = product => { + product.sku = flattenField(product.variants, "sku").filter(Boolean) + return product + } + + const productsWithSkus = products.map(product => flattenSkus(product)) + + await this.searchService_.addDocuments( + ProductService.IndexName, + productsWithSkus + ) + + iterCount += TAKE + lastSeenId = products.at(-1).id + } } } diff --git a/packages/medusa/src/subscribers/search.js b/packages/medusa/src/subscribers/product-search.js similarity index 97% rename from packages/medusa/src/subscribers/search.js rename to packages/medusa/src/subscribers/product-search.js index e54089f003..9781c8220a 100644 --- a/packages/medusa/src/subscribers/search.js +++ b/packages/medusa/src/subscribers/product-search.js @@ -2,7 +2,7 @@ import ProductService from "../services/product" import ProductVariantService from "../services/product-variant" import { flattenField } from "../utils/flatten-field" -class SearchSubscriber { +class ProductSearchSubscriber { constructor({ eventBusService, searchService, productService }, options) { this.eventBus_ = eventBusService @@ -86,4 +86,4 @@ class SearchSubscriber { } } -export default SearchSubscriber +export default ProductSearchSubscriber