Search & Filtering
Learn how we implemented search and advanced filtering
Enterprise Commerce integrates Meilisearch as its core engine for robust search, filtering, and recommendations, ensuring that product data is accurately reflected and easily accessible. Meilisearch provides a lightning-fast, typo-tolerant search experience that enhances the overall usability and efficiency of Enterprise Commerce.
To learn more about Meilisearch and its capabilities, visit Meilisearch Documentation.
The integration with Meilisearch is designed to operate exclusively on the server side. Attempts to utilize the Meilisearch client or SDK on the client side will result in an error
Developers should access the Meilisearch instance via the provided path apps/web/clients/meilisearch.ts
Using the Filter Builder
The FilterBuilder
class provides a convenient way to construct complex filter expressions for Meilisearch queries. It allows you to chain methods to define various filter criteria, including comparisons, logical operators, and special operators like EXISTS
, IS EMPTY
, and IS NULL
.
Here’s an example of how to use the FilterBuilder
:
const filter = new FilterBuilder()
.where("price", ComparisonOperators.GreaterThan, 100)
.and()
.group((sub) => sub.where("category", "=", "Electronics").or().where("category", "=", "Computers"))
.build()
// This will generate the following filter expression:
// price > 100 AND (category = "Electronics" OR category = "Computers")
This filter expression will search for products with a price greater than 100 and belonging to either the Electronics
or Computers
category.
The 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.
Reference
Enums
ComparisonOperators
Represents the comparison operators that can be used in filtering conditions.
Equal
( = )NotEqual
( != )GreaterThan
( > )GreaterThanOrEqual
( >= )LessThan
(< )LessThanOrEqual
( <= )To
( TO )
LogicalOperators
Represents the logical operators that can be used to combine filtering conditions.
And
(AND)Or
(OR)Not
(NOT)
SpecialOperators
Represents special operators that can be used in filtering conditions.
Exists
(EXISTS)IsEmpty
(IS EMPTY)IsNull
(IS NULL)
Methods
where(attribute: string, operator: ComparisonOperators | SpecialOperators, value?: Value): FilterBuilder
Adds a filtering condition to the expression.
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.
Returns the FilterBuilder
instance for chaining.
to(attribute: string, min: number, max: number): FilterBuilder
Adds a range filtering condition to the expression.
attribute
: The attribute to filter on.min
: The minimum value of the range.max
: The maximum value of the range.
Returns the FilterBuilder
instance for chaining.
exists(attribute: string): FilterBuilder
Adds an “EXISTS” condition to the expression.
attribute
: The attribute to check for existence.
Returns the FilterBuilder
instance for chaining.
isEmpty(attribute: string): FilterBuilder
Adds an “IS EMPTY” condition to the expression.
attribute
: The attribute to check for emptiness.
Returns the FilterBuilder
instance for chaining.
isNull(attribute: string): FilterBuilder
Adds an “IS NULL” condition to the expression.
attribute
: The attribute to check for null value.
Returns the FilterBuilder
instance for chaining.
in(attribute: string, values: (string | number)[]): FilterBuilder
Adds an “IN” condition to the expression.
attribute
: The attribute to filter on.values
: An array of values to check against.
Returns the FilterBuilder
instance for chaining.
not(): FilterBuilder
Adds a “NOT” operator to the expression.
Returns the FilterBuilder
instance for chaining.
and(): FilterBuilder
Adds an “AND” operator to the expression.
Returns the FilterBuilder
instance for chaining.
or(): FilterBuilder
Adds an “OR” operator to the expression.
Returns the FilterBuilder
instance for chaining.
group(fn: (builder: FilterBuilder) => void): FilterBuilder
Groups a set of filtering conditions together.
fn
: A callback function that receives a newFilterBuilder
instance to build the grouped conditions.
Returns the FilterBuilder
instance for chaining.
build(): string
Builds the final filtering expression as a string.
Returns the filtering expression string.