Date nanoseconds field type

Introduced 1.0

The date_nanos field type is similar to the date field type in that it holds a date. However, date stores the date in millisecond resolution, while date_nanos stores the date in nanosecond resolution. Dates are stored as long values that correspond to nanoseconds since the epoch. Therefore, the range of supported dates is approximately 1970–2262.

Queries on date_nanos fields are converted to range queries on the field value’s long representation. Then the stored fields and aggregation results are converted to a string using the format set on the field.

The date_nanos field supports all formats and parameters that date supports. You can use multiple formats separated by ||.

For date_nanos fields, you can use the strict_date_optional_time_nanos format to preserve nanosecond resolution. If you don’t specify the format when mapping a field as date_nanos, the default format is strict_date_optional_time||epoch_millis that lets you pass values in either strict_date_optional_time or epoch_millis format. The strict_date_optional_time format supports dates in nanosecond resolution, but the epoch_millis format supports dates in millisecond resolution only.

Example

Create a mapping with the date field of type date_nanos that has the strict_date_optional_time_nanos format:

  1. PUT testindex/_mapping
  2. {
  3. "properties": {
  4. "date": {
  5. "type": "date_nanos",
  6. "format" : "strict_date_optional_time_nanos"
  7. }
  8. }
  9. }

copy

Index two documents into the index:

  1. PUT testindex/_doc/1
  2. { "date": "2022-06-15T10:12:52.382719622Z" }

copy

  1. PUT testindex/_doc/2
  2. { "date": "2022-06-15T10:12:52.382719624Z" }

copy

You can use a range query to search for a date range:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "range": {
  5. "date": {
  6. "gte": "2022-06-15T10:12:52.382719621Z",
  7. "lte": "2022-06-15T10:12:52.382719623Z"
  8. }
  9. }
  10. }
  11. }

copy

The response contains the document whose date is in the specified range:

  1. {
  2. "took": 43,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "date": "2022-06-15T10:12:52.382719622Z"
  23. }
  24. }
  25. ]
  26. }
  27. }

When querying documents with date_nanos fields, you can use fields or docvalue_fields:

  1. GET testindex/_search
  2. {
  3. "fields": ["date"]
  4. }

copy

  1. GET testindex/_search
  2. {
  3. "docvalue_fields" : [
  4. {
  5. "field" : "date"
  6. }
  7. ]
  8. }

copy

The response to either of the preceding queries contains both indexed documents:

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 1,
  21. "_source": {
  22. "date": "2022-06-15T10:12:52.382719622Z"
  23. },
  24. "fields": {
  25. "date": [
  26. "2022-06-15T10:12:52.382719622Z"
  27. ]
  28. }
  29. },
  30. {
  31. "_index": "testindex",
  32. "_id": "2",
  33. "_score": 1,
  34. "_source": {
  35. "date": "2022-06-15T10:12:52.382719624Z"
  36. },
  37. "fields": {
  38. "date": [
  39. "2022-06-15T10:12:52.382719624Z"
  40. ]
  41. }
  42. }
  43. ]
  44. }
  45. }

You can sort on a date_nanos field as follows:

  1. GET testindex/_search
  2. {
  3. "sort": {
  4. "date": "asc"
  5. }
  6. }

copy

The response contains the sorted documents:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": null,
  21. "_source": {
  22. "date": "2022-06-15T10:12:52.382719622Z"
  23. },
  24. "sort": [
  25. 1655287972382719700
  26. ]
  27. },
  28. {
  29. "_index": "testindex",
  30. "_id": "2",
  31. "_score": null,
  32. "_source": {
  33. "date": "2022-06-15T10:12:52.382719624Z"
  34. },
  35. "sort": [
  36. 1655287972382719700
  37. ]
  38. }
  39. ]
  40. }
  41. }

You can also use a Painless script to access the nanoseconds part of the field:

  1. GET testindex/_search
  2. {
  3. "script_fields" : {
  4. "my_field" : {
  5. "script" : {
  6. "lang" : "painless",
  7. "source" : "doc['date'].value.nano"
  8. }
  9. }
  10. }
  11. }

copy

The response contains only the nanosecond parts of the fields:

  1. {
  2. "took": 4,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 1,
  21. "fields": {
  22. "my_field": [
  23. 382719622
  24. ]
  25. }
  26. },
  27. {
  28. "_index": "testindex",
  29. "_id": "2",
  30. "_score": 1,
  31. "fields": {
  32. "my_field": [
  33. 382719624
  34. ]
  35. }
  36. }
  37. ]
  38. }
  39. }