further adjustments based on seb's feedback
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user