feat(medusa-plugin-algolia): Revamp Algolia search plugin (#3510)
This commit is contained in:
@@ -15,9 +15,9 @@ export default async (
|
||||
const { settings } = options
|
||||
|
||||
await Promise.all(
|
||||
Object.entries(settings ?? []).map(([indexName, value]) =>
|
||||
meilisearchService.updateSettings(indexName, value)
|
||||
)
|
||||
Object.entries(settings || {}).map(async ([indexName, value]) => {
|
||||
return await meilisearchService.updateSettings(indexName, value)
|
||||
})
|
||||
)
|
||||
} catch (err) {
|
||||
// ignore
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { AbstractSearchService } from "@medusajs/medusa"
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
import { SearchTypes } from "@medusajs/types"
|
||||
import { SearchUtils } from "@medusajs/utils"
|
||||
import { MeiliSearch, Settings } from "meilisearch"
|
||||
import { IndexSettings, meilisearchErrorCodes, MeilisearchPluginOptions } from "../types"
|
||||
import { transformProduct } from "../utils/transform-product"
|
||||
import { meilisearchErrorCodes, MeilisearchPluginOptions } from "../types"
|
||||
import { transformProduct } from "../utils/transformer"
|
||||
|
||||
class MeiliSearchService extends AbstractSearchService {
|
||||
class MeiliSearchService extends SearchUtils.AbstractSearchService {
|
||||
isDefault = false
|
||||
|
||||
protected readonly config_: MeilisearchPluginOptions
|
||||
@@ -77,21 +77,17 @@ class MeiliSearchService extends AbstractSearchService {
|
||||
|
||||
async updateSettings(
|
||||
indexName: string,
|
||||
settings: IndexSettings | Record<string, unknown>
|
||||
settings: SearchTypes.IndexSettings & Settings
|
||||
) {
|
||||
// backward compatibility
|
||||
if (!("indexSettings" in settings)) {
|
||||
settings = { indexSettings: settings }
|
||||
}
|
||||
const indexSettings = settings.indexSettings ?? settings ?? {}
|
||||
|
||||
await this.upsertIndex(indexName, settings as IndexSettings)
|
||||
await this.upsertIndex(indexName, settings)
|
||||
|
||||
return await this.client_
|
||||
.index(indexName)
|
||||
.updateSettings(settings.indexSettings as Settings)
|
||||
return await this.client_.index(indexName).updateSettings(indexSettings)
|
||||
}
|
||||
|
||||
async upsertIndex(indexName: string, settings: IndexSettings) {
|
||||
async upsertIndex(indexName: string, settings: SearchTypes.IndexSettings) {
|
||||
try {
|
||||
await this.client_.getIndex(indexName)
|
||||
} catch (error) {
|
||||
@@ -104,15 +100,15 @@ class MeiliSearchService extends AbstractSearchService {
|
||||
}
|
||||
|
||||
getTransformedDocuments(type: string, documents: any[]) {
|
||||
switch (type) {
|
||||
case indexTypes.products:
|
||||
if (!documents?.length) {
|
||||
return []
|
||||
}
|
||||
if (!documents?.length) {
|
||||
return []
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case SearchTypes.indexTypes.PRODUCTS:
|
||||
const productsTransformer =
|
||||
this.config_.settings?.[indexTypes.products]?.transformer ??
|
||||
transformProduct
|
||||
this.config_.settings?.[SearchTypes.indexTypes.PRODUCTS]
|
||||
?.transformer ?? transformProduct
|
||||
|
||||
return documents.map(productsTransformer)
|
||||
default:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Config, Settings } from "meilisearch"
|
||||
import { SearchTypes } from "@medusajs/types"
|
||||
import { Config } from "meilisearch"
|
||||
|
||||
export const meilisearchErrorCodes = {
|
||||
INDEX_NOT_FOUND: "index_not_found",
|
||||
@@ -13,21 +14,6 @@ export interface MeilisearchPluginOptions {
|
||||
* Index settings
|
||||
*/
|
||||
settings?: {
|
||||
[key: string]: IndexSettings
|
||||
[key: string]: SearchTypes.IndexSettings
|
||||
}
|
||||
}
|
||||
|
||||
export type IndexSettings = {
|
||||
/**
|
||||
* Settings specific to the provider. E.g. `searchableAttributes`.
|
||||
*/
|
||||
indexSettings: Settings
|
||||
/**
|
||||
* Primary key for the index. Used to enforce unique documents in an index. See more in Meilisearch' https://docs.meilisearch.com/learn/core_concepts/primary_key.html.
|
||||
*/
|
||||
primaryKey?: string
|
||||
/**
|
||||
* Document transformer. Used to transform documents before they are added to the index.
|
||||
*/
|
||||
transformer?: (document: any) => any
|
||||
}
|
||||
}
|
||||
+4
-14
@@ -1,18 +1,8 @@
|
||||
import { Product } from "@medusajs/medusa"
|
||||
|
||||
const variantKeys = [
|
||||
"sku",
|
||||
"title",
|
||||
"upc",
|
||||
"ean",
|
||||
"mid_code",
|
||||
"hs_code",
|
||||
"options",
|
||||
]
|
||||
import { variantKeys } from "@medusajs/types"
|
||||
|
||||
const prefix = `variant`
|
||||
|
||||
export const transformProduct = (product: Product) => {
|
||||
export const transformProduct = (product: any) => {
|
||||
let transformedProduct = { ...product } as Record<string, unknown>
|
||||
|
||||
const initialObj = variantKeys.reduce((obj, key) => {
|
||||
@@ -42,10 +32,10 @@ export const transformProduct = (product: Product) => {
|
||||
transformedProduct.tags_value = product.tags
|
||||
? product.tags.map((t) => t.value)
|
||||
: []
|
||||
transformedProduct.categories = (product?.categories || []).map(c => c.name)
|
||||
transformedProduct.categories = (product?.categories || []).map((c) => c.name)
|
||||
|
||||
const prod = {
|
||||
...product,
|
||||
...transformedProduct,
|
||||
...flattenedVariantFields,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user