I'm facing problem in implementing algolia browseObjects?

I’m implementing algoliaSearch for listing products under categories in angular ssr.
There are two problems while using bowseObjects:

  1. Products are not filtered on the basis of categoryId using facetFilters in browseObjects
  2. browseObjects fetching unlimited products

My target is to fetch 20 products against each category.

Looking forward for your kind support.

Thanks

Hello,

It sounds like you’re encountering two distinct issues with Algolia’s browseObjects method in your Angular SSR application. Let’s address each one:

Filtering Products by Category ID: To filter products based on categoryId, you should use the facetFilters parameter correctly. The facetFilters should be an array of strings where each string is a facet filter in the format attribute:value. For example, if you want Affordable Care Act to filter by a category with ID 123, your facet filter would be [“categoryId:123”]. Here’s how you can structure your facetFilters:

const facetFilters = ["categoryId:123"]; // Replace 123 with your actual category ID

Ensure that categoryId is set as an attribute for faceting in your Algolia dashboard or through API settings.

Limiting the Number of Fetched Products: The browseObjects method is designed to fetch all objects from an index, which is why you’re seeing unlimited products being fetched. To limit the number of products fetched to 20 per category, you’ll need to implement pagination logic on your end. Unfortunately, browseObjects does not support a direct limit parameter. However, you can control the number of items you process from the response of browseObjects. Here’s a conceptual example of how you might approach it:

let count = 0;
const maxProducts = 20; // Maximum number of products you want to fetch

index.browseObjects({
  query: '', // Use an empty string for query to fetch all objects
  batch: (batch) => {
    batch.forEach((hit) => {
      if (count < maxProducts) {
        // Process the hit
        console.log(hit);
        count++;
      }
    });
  }
});

In this example, batch is a callback function that processes each batch of results. You can keep a count of how many products you’ve processed and stop once you reach 20.