You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn/filtering_and_sorting/filter_expression_reference.mdx
+7-1Lines changed: 7 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -69,14 +69,20 @@ genres != action
69
69
70
70
### Comparison (`>`, `<`, `>=`, `<=`)
71
71
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.
73
73
74
74
The expression below returns all documents with a user rating above 85:
75
75
76
76
```
77
77
rating.users > 85
78
78
```
79
79
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
+
80
86
### `TO`
81
87
82
88
`TO` is equivalent to `>= AND <=`. The following expression returns all documents with a rating of 80 or above but below 90:
Copy file name to clipboardExpand all lines: learn/filtering_and_sorting/working_with_dates.mdx
+4-32Lines changed: 4 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ In this guide, you will learn about Meilisearch's approach to date and time valu
14
14
15
15
## Preparing your documents
16
16
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"`.
18
18
19
19
As an example, consider a database of video games. In this dataset, the release year is formatted as a timestamp:
20
20
@@ -41,39 +41,11 @@ As an example, consider a database of video games. In this dataset, the release
41
41
]
42
42
```
43
43
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
-
consttimestampInMilliseconds=Date.parse(game.release_date); // Date.parse returns the timestamp in milliseconds
57
-
consttimestamp= 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 <aid="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 <aid="downloadVideogames"href="/assets/datasets/videogames.json"download="videogames.json">videogame dataset</a> to a `games` index:
73
45
74
46
<CodeSamplesDateGuideIndex1 />
75
47
76
-
## Filtering by timestamp
48
+
## Filtering by date
77
49
78
50
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):
79
51
@@ -83,7 +55,7 @@ Once you have configured `filterableAttributes`, you can filter search results b
83
55
84
56
<CodeSamplesDateGuideFilter1 />
85
57
86
-
## Sorting by timestamp
58
+
## Sorting by date
87
59
88
60
To sort search results chronologically, add your document's timestamp field to the list of [`sortableAttributes`](/reference/api/settings#update-sortable-attributes):
0 commit comments