Skip to content

Commit e4451bd

Browse files
nik9000Adam Locke
and
Adam Locke
authored
Convert date_nanos example script to runtime field (#71351)
Runtime fields are much more flexible than script_fields because you can filter and aggregate on them so we hope folks use them! This converts the example of using a `date_nanos` field in a script to a runtime field so folks get used to seeing them and hopefully using them. While I was editing this I took the opportunity to replace the script with a real-ish example. Scripts that just load the field value are nice and short but I hope no one uses them in real life because they just add overhead when compared to accessing the field directly. So I made the script do something. Relates to #69291 Co-authored-by: Adam Locke <[email protected]>
1 parent b583ea8 commit e4451bd

File tree

1 file changed

+67
-30
lines changed

1 file changed

+67
-30
lines changed

docs/reference/mapping/types/date_nanos.asciidoc

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ the default:
2323
For instance:
2424

2525
[source,console]
26-
--------------------------------------------------
26+
----
2727
PUT my-index-000001
2828
{
2929
"mappings": {
@@ -35,52 +35,89 @@ PUT my-index-000001
3535
}
3636
}
3737
38-
PUT my-index-000001/_doc/1
38+
PUT my-index-000001/_bulk?refresh
39+
{ "index" : { "_id" : "1" } }
3940
{ "date": "2015-01-01" } <2>
40-
41-
PUT my-index-000001/_doc/2
41+
{ "index" : { "_id" : "2" } }
4242
{ "date": "2015-01-01T12:10:30.123456789Z" } <3>
43-
44-
PUT my-index-000001/_doc/3
45-
{ "date": 1420070400 } <4>
46-
47-
GET my-index-000001/_search
48-
{
49-
"sort": { "date": "asc"} <5>
50-
}
43+
{ "index" : { "_id" : "3" } }
44+
{ "date": 1420070400000 } <4>
5145
5246
GET my-index-000001/_search
5347
{
54-
"script_fields" : {
55-
"my_field" : {
56-
"script" : {
57-
"lang" : "painless",
58-
"source" : "doc['date'].value.nano" <6>
59-
}
48+
"sort": { "date": "asc"}, <5>
49+
"runtime_mappings": {
50+
"date_has_nanos": {
51+
"type": "boolean",
52+
"script": "emit(doc['date'].value.nano != 0)" <6>
6053
}
61-
}
62-
}
63-
64-
GET my-index-000001/_search
65-
{
66-
"docvalue_fields" : [
54+
},
55+
"fields": [
6756
{
68-
"field" : "date",
57+
"field": "date",
6958
"format": "strict_date_optional_time_nanos" <7>
59+
},
60+
{
61+
"field": "date_has_nanos"
7062
}
7163
]
7264
}
73-
--------------------------------------------------
74-
65+
----
66+
// TEST[s/_search/_search?filter_path=hits.hits/]
7567
<1> The `date` field uses the default `format`.
7668
<2> This document uses a plain date.
7769
<3> This document includes a time.
7870
<4> This document uses milliseconds-since-the-epoch.
7971
<5> Note that the `sort` values that are returned are all in
8072
nanoseconds-since-the-epoch.
81-
<6> Access the nanosecond part of the date in a script
82-
<7> Use doc value fields, which can be formatted in nanosecond
83-
resolution
73+
<6> Use `.nano` in scripts to return the nanosecond component of the date.
74+
<7> You can specify the format when fetching data using the <<search-fields-param,`fields` parameter>>.
75+
Use <<strict-date-time-nanos,`strict_date_optional_time_nanos`>> or you'll get a rounded result.
76+
77+
////
78+
[source,console-result]
79+
----
80+
{
81+
"hits": {
82+
"hits": [
83+
{
84+
"_id": "1",
85+
"_index": "my-index-000001",
86+
"_score": null,
87+
"_source": {"date": "2015-01-01"},
88+
"fields": {
89+
"date": ["2015-01-01T00:00:00.000Z"],
90+
"date_has_nanos": [false]
91+
},
92+
"sort": [1420070400000000000]
93+
},
94+
{
95+
"_id": "3",
96+
"_index": "my-index-000001",
97+
"_score": null,
98+
"_source": {"date": 1420070400000},
99+
"fields": {
100+
"date": ["2015-01-01T00:00:00.000Z"],
101+
"date_has_nanos": [false]
102+
},
103+
"sort": [1420070400000000000]
104+
},
105+
{
106+
"_id": "2",
107+
"_index": "my-index-000001",
108+
"_score": null,
109+
"_source": {"date": "2015-01-01T12:10:30.123456789Z"},
110+
"fields": {
111+
"date": ["2015-01-01T12:10:30.123456789Z"],
112+
"date_has_nanos": [true]
113+
},
114+
"sort": [1420114230123456789]
115+
}
116+
]
117+
}
118+
}
119+
----
120+
////
84121

85122
You can also specify multiple date formats separated by `||`. The
86123
same mapping parameters than with the `date` field can be used.

0 commit comments

Comments
 (0)