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: _opensearch/bucket-agg.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -697,7 +697,7 @@ Sample Response
697
697
}
698
698
```
699
699
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.
701
701
702
702
For example, you can find how many hits your website gets per month:
Copy file name to clipboardExpand all lines: _opensearch/supported-field-types/date.md
+124Lines changed: 124 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -209,3 +209,127 @@ GET testindex/_search
209
209
}
210
210
}
211
211
```
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:
0 commit comments