Search code examples
typesense

Filter by array length in typesense query


I am using typesense and have the following document structure in a people collection:

{
 ...
 people: ['Jim', 'Jane', 'Jack']
 ...
}

I would like to add a filter to my query that allows me to filter by the number of entries within the peoples array for each document.

e.g


{
  filter_by: people:=[0...2]
}

This should return all documents that have between 0 - 2 entries within the peoples array.

The documentation states that this is possible for numeric fields, but does not mention anything related to array fields.

Numeric Filtering: Filter documents with numeric values between a min and max value, using the range operator [min..max] or using simple comparison operators >, >= <, <=, =.

Is this possible or do i need to explicitly add in a numeric field to the documents.


Solution

  • It's not possible to filter based on array length in Typesense.

    So you would have to create a new numeric field in each document called say people_length, that you calculate at indexing time and then use that field for filtering.