Skip to content

v1.15: String comparison filter #3256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: v1.15
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion learn/filtering_and_sorting/filter_expression_reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ genres != action

### Comparison (`>`, `<`, `>=`, `<=`)

The comparison operators (`>`, `<`, `>=`, `<=`) select documents satisfying a comparison. Comparison operators only apply only to numerical values.
The comparison operators (`>`, `<`, `>=`, `<=`) select documents satisfying a comparison. Comparison operators apply to both numerical and string values.

The expression below returns all documents with a user rating above 85:

```
rating.users > 85
```

String comparisons resolve in lexicographic order: symbols followed by numbers followed by letters in alphabetic order. String comparisons can be useful when filtering human-readable dates:

```
release_date > 2004-01-09
```

### `TO`

`TO` is equivalent to `>= AND <=`. The following expression returns all documents with a rating of 80 or above but below 90:
Expand Down
36 changes: 4 additions & 32 deletions learn/filtering_and_sorting/working_with_dates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In this guide, you will learn about Meilisearch's approach to date and time valu

## Preparing your documents

To filter and sort search results chronologically, your documents must have at least one numeric field containing a [UNIX timestamp](https://kb.narrative.io/what-is-unix-time).
To filter and sort search results chronologically, your documents must have at least one field containing either a [UNIX timestamp](https://kb.narrative.io/what-is-unix-time) or a date in a human readable format such as `"2025-01-13"`.

As an example, consider a database of video games. In this dataset, the release year is formatted as a timestamp:

Expand All @@ -41,39 +41,11 @@ As an example, consider a database of video games. In this dataset, the release
]
```

If your date field is expressed in a format other than a numeric timestamp, like [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html), you must convert it before indexing it with Meilisearch.

Most programming languages have built-in tools to help you with this process. The JavaScript example below converts a game's release date, `"2018-10-18"`, to a numeric timestamp:

```js
let game = {
"id": 0,
"title": "Return of the Obra Dinn",
"genre": "adventure",
"release_date": "2018-10-18T00:00Z"
};

const timestampInMilliseconds = Date.parse(game.release_date); // Date.parse returns the timestamp in milliseconds
const timestamp = timestampInMilliseconds / 1000; // UNIX timestamps must be in seconds

game = {
"id": 0,
"title": "Return of the Obra Dinn",
"genre": "adventure",
"release_date": "2018-10-18T00:00Z",
"release_timestamp": timestamp
};
```

<Tip>
When preparing your dataset, it can be useful to leave the original date and time fields in your documents intact. In the example above, we keep the `release_date` field because it is more readable than the raw `release_timestamp`.
</Tip>

After adding a numeric timestamp to all documents, [index your data](/reference/api/documents#add-or-replace-documents) as usual. The example below adds a <a id="downloadVideogames" href="/assets/datasets/videogames.json" download="videogames.json">videogame dataset</a> to a `games` index:
Once all documents in your dataset have a date field, [index your data](/reference/api/documents#add-or-replace-documents) as usual. The example below adds a <a id="downloadVideogames" href="/assets/datasets/videogames.json" download="videogames.json">videogame dataset</a> to a `games` index:

<CodeSamplesDateGuideIndex1 />

## Filtering by timestamp
## Filtering by date

To filter search results based on their timestamp, add your document's timestamp field to the list of [`filterableAttributes`](/reference/api/settings#update-filterable-attributes):

Expand All @@ -83,7 +55,7 @@ Once you have configured `filterableAttributes`, you can filter search results b

<CodeSamplesDateGuideFilter1 />

## Sorting by timestamp
## Sorting by date

To sort search results chronologically, add your document's timestamp field to the list of [`sortableAttributes`](/reference/api/settings#update-sortable-attributes):

Expand Down
Loading