Code Monkey home page Code Monkey logo

medusa-plugin-elasticsearch's Introduction

Medusa Elasticsearch Search Plugin

NPM Version Contributors Twitter Activity Issues

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


How to Install

1. Run the following command in the directory of the Medusa backend:

npm install medusa-plugin-elasticsearch

2. Set the environment variables in .env, based on your configuration.

3. In medusa-config.js add the following at the end of the plugins array:

const 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 needed
          properties: {
            id: {
              type: 'keyword',
            },
            name: {
              type: 'keyword',
            },
            description: {
              type: 'text',
            },
          },
        }
        settings: { // Not required, used in case if custom index settings are needed
          analysis: {
            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
config The Elasticsearch client configuration. true
settings The indexes list and configurations. true

2. Index Options

Name Description Required
indexSettings Standard Medusa index settings. true
transformer Custom object transformer function. false
mappings Custom Elasticsearch mapping configuration. false
settings Custom Elasticsearch index configuration. false

Test the Plugin

1. Run the following command in the directory of the Medusa backend to run the backend:

npm run start

2. Try searching products either using your storefront or using the Store APIs.

3. Example search response:

{
  "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."
        }
      }
    ]
  }
}

Additional Resources

Contributing

If you want to contribute to this project, please feel free to open a pull request or an issue. I will try to respond as soon as possible.

medusa-plugin-elasticsearch's People

Contributors

peterborodatyy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

duckheada amo1101

medusa-plugin-elasticsearch's Issues

Issue when testing the plugin with elasticsearch running on host

Hi,

I tried the plugin following the guide, but seems some error occurs when starting medusa backend, index cannot be created:
...
info: Processing SEARCH_INDEX_EVENT which has 1 subscribers
error: An error occurred while processing SEARCH_INDEX_EVENT: TypeError: Cannot read properties of undefined (reading 'PRODUCTS')
...

the plugin configuration is the same with the guide but only differs for the config part:
resolve: medusa-plugin-elasticsearch,
options: {
config: {
node: process.env.ELASTICSEARCH_HOST << http://localhost:9200 from .env
},

elasticsearch version: 7.10.0
medusa version: latest

The medusa backend and the default storefront end are running normally, and there are seeded products.

Can you help to check what might be the reason?

Is this working?

Hi! thank you for the contribution.

Is this repo being maintained? what's the status of the plugin? Is there a reason why it's not shown on medusa?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.