Skip to content

Commit f9a9ea1

Browse files
authored
Adds date math documentation (opensearch-project#2171)
* Adds date math documentation Signed-off-by: Fanit Kolchina <[email protected]> * Incorporated doc review comments Signed-off-by: Fanit Kolchina <[email protected]> * Incorporated editorial comments Signed-off-by: Fanit Kolchina <[email protected]> Signed-off-by: Fanit Kolchina <[email protected]>
1 parent 4710471 commit f9a9ea1

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

_opensearch/bucket-agg.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ Sample Response
697697
}
698698
```
699699

700-
The `date_histogram` aggregation uses date math to generate histograms for time-series data.
700+
The `date_histogram` aggregation uses [date math]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/date/#date-math) to generate histograms for time-series data.
701701

702702
For example, you can find how many hits your website gets per month:
703703

_opensearch/query-dsl/term.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ GET products/_search
329329
}
330330
```
331331

332-
Specify relative dates by using basic math expressions.
332+
Specify relative dates by using [date math]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/date/#date-math).
333333

334-
To subtract 1 year and 1 day from the specified date:
334+
To subtract 1 year and 1 day from the specified date, use the following query:
335335

336336
```json
337337
GET products/_search

_opensearch/supported-field-types/date.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,127 @@ GET testindex/_search
209209
}
210210
}
211211
```
212+
213+
## Date math
214+
215+
The date field type supports using date math to specify duration in queries. For example, the `gt`, `gte`, `lt`, and `lte` parameters in [range queries]({{site.url}}{{site.baseurl}}/opensearch/query-dsl/term/#range-query) and the `from` and `to` parameters in [date range aggregations]({{site.url}}{{site.baseurl}}/opensearch/bucket-agg/#range-date_range-ip_range) accept date math expressions.
216+
217+
A date math expression contains a fixed date, optionally followed by one or more mathematical expressions. The fixed date may be either `now` (current date and time in milliseconds since the epoch) or a string ending with `||` that specifies a date (for example, `2022-05-18||`). The date must be in the `strict_date_optional_time||epoch_millis` format.
218+
219+
Date math supports the following mathematical operators.
220+
221+
Operator | Description | Example
222+
:--- | :--- | :---
223+
`+` | Addition | `+1M`: Add 1 month.
224+
`-` | Subtraction | `-1y`: Subtract 1 year.
225+
`/` | Rounding down | `/h`: Round to the beginning of the hour.
226+
227+
Date math supports the following time units:
228+
229+
`y`: Years<br>
230+
`M`: Months<br>
231+
`w`: Weeks<br>
232+
`d`: Days<br>
233+
`h` or `H`: Hours<br>
234+
`m`: Minutes<br>
235+
`s`: Seconds
236+
{: .note }
237+
238+
### Example expressions
239+
240+
The following example expressions illustrate using date math:
241+
242+
- `now+1M`: The current date and time in milliseconds since the epoch, plus 1 month.
243+
- `2022-05-18||/M`: 05/18/2022, rounded to the beginning of the month. Resolves to `2022-05-01`.
244+
- `2022-05-18T15:23||/h`: 15:23 on 05/18/2022, rounded to the beginning of the hour. Resolves to `2022-05-18T15`.
245+
- `2022-05-18T15:23:17.789||+2M-1d/d`: 15:23:17.789 on 05/18/2022 plus 2 months minus 1 day, rounded to the beginning of the day. Resolves to `2022-07-17`.
246+
247+
248+
### Using date math in a range query
249+
250+
The following example illustrates using date math in a [range query]({{site.url}}{{site.baseurl}}/opensearch/query-dsl/term/#range-query).
251+
252+
Set up an index with `release_date` mapped as `date`:
253+
254+
```json
255+
PUT testindex
256+
{
257+
"mappings" : {
258+
"properties" : {
259+
"release_date" : {
260+
"type" : "date"
261+
}
262+
}
263+
}
264+
}
265+
```
266+
267+
Index two documents into the index:
268+
269+
```json
270+
PUT testindex/_doc/1
271+
{
272+
"release_date": "2022-09-14"
273+
}
274+
275+
PUT testindex/_doc/2
276+
{
277+
"release_date": "2022-11-15"
278+
}
279+
```
280+
281+
The following query searches for documents with `release_date` within 2 months and 1 day of 09/14/2022. The lower boundary of the range is rounded to the beginning of the day on 09/14/2022:
282+
283+
```json
284+
GET testindex/_search
285+
{
286+
"query": {
287+
"range": {
288+
"release_date": {
289+
"gte": "2022-09-14T15:23||/d",
290+
"lte": "2022-09-14||+2M+1d"
291+
}
292+
}
293+
}
294+
}
295+
```
296+
297+
The response contains both documents:
298+
299+
```json
300+
{
301+
"took" : 1,
302+
"timed_out" : false,
303+
"_shards" : {
304+
"total" : 1,
305+
"successful" : 1,
306+
"skipped" : 0,
307+
"failed" : 0
308+
},
309+
"hits" : {
310+
"total" : {
311+
"value" : 2,
312+
"relation" : "eq"
313+
},
314+
"max_score" : 1.0,
315+
"hits" : [
316+
{
317+
"_index" : "testindex",
318+
"_id" : "2",
319+
"_score" : 1.0,
320+
"_source" : {
321+
"release_date" : "2022-11-14"
322+
}
323+
},
324+
{
325+
"_index" : "testindex",
326+
"_id" : "1",
327+
"_score" : 1.0,
328+
"_source" : {
329+
"release_date" : "2022-09-14"
330+
}
331+
}
332+
]
333+
}
334+
}
335+
```

0 commit comments

Comments
 (0)