further adjustments based on seb's feedback

This commit is contained in:
zakariaelas committed 2021-09-22 15:44:52 +01:00
1 parent 560cc0de55
commit 451e3bcdcf
9 files changed
+74 -59

No files matched your search

@@ -14,7 +14,7 @@ export default async (req, res) => {
}
try {
const { q, indexName, ...options } = value
const { q, ...options } = value
const searchService = req.scope.resolve("searchService")
@@ -1,3 +1,49 @@
import ProductService from "../services/product"
async function loadProductsIntoSearchEngine(container) {
const searchService = container.resolve("searchService")
const productService = container.resolve("productService")
const TAKE = 20
const totalCount = await productService.count()
let iterCount = 0,
lastSeenId = "prod"
while (iterCount < totalCount) {
const products = await productService.list(
{ id: { gt: 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 transformedBatch = searchService.transformProducts(products)
await searchService.addDocuments(ProductService.IndexName, transformedBatch)
iterCount += products.length
lastSeenId = products[products.length - 1].id
}
}
export default async ({ container }) => {
const searchService = container.resolve("searchService")
const logger = container.resolve("logger")
@@ -7,4 +53,6 @@ export default async ({ container }) => {
)
return
}
await loadProductsIntoSearchEngine(container)
}
+1 -2
View File
@@ -2,14 +2,13 @@ import _ from "lodash"
import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
import { Brackets } from "typeorm"
import { INDEX_NS } from "../utils/index-ns"
/**
* Provides layer to manipulate products.
* @implements BaseService
*/
class ProductService extends BaseService {
static IndexName = `${INDEX_NS}_products`
static IndexName = `products`
static Events = {
UPDATED: "product.updated",
CREATED: "product.created",
+7
View File
@@ -61,6 +61,13 @@ class DefaultSearchService extends SearchService {
"This is an empty method: updateSettings must be overridden by a child class"
)
}
transformProducts(products) {
this.logger_.warn(
"This is an empty method: transformProducts must be overridden by a child class"
)
return products
}
}
export default DefaultSearchService
-1
View File
@@ -1 +0,0 @@
export const INDEX_NS = `medusa-commerce`