Files
medusa-store/www/docs/content/development/search/create.md
Shahed Nasser 914d773d3a api-ref: custom API reference (#4770)
* initialized next.js project

* finished markdown sections

* added operation schema component

* change page metadata

* eslint fixes

* fixes related to deployment

* added response schema

* resolve max stack issue

* support for different property types

* added support for property types

* added loading for components

* added more loading

* type fixes

* added oneOf type

* removed console

* fix replace with push

* refactored everything

* use static content for description

* fixes and improvements

* added code examples section

* fix path name

* optimizations

* fixed tag navigation

* add support for admin and store references

* general enhancements

* optimizations and fixes

* fixes and enhancements

* added search bar

* loading enhancements

* added loading

* added code blocks

* added margin top

* add empty response text

* fixed oneOf parameters

* added path and query parameters

* general fixes

* added base path env variable

* small fix for arrays

* enhancements

* design enhancements

* general enhancements

* fix isRequired

* added enum values

* enhancements

* general fixes

* general fixes

* changed oas generation script

* additions to the introduction section

* added copy button for code + other enhancements

* fix response code block

* fix metadata

* formatted store introduction

* move sidebar logic to Tags component

* added test env variables

* fix code block bug

* added loading animation

* added expand param + loading

* enhance operation loading

* made responsive + improvements

* added loading provider

* fixed loading

* adjustments for small devices

* added sidebar label for endpoints

* added feedback component

* fixed analytics

* general fixes

* listen to scroll for other headings

* added sample env file

* update api ref files + support new fields

* fix for external docs link

* added new sections

* fix last item in sidebar not showing

* move docs content to www/docs

* change redirect url

* revert change

* resolve build errors

* configure rewrites

* changed to environment variable url

* revert changing environment variable name

* add environment variable for API path

* fix links

* fix tailwind settings

* remove vercel file

* reconfigured api route

* move api page under api

* fix page metadata

* fix external link in navigation bar

* update api spec

* updated api specs

* fixed google lint error

* add max-height on request samples

* add padding before loading

* fix for one of name

* fix undefined types

* general fixes

* remove response schema example

* redesigned navigation bar

* redesigned sidebar

* fixed up paddings

* added feedback component + report issue

* fixed up typography, padding, and general styling

* redesigned code blocks

* optimization

* added error timeout

* fixes

* added indexing with algolia + fixes

* fix errors with algolia script

* redesign operation sections

* fix heading scroll

* design fixes

* fix padding

* fix padding + scroll issues

* fix scroll issues

* improve scroll performance

* fixes for safari

* optimization and fixes

* fixes to docs + details animation

* padding fixes for code block

* added tab animation

* fixed incorrect link

* added selection styling

* fix lint errors

* redesigned details component

* added detailed feedback form

* api reference fixes

* fix tabs

* upgrade + fixes

* updated documentation links

* optimizations to sidebar items

* fix spacing in sidebar item

* optimizations and fixes

* fix endpoint path styling

* remove margin

* final fixes

* change margin on small devices

* generated OAS

* fixes for mobile

* added feedback modal

* optimize dark mode button

* fixed color mode useeffect

* minimize dom size

* use new style system

* radius and spacing design system

* design fixes

* fix eslint errors

* added meta files

* change cron schedule

* fix docusaurus configurations

* added operating system to feedback data

* change content directory name

* fixes to contribution guidelines

* revert renaming content

* added api-reference to documentation workflow

* fixes for search

* added dark mode + fixes

* oas fixes

* handle bugs

* added code examples for clients

* changed tooltip text

* change authentication to card

* change page title based on selected section

* redesigned mobile navbar

* fix icon colors

* fix key colors

* fix medusa-js installation command

* change external regex in algolia

* change changeset

* fix padding on mobile

* fix hydration error

* update depedencies
2023-08-15 18:07:54 +03:00

14 KiB
Raw Blame History

description, addHowToData
description addHowToData
Learn how to create a search service in Medusa. You can create the search service directly in your Medusa backend codebase, in a plugin, or in a module. true

How to Create a Search Service

In this document, youll learn how to create a search service in Medusa. You can create the search service directly in your Medusa backend codebase, in a plugin, or in a module.

Prerequisites

Medusa Utils Package

A search service must extend the AbstractSearchService class imported from the @medusajs/utils package. If you dont already have the package installed, run the following command to install it within your project:

npm install @medusajs/utils

Step 1: Create Search Service Class

A search service class should be defined 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 files name should be the slug version of the search services name without service, and the classs name should be the pascal case of the search services name following by Service.

For example, if youre creating an algolia search service, the file name would be algolia.ts, whereas the class name would be AlgoliaService.

:::note

You can learn more about services and their naming convention in this documentation.

:::

For example, create the file src/services/my-search.ts with the following content:

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

This creates the service MySearchService which, at the moment, adds a general implementation of the methods defined in the abstract class AbstractSearchService.

Using a Constructor

You can use a constructor to access services and resources registered in the dependency container, to define any necessary clients if youre integrating a third-party storage service, and to access plugin options if your search service is defined in a plugin.

For example:

// ...
import { ProductService } from "@medusajs/medusa"

type InjectedDependencies = {
  productService: ProductService
}

class MySearchService extends AbstractSearchService {
  // ...
  protected readonly productService_: ProductService

  constructor({ productService }: InjectedDependencies) {
    // @ts-expect-error prefer-rest-params
    super(...arguments)
    this.productService_ = productService
  }

  // ...
}

You can access the plugin options in the second parameter passed to the constructor:

// ...

class MySearchService extends AbstractSearchService {
  // ...
  protected readonly pluginOptions: Record<string, any>

  constructor({
    productService,
  }: InjectedDependencies, pluginOptions) {
    // @ts-expect-error prefer-rest-params
    super(...arguments)
    
    // ...
    this.pluginOptions = pluginOptions
  }

  // ...
}

isDefault Property

The isDefault property is mainly used to pinpoint the default search service defined in the Medusa core. For custom search services, the isDefault property should be false.


Step 2: Implement Required Methods

In this section, youll learn about the required methods to implement in the search service.

:::note

The Medusa backend mainly uses the addDocuments, deleteDocument, and search methods in different scenarios that are explained for each of the methods. Other methods can be helpful based on the search engine youre integrating.

:::

createIndex

This method is used to create an index in the search engine.

The method accepts two parameters:

  1. indexName: this is the first parameter, and its a string indicating the name of the index to create.
  2. options: this is the second parameter is typically an object, and it can be used to pass any necessary options to the method. This parameter does not have any defined format.

The method does not require any specific data type to be returned.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  createIndex(indexName: string, options: Record<string, any>) {
    return this.client_.initIndex(indexName)
  }
}

getIndex

This method is used to retrieve an indexs results from the search engine.

The method accepts one parameter, which is a string indicating the name of the index. The method does not require any specific data type to be returned.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  getIndex(indexName: string) {
    return this.client_.getIndex(indexName)
  }
}

addDocuments

This method is used to add a document to an index in the search engine.

This method is used when the Medusa backend loads, indexing all products available in the Medusa backend. Its also used whenever a new product is added or a product is updated.

The method accepts the following parameters:

  • indexName: the first parameter is a string indicating the name of the index to add the document to.
  • documents: the second parameter is typically an array of objects to index. For example, it can be an array of products to index.
  • type: the third parameter is a string indicating the type of object being indexed. For example, when indexing products, the type would be products.

The method should return the response of saving the documents in the search engine, but theres no required format of the response.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async addDocuments(
    indexName: string,
    documents: Record<string, any>[],
    type: string
  ) {
    return await this.client_
      .addDocuments(indexName, documents)
  }
}

replaceDocuments

This method is used to replace the existing documents in the search engine of an index with new documents.

The method accepts the following parameters:

  • indexName: the first parameter is a string indicating the name of the index to replace the documents in.
  • documents: the second parameter is typically an array of objects to index. For example, it can be an array of products to index. This would be the new documents to add to the index.
  • type: the third parameter is a string indicating the type of object being indexed. For example, when indexing products, the type would be products.

The method should return the response of saving the documents in the search engine, but theres no required format of the response.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async replaceDocuments(
    indexName: string,
    documents: Record<string, any>[],
    type: string
  ) {
    await this.client_
      .removeDocuments(indexName)
    return await this.client_
      .addDocuments(indexName, documents)
  }
}

deleteDocument

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 engines index.

The method accepts the following parameters:

  • indexName: the first parameter is a string indicating the name of the index this document belongs in.
  • document_id: the second parameter is a string or a number indicating the ID of the document to delete. When a product is deleted, the products ID is passed as the value of this parameter.

The method should return the response of deleting the document in the search engine, but theres no required format of the response.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async deleteDocument(
    indexName: string,
    document_id: string | number
  ) {
    return await this.client_
      .deleteDocument(indexName, document_id)
  }
}

deleteAllDocuments

This method is used to delete all documents from an index.

The method accepts one parameter, which is a string indicating the name of the index to delete its documents.

The method should return the response of deleting the documents of that index in the search engine, but theres no required format of the response.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async deleteAllDocuments(indexName: string) {
    return await this.client_
      .deleteDocuments(indexName)
  }
}

This method is used to search through an index by a query.

In the Medusa backend, this method is used within the Search Products endpoint to retrieve the search results.

This method accepts the following parameters:

  1. indexName: the first parameter is a string indicating the index to search through. When using the Search Products endpoint, the index is the default index defined in the IndexName static property of the ProductService, which is products.
  2. query: the second parameter is a string indicating the query to use to search through the documents.
  3. options: the third parameter is typically an object that can be used to pass any necessary options to the search engine.

Although theres no required data format or type to be returned to the method, its recommended to return an object having a property hits with its value being an array of results. Each result can be an object of any format. This is recommended as this is the convention followed within Medusas official search plugins.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async search(
    indexName: string,
    query: string,
    options: Record<string, any>
  ) {
    const hits = await this.client_
      .search(indexName, query)
    return {
      hits,
    }
  }
}

updateSettings

This method is used to update the settings of an index within the search engine. This can be 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.

The method accepts the following parameters:

  1. indexName: the first parameter is a string indicating the index that should be updated.
  2. settings: the second parameter is typically an object that holds the settings of the index. Theres no defined format for this parameter.

The method should return the response of updating the index in the search engine, but theres no required format of the response.

An example implementation, assuming client_ would interact with a third-party service:

class MySearchService extends AbstractSearchService {
  // ...

  async updateSettings(
    indexName: string,
    settings: Record<string, any>
  ) {
    return await this.client_
      .updateSettings(indexName, settings)
  }
}

Step 3: Run Build Command

In the directory of the Medusa backend, run the build command to transpile the files in the src directory into the dist directory:

npm run build

Test it Out

:::note

This section explains how to test out your implementation if the search service was created in the Medusa backend codebase. You can refer to the plugin documentation on how to test a plugin.

:::

Run your backend to test it out:

npx medusa develop

You can then send a request to the Search Products endpoint to see if your search service returns any results.


See Also