Algolia filters on array property not working as expected
Asked Answered
F

1

3

I have an index on algolia having users like this

UserA:

{
 createdAt: 1675364400000,
 email: "[email protected]",
 products: [
   {
    productId: 'product1',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'active'
   },
   {
    productId: 'product2',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'pending'
   },
 ]
}

UserB:

{
 createdAt: 1675364400000,
 email: "[email protected]",
 products: [
   {
    productId: 'product4',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'active'
   },
   {
    productId: 'product5',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'pending'
   },
 ]
}

Need to apply filter on algolia to get all the users who has product2 with pending status. I'm getting both UserA (because it has product2 pending) and UserB (because it has product with pending status).

I added products.status and products.productId in attributesForFaceting and used the following in filters products.productId:product2 AND products.status:pending. Its getting all the users having product2 and any products with status pending

Foliose answered 2/2, 2023 at 13:6 Comment(5)
Is this even possible using this structure? Found this on a forum and seems like a need to change the structure of array? discourse.algolia.com/t/…Foliose
the example you have given will not cause the problem since products.productId:product2 AND products.status:pending is only in user A. but you will get it wrong if the products array had something like [{productId: 'product2',status: 'active'},{productId: 'product4',status: 'pending'}]Muncey
in that case you will have to do something like in the forum. also something similar #74382051Muncey
Thanks @Muncey for your comments, I've checked this and it seems there will be a lot of redundant data if we do something like that, to avoid this I've implemented something else, and I'll post the answer soon, I'll appreciate it if you'll give a thought on that.Foliose
Great. i'm curious to know your workaround. Did you combine the 2 fields to 1Muncey
F
1

Changing the structure of the index on algolia helped me to resolve this issue. There are multiple ways to structure the index like mentioned in this but with this approach, can have redundant data and more requests to algolia if we need to update the other fields like createdAt in my case. Here is the structure I used to avoid all these overheads.

{
 createdAt: 1675364400000,
 email: "[email protected]",
 "product1": {
    productId: 'product1',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'active'
  },
 "product2": {
    productId: 'product2',
    dateAdded: 1675364400000,
    dateUpdated: 1675364400000,
    status: 'pending'
  }
}

And in attributesForFaceting added product1.status, product2.status, and the same for all our products. In this way, if we need to update the createdAt on algolia, we must change just one object with no redundant data.

We have a limited number of products that will not change very often. This might have some issues with attributesForFaceting if we have many products and change very often.

I would love to hear suggestions for improvement.

Foliose answered 5/2, 2023 at 4:14 Comment(2)
This looks good considering there is a fixed number of productsMuncey
Another workaround that came to my mind was to combine the two fields. Something like "status":"product1-active". But I'm not sure how clean this solution is considering you will have to change the status field to have product name during indexing and also you might have to restructure the search result so that status has only the value activeMuncey

© 2022 - 2024 — McMap. All rights reserved.