Learn how we implemented search and advanced filtering
clients/search.ts
FilterBuilder
class provides a convenient way to construct complex filter expressions for both Meilisearch and Algolia queries. It allows you to chain methods to define various filter criteria, including comparisons, logical operators, and special operators.
Here’s an example of how to use the FilterBuilder
when using Meilisearch:
FilterBuilder
when using Algolia:
FilterBuilder
offers a variety of methods to construct complex search queries, allowing you to tailor your product searches to specific needs and provide a user-friendly experience.
FilterBuilder
syntax is consistent for both Meilisearch and
Algolia, there might be slight differences in how certain filters are applied
or interpreted by each search engine. Always refer to the specific
documentation of the search engine you’re using for any engine-specific
behavior.Equal
( = )NotEqual
( != )GreaterThan
( > )GreaterThanOrEqual
( >= )LessThan
(< )LessThanOrEqual
( <= )To
( TO )And
(AND)Or
(OR)Not
(NOT)Exists
(EXISTS)IsEmpty
(IS EMPTY)IsNull
(IS NULL)where(attribute: string, operator: ComparisonOperators | SpecialOperators, value?: Value): FilterBuilder
attribute
: The attribute to filter on.operator
: The comparison or special operator to use.value
(optional): The value to compare against. Can be a single value or an array of values.FilterBuilder
instance for chaining.
to(attribute: string, min: number, max: number): FilterBuilder
attribute
: The attribute to filter on.min
: The minimum value of the range.max
: The maximum value of the range.FilterBuilder
instance for chaining.
exists(attribute: string): FilterBuilder
attribute
: The attribute to check for existence.FilterBuilder
instance for chaining.
isEmpty(attribute: string): FilterBuilder
attribute
: The attribute to check for emptiness.FilterBuilder
instance for chaining.
isNull(attribute: string): FilterBuilder
attribute
: The attribute to check for null value.FilterBuilder
instance for chaining.
in(attribute: string, values: (string | number)[]): FilterBuilder
attribute
: The attribute to filter on.values
: An array of values to check against.FilterBuilder
instance for chaining.
not(): FilterBuilder
FilterBuilder
instance for chaining.
and(): FilterBuilder
FilterBuilder
instance for chaining.
or(): FilterBuilder
FilterBuilder
instance for chaining.
group(fn: (builder: FilterBuilder) => void): FilterBuilder
fn
: A callback function that receives a new FilterBuilder
instance to build the grouped conditions.FilterBuilder
instance for chaining.
build(): string