Algolia facet filter by empty/null value
Asked Answered
G

2

14

Is there a way to filter hits by a property's value that is either null or an empty string?

i.e., show me all objects that do not have an author

facetFilters=author:null

facetFilters=author:''

Or include it in a list of OR values?

i.e., show me all objects with author set to Twain or that do not have an author

facetFilters=(author:Twain,author:null)

facetFilters=(author:Twain,author:'')

Glyptography answered 11/1, 2016 at 20:36 Comment(0)
A
13

Unfortunately - as of today - Algolia doesn't support that. One work-around could be to push an extra value if it's null, like pushing author: 'unknown' that you could use to filter on.

Apposition answered 11/1, 2016 at 21:37 Comment(0)
M
1

Or, you could add a _tags value at index-time that you can use to filter later on:

const tags: string[] = [];
if (!obj.author) tags.push('no-author');
idx.saveObject({ ...obj, _tags: tags });

Then, you can filter by the tag later on like so:

const res = idx.search('', { filters: '_tags:no-author' });
Marie answered 26/2, 2021 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.