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