From 3ff3511c0739a2ea14a3069e8ce59ed12465c97c Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Thu, 15 May 2025 19:08:15 -0300 Subject: [PATCH 1/2] update comparison operator for filters, date guide --- .../filter_expression_reference.mdx | 8 ++++- .../working_with_dates.mdx | 36 +++---------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/learn/filtering_and_sorting/filter_expression_reference.mdx b/learn/filtering_and_sorting/filter_expression_reference.mdx index 22476540f..5f958c287 100644 --- a/learn/filtering_and_sorting/filter_expression_reference.mdx +++ b/learn/filtering_and_sorting/filter_expression_reference.mdx @@ -69,7 +69,7 @@ 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: @@ -77,6 +77,12 @@ 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: diff --git a/learn/filtering_and_sorting/working_with_dates.mdx b/learn/filtering_and_sorting/working_with_dates.mdx index bc55b1942..5c0fa7120 100644 --- a/learn/filtering_and_sorting/working_with_dates.mdx +++ b/learn/filtering_and_sorting/working_with_dates.mdx @@ -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: @@ -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 -}; -``` - - -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`. - - -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 videogame dataset 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 videogame dataset to a `games` index: -## 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): @@ -83,7 +55,7 @@ Once you have configured `filterableAttributes`, you can filter search results b -## 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): From 9be14f893a538fef940d2f8961afebaeb9efba1c Mon Sep 17 00:00:00 2001 From: gui machiavelli Date: Mon, 19 May 2025 16:18:36 -0300 Subject: [PATCH 2/2] first draft ready --- learn/filtering_and_sorting/filter_expression_reference.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/learn/filtering_and_sorting/filter_expression_reference.mdx b/learn/filtering_and_sorting/filter_expression_reference.mdx index 5f958c287..bcd3757e5 100644 --- a/learn/filtering_and_sorting/filter_expression_reference.mdx +++ b/learn/filtering_and_sorting/filter_expression_reference.mdx @@ -69,7 +69,7 @@ genres != action ### Comparison (`>`, `<`, `>=`, `<=`) -The comparison operators (`>`, `<`, `>=`, `<=`) select documents satisfying a comparison. Comparison operators apply to both numerical and string 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: @@ -77,10 +77,10 @@ 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: +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: ``` -release_date > 2004-01-09 +release_date > 2004-01-01 ``` ### `TO`