further adjustments based on seb's feedback
This commit is contained in:
@@ -88,6 +88,15 @@ class SearchService extends BaseService {
|
||||
updateSettings(indexName, settings) {
|
||||
throw Error("updateSettings must be overridden by a child class")
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to perform transformations (if any) on products before indexation
|
||||
* @param products {Array.<Object>} - the list of products
|
||||
* @return {Array.<Object>} - returns the transformed products
|
||||
*/
|
||||
transformProducts(products) {
|
||||
throw Error("transformProducts must be overridden by a child class")
|
||||
}
|
||||
}
|
||||
|
||||
export default SearchService
|
||||
|
||||
@@ -1,65 +1,12 @@
|
||||
import { transformProduct } from "../utils/transform-product"
|
||||
|
||||
const INDEX_NS = "medusa-commerce"
|
||||
|
||||
async function loadIntoSearchEngine(container) {
|
||||
const meilisearchService = container.resolve("meilisearchService")
|
||||
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 transformedProducts = products.map(transformProduct)
|
||||
|
||||
await meilisearchService.addDocuments(
|
||||
`medusa-commerce_products`,
|
||||
transformedProducts
|
||||
)
|
||||
|
||||
iterCount += products.length
|
||||
lastSeenId = products.at(-1).id
|
||||
}
|
||||
}
|
||||
|
||||
export default async (container, options) => {
|
||||
try {
|
||||
const meilisearchService = container.resolve("meilisearchService")
|
||||
|
||||
await Promise.all(
|
||||
Object.entries(options.settings).map(([key, value]) =>
|
||||
meilisearchService.updateSettings(`${INDEX_NS}_${key}`, value)
|
||||
meilisearchService.updateSettings(key, value)
|
||||
)
|
||||
)
|
||||
|
||||
await loadIntoSearchEngine(container)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { SearchService } from "medusa-interfaces"
|
||||
import { MeiliSearch } from "meilisearch"
|
||||
import { transformProduct } from "../utils/transform-product"
|
||||
|
||||
class MeiliSearchService extends SearchService {
|
||||
constructor(container, options) {
|
||||
@@ -41,6 +42,11 @@ class MeiliSearchService extends SearchService {
|
||||
updateSettings(indexName, settings) {
|
||||
return this.client_.index(indexName).updateSettings(settings)
|
||||
}
|
||||
|
||||
transformProducts(products) {
|
||||
if (!products) return []
|
||||
return products.map(transformProduct)
|
||||
}
|
||||
}
|
||||
|
||||
export default MeiliSearchService
|
||||
|
||||
@@ -11,7 +11,7 @@ class ProductSearchSubscriber {
|
||||
|
||||
this.productService_ = productService
|
||||
|
||||
this.productIndexName = "medusa-commerce_products"
|
||||
this.productIndexName = productService.constructor.IndexName
|
||||
|
||||
this.eventBus_.subscribe("product.created", this.handleProductCreation)
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 +0,0 @@
|
||||
export const INDEX_NS = `medusa-commerce`
|
||||
Reference in New Issue
Block a user