> ## Documentation Index
> Fetch the complete documentation index at: https://docs.commerce.blazity.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Search & Filtering

> Learn how we implemented search and advanced filtering

Enterprise Commerce integrates both Meilisearch or Algolia as its core engine for **robust search, filtering, and recommendations**, ensuring that product data is accurately reflected and easily accessible. Both search engines provide lightning-fast, typo-tolerant search experiences that enhance the overall usability and efficiency of Enterprise Commerce.

To learn more about these search engines and their capabilities, visit:

* [Meilisearch Documentation](https://www.meilisearch.com/docs)
* [Algolia Documentation](https://www.algolia.com/doc/)

<Warning>
  The integration with both Meilisearch and Algolia is designed to operate
  exclusively on the server side. Attempts to utilize their respective clients
  or SDKs on the client side will result in an error.
</Warning>

Developers should access the search engine instances via the provided path `clients/search.ts`

## Using the Filter Builder

The `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:

```typescript theme={null}
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")
```

Here's an example of how to use the `FilterBuilder` when using Algolia:

```typescript theme={null}
const filter = new FilterBuilder()
  .where("price", 100, ComparisonOperators.GreaterThan)
  .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")
```

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.

<Note>
  While the `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.
</Note>

## 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 new `FilterBuilder` 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.
