Elasticsearch
Provide powerful indexing and searching features in your commerce application with Elasticsearch.
Features
- Flexible configurations for specifying searchable and retrievable attributes.
- Utilize Elasticsearch's powerful search functionalities including possibility to configure custom mappings, indexes, tokenizers and more.
Prerequisites
- Medusa backend
- Elasticsearch instance or cloud
How to Install
1. Run the following command in the directory of the Medusa backend:
1npm install medusa-plugin-elasticsearch
2. Set the environment variables in Copy to clipboard
.env
, based on your configuration.3. In Copy to clipboard
medusa-config.js
add the following at the end of the Copy to clipboardplugins
array:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980const plugins = [// ...{resolve: `medusa-plugin-elasticsearch`,options: {config: {cloud: {id: process.env.ELASTIC_CLOUD_ID},auth: {username: process.env.ELASTIC_USER_NAME,password: process.env.ELASTIC_PASSWORD,},},settings: {products: {indexSettings: {searchableAttributes: ["title", "description"],attributesToRetrieve: ["id","title","description","handle","thumbnail","variants","variant_sku","options","collection_title","collection_handle","images",],},transformer: (product) => ({id: product.id,name: product.name,description: product.description,// other attributes...}),mappings: { // Not required, used in case if custom mapping configuration is neededproperties: {id: {type: 'keyword',},name: {type: 'keyword',},description: {type: 'text',},},}settings: { // Not required, used in case if custom index settings are neededanalysis: {normalizer: {sortable: {type: 'lowercase',},},tokenizer: {autocomplete: {type: 'edge_ngram',min_gram: 2,max_gram: 10,token_chars: ['letter', 'digit'],},},analyzer: {autocomplete_index: {type: 'custom',tokenizer: 'autocomplete',filter: ['lowercase'],},},},},},},},},]
Options
1. Global options
Name | Description | Required |
---|---|---|
Copy to clipboardconfig | The Elasticsearch client configuration. | true |
Copy to clipboardsettings | The indexes list and configurations. | true |
2. Index Options
Name | Description | Required |
---|---|---|
Copy to clipboardindexSettings | Standard Medusa index settings. | true |
Copy to clipboardtransformer | Custom object transformer function. | false |
Copy to clipboardmappings | Custom Elasticsearch mapping configuration. | false |
Copy to clipboardsettings | Custom Elasticsearch index configuration. | false |
Test the Plugin
1. Run the following command in the directory of the Medusa backend to run the backend:
1npm run start
2. Try searching products either using your storefront or using the Store APIs.
3. Example search response:
1234567891011121314151617181920212223242526272829{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 0.06801665,"hits": [{"_index": "products","_id": "prod_01H2P51GTXD6Y4BB4C950VBQYN","_score": 0.06801665,"_source": {"id": "prod_01H2P51GTXD6Y4BB4C950VBQYN","title": "Medusa Sweatshirt","description": "Reimagine the feeling of a classic sweatshirt. With our cotton sweatshirt, everyday essentials no longer have to be ordinary."}}]}}