Skip to content

Commit 14f775f

Browse files
nik9000Adam Locke
and
Adam Locke
authored
Convert date_nanos example script to runtime field (backport of #71351) (#71595)
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 b4dbb4d commit 14f775f

File tree

1 file changed

+70
-30
lines changed

1 file changed

+70
-30
lines changed

docs/reference/mapping/types/date_nanos.asciidoc

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,52 +45,92 @@ PUT my-index-000001?include_type_name=true
4545
}
4646
}
4747
48-
PUT my-index-000001/_doc/1
48+
PUT my-index-000001/_bulk?refresh
49+
{ "index" : { "_id" : "1" } }
4950
{ "date": "2015-01-01" } <2>
50-
51-
PUT my-index-000001/_doc/2
51+
{ "index" : { "_id" : "2" } }
5252
{ "date": "2015-01-01T12:10:30.123456789Z" } <3>
53-
54-
PUT my-index-000001/_doc/3
55-
{ "date": 1420070400 } <4>
56-
57-
GET my-index-000001/_search
58-
{
59-
"sort": { "date": "asc"} <5>
60-
}
53+
{ "index" : { "_id" : "3" } }
54+
{ "date": 1420070400000 } <4>
6155
6256
GET my-index-000001/_search
6357
{
64-
"script_fields" : {
65-
"my_field" : {
66-
"script" : {
67-
"lang" : "painless",
68-
"source" : "doc['date'].value.nano" <6>
69-
}
58+
"sort": { "date": "asc"}, <5>
59+
"runtime_mappings": {
60+
"date_has_nanos": {
61+
"type": "boolean",
62+
"script": "emit(doc['date'].value.nano != 0)" <6>
7063
}
71-
}
72-
}
73-
74-
GET my-index-000001/_search
75-
{
76-
"docvalue_fields" : [
64+
},
65+
"fields": [
66+
{
67+
"field": "date",
68+
"format": "strict_date_optional_time_nanos" <7>
69+
},
7770
{
78-
"field" : "date",
79-
"format": "strict_date_time" <7>
71+
"field": "date_has_nanos"
8072
}
8173
]
8274
}
83-
--------------------------------------------------
84-
75+
----
76+
// TEST[s/_search/_search?filter_path=hits.hits/]
8577
<1> The `date` field uses the default `format`.
8678
<2> This document uses a plain date.
8779
<3> This document includes a time.
8880
<4> This document uses milliseconds-since-the-epoch.
8981
<5> Note that the `sort` values that are returned are all in
9082
nanoseconds-since-the-epoch.
91-
<6> Access the nanosecond part of the date in a script
92-
<7> Use doc value fields, which can be formatted in nanosecond
93-
resolution
83+
<6> Use `.nano` in scripts to return the nanosecond component of the date.
84+
<7> You can specify the format when fetching data using the <<search-fields-param,`fields` parameter>>.
85+
Use <<strict-date-time-nanos,`strict_date_optional_time_nanos`>> or you'll get a rounded result.
86+
87+
////
88+
[source,console-result]
89+
----
90+
{
91+
"hits": {
92+
"hits": [
93+
{
94+
"_id": "1",
95+
"_index": "my-index-000001",
96+
"_type": "_doc",
97+
"_score": null,
98+
"_source": {"date": "2015-01-01"},
99+
"fields": {
100+
"date": ["2015-01-01T00:00:00.000Z"],
101+
"date_has_nanos": [false]
102+
},
103+
"sort": [1420070400000000000]
104+
},
105+
{
106+
"_id": "3",
107+
"_type": "_doc",
108+
"_index": "my-index-000001",
109+
"_score": null,
110+
"_source": {"date": 1420070400000},
111+
"fields": {
112+
"date": ["2015-01-01T00:00:00.000Z"],
113+
"date_has_nanos": [false]
114+
},
115+
"sort": [1420070400000000000]
116+
},
117+
{
118+
"_id": "2",
119+
"_index": "my-index-000001",
120+
"_type": "_doc",
121+
"_score": null,
122+
"_source": {"date": "2015-01-01T12:10:30.123456789Z"},
123+
"fields": {
124+
"date": ["2015-01-01T12:10:30.123456789Z"],
125+
"date_has_nanos": [true]
126+
},
127+
"sort": [1420114230123456789]
128+
}
129+
]
130+
}
131+
}
132+
----
133+
////
94134

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

0 commit comments

Comments
 (0)