SearchService
Overview
A search service class is in a TypeScript or JavaScript file created in the src/services directory. The class must extend the AbstractSearchService class imported
from the @medusajs/utils package.
Based on services’ naming conventions, the file’s name should be the slug version of the search service’s name without service, and the class’s name should be the
pascal case of the search service’s name following by Service.
For example, create the MySearchService class in the file src/services/my-search.ts:
import { AbstractSearchService } from "@medusajs/utils"
class MySearchService extends AbstractSearchService {
isDefault = false
createIndex(indexName: string, options: Record<string, any>) {
throw new Error("Method not implemented.")
}
getIndex(indexName: string) {
throw new Error("Method not implemented.")
}
addDocuments(
indexName: string,
documents: Record<string, any>[],
type: string
) {
throw new Error("Method not implemented.")
}
replaceDocuments(
indexName: string,
documents: Record<string, any>[],
type: string
) {
throw new Error("Method not implemented.")
}
deleteDocument(
indexName: string,
document_id: string | number
) {
throw new Error("Method not implemented.")
}
deleteAllDocuments(indexName: string) {
throw new Error("Method not implemented.")
}
search(
indexName: string,
query: string,
options: Record<string, any>
) {
return {
message: "test",
}
}
updateSettings(
indexName: string,
settings: Record<string, any>
) {
throw new Error("Method not implemented.")
}
}
export default MySearchService
Notes About Class Methods
Although there are several helper methods in this class, the main methods used by the Medusa backend are addDocuments, deleteDocument, and search.
The rest of the methods are provided in case you need them for custom use cases.
constructor
Parameters
__namedParametersInjectedDependenciesRequiredoptionsanyRequiredProperties
isDefaultbooleanRequiredisDefault property must be false.Default: true
options_Record<string, unknown>RequiredMethods
createIndex
This method is used to create an index in the search engine.
Example
An example implementation, assuming client would interact with a third-party service:
Another example of how the MeiliSearch plugin uses the
options parameter:
Parameters
indexNamestringRequiredoptionsunknownRequiredReturns
PromisePromise<void>RequiredgetIndex
This method is used to retrieve an index’s results from the search engine.
Example
Parameters
indexNamestringRequiredReturns
PromisePromise<void>RequiredaddDocuments
This method is used to add a document to an index in the search engine.
When the Medusa backend loads, it triggers indexing for all products available in the Medusa backend, which uses this method to add or update documents. It’s also used whenever a new product is added or a product is updated.
Example
Parameters
indexNamestringRequireddocumentsunknownRequiredtypestringRequiredproducts.Returns
PromisePromise<void>RequiredreplaceDocuments
This method is used to replace existing documents in the search engine of an index with new documents.
Example
Parameters
indexNamestringRequireddocumentsunknownRequiredtypestringRequiredproducts.Returns
PromisePromise<void>RequireddeleteDocument
This method is used to delete a document from an index.
When a product is deleted in the Medusa backend, this method is used to delete the product from the search engine’s index.
Example
Parameters
indexNamestringRequireddocument_idstring | numberRequiredReturns
PromisePromise<void>RequireddeleteAllDocuments
This method is used to delete all documents from an index.
Example
Parameters
indexNamestringRequiredReturns
PromisePromise<void>Requiredsearch
This method is used to search through an index by a query.
In the Medusa backend, this method is used within the Search Products API Route to retrieve the search results. The API route's response type is an array of items, though the item's format is not defined as it depends on the data returned by this method.
Example
Parameters
indexNamestringRequiredproducts.queryunknownRequiredoptionsunknownRequiredpaginationOptions: An object having anoffsetandlimitproperties, which are passed in the API Route's body.filter: Filters that are passed in the API Route's request body. Its format is unknown, so you can pass filters based on your search service.additionalOptions: Any other parameters that may be passed in the request's body.
Returns
PromisePromise<object>RequiredupdateSettings
This method is used to update the settings of an index within the search service. This is useful if you want to update the index settings when the plugin options change.
For example, in the Algolia plugin, a loader, which runs when the Medusa backend loads, is used to update the settings of indices based on the plugin options. The loader uses this method to update the settings.
Example
Parameters
indexNamestringRequiredsettingsunknownRequiredReturns
PromisePromise<void>Required