fix: use type to choose transformer before adding or replacing documents
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
export { countries } from "./countries"
|
||||
export { isoCountryLookup } from "./countries"
|
||||
export { transformIdableFields } from "./transform-idable-fields"
|
||||
export { indexTypes } from "./index-types"
|
||||
export { default as Validator } from "./validator"
|
||||
export { default as MedusaError } from "./errors"
|
||||
export { default as getConfigFile } from "./get-config-file"
|
||||
|
||||
@@ -32,9 +32,10 @@ class SearchService extends BaseService {
|
||||
* Used to index documents by the search engine provider
|
||||
* @param indexName {string} - the index name
|
||||
* @param documents {Array.<Object>} - documents array to be indexed
|
||||
* @param type {Array.<Object>} - type of documents to be added (e.g: products, regions, orders, etc)
|
||||
* @return {Promise<{object}>} - returns response from search engine provider
|
||||
*/
|
||||
addDocuments(indexName, documents) {
|
||||
addDocuments(indexName, documents, type) {
|
||||
throw Error("addDocuments must be overridden by a child class")
|
||||
}
|
||||
|
||||
@@ -42,9 +43,10 @@ class SearchService extends BaseService {
|
||||
* Used to replace documents
|
||||
* @param indexName {string} - the index name.
|
||||
* @param documents {Object} - array of document objects that will replace existing documents
|
||||
* @param type {Array.<Object>} - type of documents to be replaced (e.g: products, regions, orders, etc)
|
||||
* @return {Promise<{object}>} - returns response from search engine provider
|
||||
*/
|
||||
replaceDocuments(indexName, documents) {
|
||||
replaceDocuments(indexName, documents, type) {
|
||||
throw Error("updateDocument must be overridden by a child class")
|
||||
}
|
||||
|
||||
@@ -88,15 +90,6 @@ 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,5 +1,6 @@
|
||||
import { SearchService } from "medusa-interfaces"
|
||||
import { MeiliSearch } from "meilisearch"
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
import { transformProduct } from "../utils/transform-product"
|
||||
|
||||
class MeiliSearchService extends SearchService {
|
||||
@@ -19,12 +20,14 @@ class MeiliSearchService extends SearchService {
|
||||
return this.client_.index(indexName)
|
||||
}
|
||||
|
||||
addDocuments(indexName, documents) {
|
||||
return this.client_.index(indexName).addDocuments(documents)
|
||||
addDocuments(indexName, documents, type) {
|
||||
const transformedDocuments = this.getTransformedDocuments(type, documents)
|
||||
return this.client_.index(indexName).addDocuments(transformedDocuments)
|
||||
}
|
||||
|
||||
replaceDocuments(indexName, documents) {
|
||||
return this.client_.index(indexName).addDocuments(documents)
|
||||
replaceDocuments(indexName, documents, type) {
|
||||
const transformedDocuments = this.getTransformedDocuments(type, documents)
|
||||
return this.client_.index(indexName).addDocuments(transformedDocuments)
|
||||
}
|
||||
|
||||
deleteDocument(indexName, document_id) {
|
||||
@@ -43,6 +46,15 @@ class MeiliSearchService extends SearchService {
|
||||
return this.client_.index(indexName).updateSettings(settings)
|
||||
}
|
||||
|
||||
getTransformedDocuments(type, documents) {
|
||||
switch (type) {
|
||||
case indexTypes.products:
|
||||
return this.transformProducts(documents)
|
||||
default:
|
||||
return documents
|
||||
}
|
||||
}
|
||||
|
||||
transformProducts(products) {
|
||||
if (!products) return []
|
||||
return products.map(transformProduct)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
import { transformProduct } from "../utils/transform-product"
|
||||
|
||||
class ProductSearchSubscriber {
|
||||
@@ -37,9 +38,11 @@ class ProductSearchSubscriber {
|
||||
|
||||
handleProductCreation = async (data) => {
|
||||
const product = await this.retrieveProduct_(data.id)
|
||||
await this.meilisearchService_.addDocuments(this.productIndexName, [
|
||||
product,
|
||||
])
|
||||
await this.meilisearchService_.addDocuments(
|
||||
this.productIndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
|
||||
retrieveProduct_ = async (product_id) => {
|
||||
@@ -76,9 +79,11 @@ class ProductSearchSubscriber {
|
||||
|
||||
handleProductUpdate = async (data) => {
|
||||
const product = await this.retrieveProduct_(data.id)
|
||||
await this.meilisearchService_.addDocuments(this.productIndexName, [
|
||||
product,
|
||||
])
|
||||
await this.meilisearchService_.addDocuments(
|
||||
this.productIndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
|
||||
handleProductDeletion = async (data) => {
|
||||
@@ -90,9 +95,11 @@ class ProductSearchSubscriber {
|
||||
|
||||
handleProductVariantChange = async (data) => {
|
||||
const product = await this.retrieveProduct_(data.product_id)
|
||||
await this.meilisearchService_.addDocuments(this.productIndexName, [
|
||||
product,
|
||||
])
|
||||
await this.meilisearchService_.addDocuments(
|
||||
this.productIndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import ProductService from "../services/product"
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
|
||||
async function loadProductsIntoSearchEngine(container) {
|
||||
const searchService = container.resolve("searchService")
|
||||
@@ -43,9 +44,11 @@ async function loadProductsIntoSearchEngine(container) {
|
||||
}
|
||||
)
|
||||
|
||||
const transformedBatch = searchService.transformProducts(products)
|
||||
|
||||
await searchService.addDocuments(ProductService.IndexName, transformedBatch)
|
||||
await searchService.addDocuments(
|
||||
ProductService.IndexName,
|
||||
products,
|
||||
indexTypes.products
|
||||
)
|
||||
|
||||
iterCount += products.length
|
||||
lastSeenId = products[products.length - 1].id
|
||||
|
||||
@@ -25,13 +25,13 @@ class DefaultSearchService extends SearchService {
|
||||
)
|
||||
}
|
||||
|
||||
addDocuments(indexName, documents) {
|
||||
addDocuments(indexName, documents, type) {
|
||||
this.logger_.warn(
|
||||
"This is an empty method: addDocuments must be overridden by a child class"
|
||||
)
|
||||
}
|
||||
|
||||
replaceDocuments(indexName, documents) {
|
||||
replaceDocuments(indexName, documents, type) {
|
||||
this.logger_.warn(
|
||||
"This is an empty method: replaceDocuments must be overridden by a child class"
|
||||
)
|
||||
@@ -61,13 +61,6 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user