Algolia filter equivalent to SQL IN for arrays?
Asked Answered
C

3

9

I've got records in Algolia containing an array attribute with integer values, like :

{
  ...
  choice_ids: [1, 99, 100, 200]
  ...
}

I want to filter all records containing any value of another array. For example I search for [1, 300, 400, 600, 700], I should get the record on top because it contains 1.

Should I construct the filter with OR arguments or is there a better way?

Clarkia answered 27/8, 2016 at 22:59 Comment(0)
D
13

Should I construct the filter with OR arguments or is there a better way?

Yes that's the way to go.

index.search('....', { filters: '(choice_ids=1 OR choice_ids=99 OR choice_ids=100 OR choice_ids=200)' });
Dedrick answered 28/8, 2016 at 9:32 Comment(3)
Still this the best way ? No other solutions ?Mcfarlin
Yes, still the best way.Dedrick
Is there a limit on total number of values we pass in filters?Bilbao
C
1

For me it wasn't working with '=' but with ':', meaning:

{ filters: 'choice_ids:1 OR choice_ids:99 OR choice_ids:100 OR choice_ids:200' })
Carmine answered 6/11, 2018 at 10:10 Comment(0)
C
0

For me, neither of @Léo Chaz Maltrait or @redox answers worked. I had to format mine like:

{ filters: '(choice_ids:"1") OR (choice_ids:"99" OR (choice_ids:"100") OR (choice_ids:"200"))' })

I am also using algoliasearch npm package.

Cornstarch answered 16/5, 2022 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.