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.

cmgchess
  • 7,996
  • 37
  • 44
  • 62
Piusha
  • 594
  • 5
  • 11
  • https://discourse.algolia.com/t/filtering-with-a-property-that-is-an-array-of-objects/4693/4 – cmgchess Nov 10 '22 at 13:49
  • a solution is to have a record for each element in the avails array. downside is it will increase the no of records – cmgchess Nov 10 '22 at 13:50
  • yes @cmgchess, to avoid this downside, we can have something like this https://stackoverflow.com/a/75350079/9372104. Maybe not ideal in some cases – Ghayoor ul Haq Feb 05 '23 at 05:36

1 Answers1

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],
        }
    }
]
cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • 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? – callmekatootie Mar 02 '23 at 12:34
  • @callmekatootie did you find a workaround for this. cant think of a good one – cmgchess Mar 20 '23 at 18:44
  • 1
    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. – callmekatootie Mar 24 '23 at 02:49