Skip to content

Commit bb5fc71

Browse files
v1.15: String comparison filter (#3256)
1 parent 9e6fe75 commit bb5fc71

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

learn/filtering_and_sorting/filter_expression_reference.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,20 @@ genres != action
6969

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

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

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

7676
```
7777
rating.users > 85
7878
```
7979

80+
String comparisons resolve in lexicographic order: symbols followed by numbers followed by letters in alphabetic order. The expression below returns all documents released after the first day of 2004:
81+
82+
```
83+
release_date > 2004-01-01
84+
```
85+
8086
### `TO`
8187

8288
`TO` is equivalent to `>= AND <=`. The following expression returns all documents with a rating of 80 or above but below 90:

learn/filtering_and_sorting/working_with_dates.mdx

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ In this guide, you will learn about Meilisearch's approach to date and time valu
1414

1515
## Preparing your documents
1616

17-
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).
17+
To filter and sort search results chronologically, your documents must have at least one field containing a [UNIX timestamp](https://kb.narrative.io/what-is-unix-time). You may also use a string with a date in a format that can be sorted lexicographically, such as `"2025-01-13"`.
1818

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

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

44-
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.
45-
46-
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:
47-
48-
```js
49-
let game = {
50-
"id": 0,
51-
"title": "Return of the Obra Dinn",
52-
"genre": "adventure",
53-
"release_date": "2018-10-18T00:00Z"
54-
};
55-
56-
const timestampInMilliseconds = Date.parse(game.release_date); // Date.parse returns the timestamp in milliseconds
57-
const timestamp = timestampInMilliseconds / 1000; // UNIX timestamps must be in seconds
58-
59-
game = {
60-
"id": 0,
61-
"title": "Return of the Obra Dinn",
62-
"genre": "adventure",
63-
"release_date": "2018-10-18T00:00Z",
64-
"release_timestamp": timestamp
65-
};
66-
```
67-
68-
<Tip>
69-
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`.
70-
</Tip>
71-
72-
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:
44+
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:
7345

7446
<CodeSamplesDateGuideIndex1 />
7547

76-
## Filtering by timestamp
48+
## Filtering by date
7749

7850
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):
7951

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

8456
<CodeSamplesDateGuideFilter1 />
8557

86-
## Sorting by timestamp
58+
## Sorting by date
8759

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

0 commit comments

Comments
 (0)