#Transformer function in Meilisearch is not working!

9 messages ยท Page 1 of 1 (latest)

carmine shuttle
#

As per the documentation, the transformer function give you more control over what to be indexed.

documentation
๐Ÿ‘‡
The transformer function in the MeiliSearch settings is an optional function that accepts a product as a parameter and returns an object to be indexed. This allows you to have more control over what you're indexing. For example, you can add details related to variants or custom relations, or you can filter out certain products.

here is my transformer function to send created_at as timestamp, but it is not working. Actually the transformer function does not even run, which I have checked it through the console log.

settings: {
  campaigns: {
    indexSettings: {
      searchableAttributes: [
          "title",
          "description",
          "handle",
        ],
        displayedAttributes: [
          "title",
          "description",
          "id",
          "images",
          "handle",
          "campaign_target",
          "deadline",
          "is_active",
          "organizers",
          "raised_amount",
          "created_at",
        ],
          filterableAttributes: ["created_at", "deadline"],
       },
       primaryKey: "id",
       transformer: (campaign) => (
          {
            created_at: new Date(campaign.created_at).getTime(),
            deadline: new Date(campaign.deadline).getTime(),
          }
        )
    },
}

I 've gone through each step of setting the plugin up, and there was nothing I was missing, and here is my code for plugin setting.

For the moment for I have just found a way around it, I am transforming data just before it is sent to Meilisearch. Here is the code for that

#
....
await this.searchService.addDocuments(
            CampaignService.IndexName,
            [
                {
                    ...campaign,
                    created_at: new Date(campaign.created_at).getTime(),
                    deadline: new Date(campaign.deadline).getTime(),
                },
            ],
            CampaignService.IndexName
        );
....

Now, the main question is, isn't this meant to be done through transformer in medusa-config file for each index. Say, the other day I would like to have a filterable fields for product to filter them by their creation time, and I would like to have the created_at field as timestamp (just because time must be in timestamp to be filterable). Should I change the core medusa subscriber for products? ๐Ÿ˜•

Or, there is a way for this to be done in transformer function.

chilly pike
#

@carmine shuttle Did you get transformer working?

carmine shuttle
#

@chilly pike it is been a long time since I posted this. Back then the transformer was not working so I did it while sending document to mellisearch (the last code snippet), and it was waorking well. So, I didnt do anything else, nor I tried transformer again since then.

chilly pike
#

@carmine shuttle Thank you. I was trying to get transformer to work, so I searched about it. I've got it working now. ๐Ÿ™‚ (BTW, I think you should've returned the "id" key in your transformer function in order to get it to work.)

carmine shuttle
#

Your welcome.
Thanks for the feedback, I will try it again, and check if it works.

toxic juniper
#

Hi @carmine shuttle @chilly pike I have opened a pull request to update Medusa docs.

Here is working config:


 {
    resolve: `medusa-plugin-meilisearch`,
    options: {
      config: {
        host: process.env.MEILISEARCH_HOST,
        apiKey: process.env.MEILISEARCH_API_KEY,
      },
      settings: {
        products: {
          indexSettings: {
            searchableAttributes: ["title", "description", "variant_sku"],
            displayedAttributes: [
              "title",
              "description",
              "variant_sku",
              "thumbnail",
              "handle",
            ],
          },
          primaryKey: "id",
          transformer: (product) => ({
            id: product.id,
            title: product.title,
            description: product.description,
            variant_sku: product.variant_sku,
            thumbnail: product.thumbnail,
            handle: product.handle,
            // include other attributes as needed
          }),
        },
      },
    },
  },

chilly pike
#

@toxic juniper Thanks. Where does the variant_sku comes from? I see that in many examples, but in my store the sku is inside the variants array.

toxic juniper