-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert date_nanos example script to runtime field #71351
Merged
nik9000
merged 4 commits into
elastic:master
from
nik9000:docs_runtime_field_date_nanos_3
Apr 12, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ the default: | |
For instance: | ||
|
||
[source,console] | ||
-------------------------------------------------- | ||
---- | ||
PUT my-index-000001 | ||
{ | ||
"mappings": { | ||
|
@@ -35,52 +35,89 @@ PUT my-index-000001 | |
} | ||
} | ||
|
||
PUT my-index-000001/_doc/1 | ||
PUT my-index-000001/_bulk?refresh | ||
{ "index" : { "_id" : "1" } } | ||
{ "date": "2015-01-01" } <2> | ||
|
||
PUT my-index-000001/_doc/2 | ||
{ "index" : { "_id" : "2" } } | ||
{ "date": "2015-01-01T12:10:30.123456789Z" } <3> | ||
|
||
PUT my-index-000001/_doc/3 | ||
{ "date": 1420070400 } <4> | ||
|
||
GET my-index-000001/_search | ||
{ | ||
"sort": { "date": "asc"} <5> | ||
} | ||
{ "index" : { "_id" : "3" } } | ||
{ "date": 1420070400000 } <4> | ||
|
||
GET my-index-000001/_search | ||
{ | ||
"script_fields" : { | ||
"my_field" : { | ||
"script" : { | ||
"lang" : "painless", | ||
"source" : "doc['date'].value.nano" <6> | ||
} | ||
"sort": { "date": "asc"}, <5> | ||
"runtime_mappings": { | ||
"date_has_nanos": { | ||
"type": "boolean", | ||
"script": "emit(doc['date'].value.nano != 0)" <6> | ||
} | ||
} | ||
} | ||
|
||
GET my-index-000001/_search | ||
{ | ||
"docvalue_fields" : [ | ||
}, | ||
"fields": [ | ||
{ | ||
"field" : "date", | ||
"field": "date", | ||
"format": "strict_date_optional_time_nanos" <7> | ||
}, | ||
{ | ||
"field": "date_has_nanos" | ||
} | ||
] | ||
} | ||
-------------------------------------------------- | ||
|
||
---- | ||
// TEST[s/_search/_search?filter_path=hits.hits/] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 for |
||
<1> The `date` field uses the default `format`. | ||
<2> This document uses a plain date. | ||
<3> This document includes a time. | ||
<4> This document uses milliseconds-since-the-epoch. | ||
<5> Note that the `sort` values that are returned are all in | ||
nanoseconds-since-the-epoch. | ||
<6> Access the nanosecond part of the date in a script | ||
<7> Use doc value fields, which can be formatted in nanosecond | ||
resolution | ||
<6> Use `.nano` in scripts to return the nanosecond component of the date. | ||
<7> You can specify the format when fetching data using the <<search-fields-param,`fields` parameter>>. | ||
Use <<strict-date-time-nanos,`strict_date_optional_time_nanos`>> or you'll get a rounded result. | ||
|
||
//// | ||
[source,console-result] | ||
---- | ||
{ | ||
"hits": { | ||
"hits": [ | ||
{ | ||
"_id": "1", | ||
"_index": "my-index-000001", | ||
"_score": null, | ||
"_source": {"date": "2015-01-01"}, | ||
"fields": { | ||
"date": ["2015-01-01T00:00:00.000Z"], | ||
"date_has_nanos": [false] | ||
}, | ||
"sort": [1420070400000000000] | ||
}, | ||
{ | ||
"_id": "3", | ||
"_index": "my-index-000001", | ||
"_score": null, | ||
"_source": {"date": 1420070400000}, | ||
"fields": { | ||
"date": ["2015-01-01T00:00:00.000Z"], | ||
"date_has_nanos": [false] | ||
}, | ||
"sort": [1420070400000000000] | ||
}, | ||
{ | ||
"_id": "2", | ||
"_index": "my-index-000001", | ||
"_score": null, | ||
"_source": {"date": "2015-01-01T12:10:30.123456789Z"}, | ||
"fields": { | ||
"date": ["2015-01-01T12:10:30.123456789Z"], | ||
"date_has_nanos": [true] | ||
}, | ||
"sort": [1420114230123456789] | ||
} | ||
] | ||
} | ||
} | ||
---- | ||
//// | ||
|
||
You can also specify multiple date formats separated by `||`. The | ||
same mapping parameters than with the `date` field can be used. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was lacking a few 0s in the example and we never caught it because we didn't have a test for the response.