installing algolia plugin in medusajs backend store, gives an error

61 Views Asked by At

i am trying to install algolia search in medusajs by using the documentation on this link https://docs.medusajs.com/plugins/search/algolia

i installed the plugin by the follwoing command npm install medusa-plugin-algolia entered the api keys in .env

below is the config , i am using (as given in the documentation )

  resolve: `medusa-plugin-algolia`,
    options: {
      applicationId: process.env.ALGOLIA_APP_ID,
      adminApiKey: process.env.ALGOLIA_ADMIN_API_KEY,
      settings: {
        products: {
          indexSettings: {
          searchableAttributes: ["title", "description"],
          attributesToRetrieve: [
            "id",
            "title",
            "description",
            "handle",
            "thumbnail",
            "variants",
            "variant_sku",
            "options",
            "collection_title",
            "collection_handle",
            "images",
          ],
          },
        },
      },
    },
  },
];

but i am getting the below error in the terminal on running - npx medusa develop

info: Processing SEARCH_INDEX_EVENT which has 1 subscribers error: An error occurred while processing SEARCH_INDEX_EVENT: [object Object]

Even on algolia, i am not getting the records, as i should get the index is getting created "products", but the records are not being uploaded to algolia by the api algolia no records image

Also on postman while verifying the plugin, i am not getting the desired result, it is empty

postman result

kindly let me what config should be used in the plugin, so that the api is able to upload all the records to algolia

3

There are 3 best solutions below

0
andychukse On

It appears your indexer is not able to parse the object properly.

transformer: 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.

const plugins = [
  {
    resolve: `medusa-plugin-algolia`,
    options: {
      applicationId: process.env.ALGOLIA_APP_ID,
      adminApiKey: process.env.ALGOLIA_ADMIN_API_KEY,
      settings: {
        indexName: {
          indexSettings: {
            searchableAttributes: ["title", "description"],
            attributesToRetrieve: [
              "id",
              "title",
              "description",
              "handle",
              "thumbnail",
              "variants",
              "variant_sku",
              "options",
              "collection_title",
              "collection_handle",
              "images",
            ],
          },
        transformer: (product) => ({ 
          objectID: product.id, 
          // other attributes...
        }),
      },
    },
    },
  },
]
0
blueflyer On

i had tried earlier with transformer, but it did not work out.

But now the issue is resolved . i checked the - Search API Logs, in algolia. checked the response. below was the response error

{
  "message": "Record at the position 3 objectID=prod_01HR0QN8A82WKCNS1RWE4P8QPN is too big size=15219/10000 bytes. Please have a look at https://www.algolia.com/doc/guides/sending-and-managing-data/prepare-your-data/in-depth/index-and-records-size-and-usage-limitations/#record-size-limits",
  "position": 3,
  "objectID": "prod_01HR0QN8A82WKCNS1RWE4P8QPN",
  "status": 400
}

checked in the database, for the particular entry, it was having 8 variants, so the size of this entry was greater than other, reduced it to 4 variants . since algolia was a free subscription, it has limits on record size limits.

so algolia was not allowing the records to be imported.

so after the size of that particular record was reduced, algolia allowed the import of all the records on its portal and search was active.

1
Zeeshan Mehdi On

I resolved the issue just by changing the transformer with adding return like this:

 transformer: (item) => {
          // Extract titles and descriptions directly from item.metadata
          return {
            objectID: item.id,
            title: item.title,
            handle: item.handle,
            thumbnail: item.thumbnail,
            subtitle: item.subtitle,
            tags: item.tags,
            description: item.description,
            material: item.material,
            metadata: item.metadata, // Keep the original metadata as well if needed
            collection_title: item.collection ? item.collection.title : "", // Adjusted to avoid potential undefined access
            collection_handle: item.collection ? item.collection.handle : "", // Adjusted to avoid potential undefined access
          };
        },