How to query algolia data with in the array of object
Asked Answered
G

1

6

I have a data array with multiple objects in Algolia index as below.

[
{
status:available
startDate: 2000-10-20
endDate:2022-10-20
availablePlatform:[1,2,3]
availableCountry:908,

},
{
status:available,
startDate: 2023-10-20
endDate:2123-10-20
availablePlatform:[4,5,6,7]
AvailableCountry:[144],
},

]

I need to perform a filtering which should do a exact match within these objects.

Ex:

(1) avails.status:available AND avails.availablePlatform:2 AND avails.availableCountry:908

This returns true as expected.

(2) avails.status:available AND avails.availablePlatform:2 AND avails.availableCountry:144

This one also returns true as Algolia has matched availableCountry from the second object. But i need to return false in this scenario. Does such a capability exist?, or are there other ways of approaching this problem that folks could point me to?

Same question asked here as well https://discourse.algolia.com/t/filtering-which-should-do-a-exact-match-within-data-objects-in-an-array/16677

Thanks.

Goalie answered 9/11, 2022 at 21:24 Comment(3)
discourse.algolia.com/t/…Vine
a solution is to have a record for each element in the avails array. downside is it will increase the no of recordsVine
yes @cmgchess, to avoid this downside, we can have something like this https://mcmap.net/q/1917411/-algolia-filters-on-array-property-not-working-as-expected. Maybe not ideal in some casesStucco
V
1

According to this post on the Algolia discourse you will have to index a separate record for each element in the avails array and that this is the only way.

assuming your entry looks something like this

[{
    objectID: 'someid',
    somefield: 'somevalue',
    avails: [{
            status: 'available'
            startDate: '2000 - 10 - 20'
            endDate: '2022 - 10 - 20'
            availablePlatform: [1, 2, 3]
            availableCountry: 908,

        },
        {
            status: 'available',
            startDate: '2023 - 10 - 20'
            endDate: '2123 - 10 - 20'
            availablePlatform: [4, 5, 6, 7]
            availableCountry: [144],
        },

    ]
}]

you might have to break your entry to subentries on the index for each entry in the avails array. The downside of this is that now you will have increased number of records and redundant data for each element in the avails array

[{
        objectID: 'someid-1',
        somefield: 'somevalue',
        avails: {
            status: 'available'
            startDate: '2000 - 10 - 20'
            endDate: '2022 - 10 - 20'
            availablePlatform: [1, 2, 3]
            availableCountry: 908,

        }


    },
    {
        objectID: 'someid-2',
        somefield: 'somevalue',
        avails: {
            status: 'available',
            startDate: '2023 - 10 - 20'
            endDate: '2123 - 10 - 20'
            availablePlatform: [4, 5, 6, 7]
            availableCountry: [144],
        }
    }
]
Vine answered 22/11, 2022 at 15:21 Comment(3)
This example has one attribute that is an array of objects. What if my record has multiple attributes that are arrays of objects? And I'd like to include all attributes in the query? This example has one attribute with 2 objects in the array. If I have 2 more attributes, with 3 and 4 items each in the array, would that mean that I would have to flatten and create 24 (4*3*2) records now?Malinowski
@Malinowski did you find a workaround for this. cant think of a good oneVine
I reached out to Algolia support and you are correct. This is indeed the ONLY way to proceed. The one proposed by @Ghayoor ul Haq seems to be fine if you are having few items in your array but otherwise, having 1 record for each child (for each facet that is an array) is the only option. Yes, it results in lots of records and increases as the number of facets you need to work with increase, but that seems to be normal for Algolia.Malinowski

© 2022 - 2024 — McMap. All rights reserved.