medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.25k stars 2.67k forks source link

Meilisearch Plugin: Updated products omits categories in transformer function #5243

Closed hyenabyte closed 5 months ago

hyenabyte commented 1 year ago

Bug report

Describe the bug

I have a transformer function set up to do some basic mapping of data, looks like this:

{
  transformer: (product) => {
    return {
      id: product.id,
      title: product.title,
      handle: product.handle,
      thumbnail: product.thumbnail,
      created_at: product.created_at,
      collection: product.collection.handle,
      price: product.variants[0]?.prices[0]?.amount ?? 0,

      categories: product.categories,
      variants: product.variants,
    };
  },
}

I noticed that categories would go missing from the indexed data once I updated the product, and I figured out if I logged out product categories would be included at the first indexing on server start, but when the transform was run again on product update categories are no longer included.

System information

Steps to reproduce the behavior

  1. Create a product and assign it one or more categories
  2. Restart the server and have the product indexed
  3. Edit the product, i.e. change its name
  4. Observe the missing categories in meilisearch for the updated product

Expected behavior

The data should not differ between create and update in the transform function, or alternatively there should be a way to easily discern between create, update, delete and so on within the function.

Additional context

The entire config for the plugin:

  {
    resolve: `medusa-plugin-meilisearch`,
    options: {
      config: {
        host: process.env.MEILISEARCH_HOST ?? 'localhost:7700',
        apiKey: process.env.MEILISEARCH_API_KEY ?? '',
      },
      settings: {
        products: {
          indexSettings: {
            searchableAttributes: ['title', 'description', 'variant_sku', 'categories'],
            displayedAttributes: [
              'id',
              'title',
              'description',
              'variant_sku',
              'thumbnail',
              'handle',
              'categories',
              'variants',
              'variantsActual',
              'collection',
              'price',
            ],
            sortableAttributes: ['title', 'created_at', 'price'],
            filterableAttributes: ['categories', 'collection', 'variants'],
          },
          primaryKey: 'id',
          transformer: (product) => {
            // console.log('Transformer run for product: ', product.id);
            // console.log(product);
            return {
              id: product.id,
              title: product.title,
              handle: product.handle,
              thumbnail: product.thumbnail,
              created_at: product.created_at,
              collection: product.collection.handle,

              price: product.variants[0]?.prices[0]?.amount ?? 0,

              categories: product.categories,

              variants: product.variants,
            };
          },
        },
      },
    },
  },
nfzv commented 9 months ago

Hey, having the same issue, but it took me a month to realize/debug that categories went indeed missing on updates. Any progress on that?

nfzv commented 9 months ago

This PR probably resolves the issue: #6555

nfzv commented 9 months ago

Patched the PR changes #6555 locally, can confirm it fixes this issue.

sradevski commented 5 months ago

Hey, thanks for the report! Since v2 brought a lot of architectural and API changes on the backend, we will be closing this ticket since it no longer applies to our new setup, or the issue has already been fixed. If you are still facing issues with v1, please open a new ticket and we will address it as soon as possible. Thanks! 🙏