foufrix
foufrix

Reputation: 1456

Apply filtering with an array of value instead of one value for a filter in Algolia

I have in my index a list of object, each of them has an objectID value.

On some search, i want to filter OUT a certain number of them, using there objectID.

For the moment it works with one value as a string, i would like to know how to do for multiple value.

filters = 'NOT objectID:' + objectIDToFilter;

This work for one object, what can i do to apply this for an array of ObjectID. because :

filters = 'NOT objectID:' + arrayObjectID;

does not work.

I was thinking of generating a huge string with an arrayId.map with all my 'NOT objectID:1 AND NOT objectID: 2 ...' but i wanted to know if there is a cleaner way to do it.

I unfortunately misunderstood the line in algolia doc :

Array Attributes: Any attribute set up as an array will match the filter as soon as one of the values in the array match.

This apparently refers to the value itself in Algolia and not the filter

Upvotes: 1

Views: 664

Answers (1)

foufrix
foufrix

Reputation: 1456

So i did not found a solution on algolia doc, i went for the long string, hope there is no limits on how much filter we can add on a query (found nothing about that).

Here is what i did if someone need it :

let filters = `NOT objectID:${userID}`; 
blockedByUsers.map((blockedByUser) => {
   filters = filters + ` AND NOT objectID:${blockedByUser}`;
});

If you need to add multiple but don't have a starting point like i do, you can't start the query with an AND , a solution i found to bypass that:

let filters = `NOT objectID:${blockedByUsers[0]}`;
blockedByUsers.map((blockedByUser, i) => {
   if (i > 0) filters = filters + ` AND NOT objectID:${blockedByUser}`;
});

There is probably a cleaner solution, but this work. If you have found other solution for that problems i'll be happy to see :)

Upvotes: 2

Related Questions