How can I filter values on a repeated field?
Problem
Using filter values on a repeated field returns the following error:
filters: field filter: invalid type repeatedString for single value
or
error creating request: set-filter: expected string not "]"
Solution
The syntax of filter expression for a repeated field (e.g. List of Strings) is slightly different than the syntax for a non-repeated field.
To resolve this error, you need to change the syntax from 'value'
to ['value']
.
An example query is mentioned below:
{
"q": "",
"filter": "headings = ['Overview']"
"fields": "headings"
}
You can specify more than a few values separated by a comma, e.g., ['Heading 1','Heading2']
(order is important in this case).
If the order is not important and you want to return if the values are present anywhere in the list, you can use a contains (~) operator instead. Specify a few values to match (e.g. "filter": "headings ~ ['Conclusion','Introduction','Heading 2']"
and any records that contain these three headings will be returned.
You can also look for results where the lists are empty by using "filter": "headings = ['']"
. However, note that this is not the same as searching for results where the field value is null. To search for results where the field value is null, use: filter": "IS_NULL(headings)"
.
Additional Note:
If you are using the React SDK, you need to update the FilterBuilder for the field and include the array: true
in the integration code.
Here is an example:
const categoryFilter = new FilterBuilder({
name: 'categories',
field: 'category',
array: true,
});