Skip to content

Commit db4911f

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

File tree

1 file changed

+69
-29
lines changed

1 file changed

+69
-29
lines changed

docs/reference/mapping/types/date_nanos.asciidoc

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,92 @@ PUT my-index-000001?include_type_name=true
3737
}
3838
}
3939
40-
PUT my-index-000001/_doc/1
40+
PUT my-index-000001/_bulk?refresh
41+
{ "index" : { "_id" : "1" } }
4142
{ "date": "2015-01-01" } <2>
42-
43-
PUT my-index-000001/_doc/2
43+
{ "index" : { "_id" : "2" } }
4444
{ "date": "2015-01-01T12:10:30.123456789Z" } <3>
45-
46-
PUT my-index-000001/_doc/3
47-
{ "date": 1420070400 } <4>
48-
49-
GET my-index-000001/_search
50-
{
51-
"sort": { "date": "asc"} <5>
52-
}
45+
{ "index" : { "_id" : "3" } }
46+
{ "date": 1420070400000 } <4>
5347
5448
GET my-index-000001/_search
5549
{
56-
"script_fields" : {
57-
"my_field" : {
58-
"script" : {
59-
"lang" : "painless",
60-
"source" : "doc['date'].value.nano" <6>
61-
}
50+
"sort": { "date": "asc"}, <5>
51+
"runtime_mappings": {
52+
"date_has_nanos": {
53+
"type": "boolean",
54+
"script": "emit(doc['date'].value.nano != 0)" <6>
6255
}
63-
}
64-
}
65-
66-
GET my-index-000001/_search
67-
{
68-
"docvalue_fields" : [
56+
},
57+
"fields": [
6958
{
70-
"field" : "date",
59+
"field": "date",
7160
"format": "strict_date_optional_time_nanos" <7>
61+
},
62+
{
63+
"field": "date_has_nanos"
7264
}
7365
]
7466
}
75-
--------------------------------------------------
76-
67+
----
68+
// TEST[s/_search/_search?filter_path=hits.hits/]
7769
<1> The `date` field uses the default `format`.
7870
<2> This document uses a plain date.
7971
<3> This document includes a time.
8072
<4> This document uses milliseconds-since-the-epoch.
8173
<5> Note that the `sort` values that are returned are all in
8274
nanoseconds-since-the-epoch.
83-
<6> Access the nanosecond part of the date in a script
84-
<7> Use doc value fields, which can be formatted in nanosecond
85-
resolution
75+
<6> Use `.nano` in scripts to return the nanosecond component of the date.
76+
<7> You can specify the format when fetching data using the <<search-fields-param,`fields` parameter>>.
77+
Use <<strict-date-time-nanos,`strict_date_optional_time_nanos`>> or you'll get a rounded result.
78+
79+
////
80+
[source,console-result]
81+
----
82+
{
83+
"hits": {
84+
"hits": [
85+
{
86+
"_id": "1",
87+
"_index": "my-index-000001",
88+
"_type": "_doc",
89+
"_score": null,
90+
"_source": {"date": "2015-01-01"},
91+
"fields": {
92+
"date": ["2015-01-01T00:00:00.000Z"],
93+
"date_has_nanos": [false]
94+
},
95+
"sort": [1420070400000000000]
96+
},
97+
{
98+
"_id": "3",
99+
"_type": "_doc",
100+
"_index": "my-index-000001",
101+
"_score": null,
102+
"_source": {"date": 1420070400000},
103+
"fields": {
104+
"date": ["2015-01-01T00:00:00.000Z"],
105+
"date_has_nanos": [false]
106+
},
107+
"sort": [1420070400000000000]
108+
},
109+
{
110+
"_id": "2",
111+
"_index": "my-index-000001",
112+
"_type": "_doc",
113+
"_score": null,
114+
"_source": {"date": "2015-01-01T12:10:30.123456789Z"},
115+
"fields": {
116+
"date": ["2015-01-01T12:10:30.123456789Z"],
117+
"date_has_nanos": [true]
118+
},
119+
"sort": [1420114230123456789]
120+
}
121+
]
122+
}
123+
}
124+
----
125+
////
86126

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

0 commit comments

Comments
 (0)