Elasticsearch 脚本:可以将嵌套日期作为日期对象直接访问吗? [英] Elasticsearch script: can a nested date be accessed directly as a date object?
问题描述
在我的 ES 数据中,有 nested
和 parent
日期字段.我需要在一个 ES 无痛脚本中使用这些日期.使用 parent
日期很容易操作,因为它被视为 时间对象
.例如,将 parent
日期 doc['validFrom']
转换为我刚刚使用的 UNIX 时间数字:
doc['validFrom'].value.millis
但是对于像 params._source['offers'][0].validFrom
这样的 nested
日期进行操作,情况就不同了.这些日期作为 String
而不是 date
返回.所以我必须手动将它们解析为 date
对象:
LocalDateTime.parse(params._source['offers'][0].validFrom), ZoneId.systemDefault()).toInstant().toEpochMilli()
这种手动日期解析给脚本带来了额外的复杂性.在我看来,它对性能也不利.嵌套日期字段
可以直接在elasticsearch脚本中作为date object
访问,而不用从String解析吗?
附言数据示例:
<预><代码>[{id":1",等级":8,validFrom":1970-01-01T00:00:00"优惠":[{id":777",排名":12,validFrom":2020-07-06T00:00:00"//!!!应该从这里取日期}]},{id":2",排名":35,validFrom":2019-05-03T00:00:00"//!!!应从此处获取日期,因为优惠为空报价":空}]我的脚本
排序":[{_脚本":{脚本":{源":params._source.offers != null ?ZonedDateTime.of(LocalDateTime.parse(params._source['offers'][0].validFrom), ZoneId.systemDefault()).toInstant().toEpochMilli() : doc['validFrom'].value.millis",朗":无痛";},类型":数量",顺序":升序";}}]
这个问题与这个问题有关.
这里的主题是区分 doc_values
和 _source
字段.
由于 doc_values
确实返回原始类型,您可以在日期字段上访问 .millis
.但是 _source
本身是一个 JSON 格式的、未经分析的 map-of-maps,因此不幸的是,您只能获得最初摄取的内容.
当您遇到性能问题时,我建议将嵌套的 validFrom
提取到顶层并调用它,例如 validFromOverride
.您的排序脚本逻辑复杂性将显着降低.
映射和doc 结构不需要是不可变的.
In my ES data, there are nested
and parent
date fields. I need to use these dates in one ES painless script. It is easy to operate with the parent
date as it is treated as a time object
. For example, to transform parent
dates doc['validFrom']
to UNIX-time number I just use:
doc['validFrom'].value.millis
But it is a different case for operating with nested
dates like params._source['offers'][0].validFrom
. These dates are returned as a String
, not date
. So I have to parse them to date
object manually:
LocalDateTime.parse(params._source['offers'][0].validFrom), ZoneId.systemDefault()).toInstant().toEpochMilli()
This manual date parsing brings extra complexity to the script. It seems to me that it is also not good for performance. Can a nested date field
be accessed as a date object
in elasticsearch script directly without parsing from String?
P.S Data example:
[
{
"id": "1",
"rank": 8,
"validFrom": "1970-01-01T00:00:00"
"offers": [
{
"id": "777",
"rank": 12,
"validFrom": "2020-07-06T00:00:00" // !!! should take the date from here
}
]
},
{
"id": "2",
"rank": 35,
"validFrom": "2019-05-03T00:00:00" // !!! should take the date from here as offers are null
"offers": null
}
]
My Script
"sort": [
{
"_script": {
"script": {
"source": "params._source.offers != null ? ZonedDateTime.of(LocalDateTime.parse(params._source['offers'][0].validFrom), ZoneId.systemDefault()).toInstant().toEpochMilli() : doc['validFrom'].value.millis",
"lang": "painless"
},
"type": "number",
"order": "asc"
}
}
]
This question is related to this one.
The topic here is differentiating between doc_values
and _source
fields.
Since doc_values
do return primitive types, you're able to access .millis
on the date field. But _source
itself is a JSON-ish, non-analyzed map-of-maps so you only get what was originally ingested, unfortunately.
When you're having trouble w/ the performance, I'd recommend extracting the nested validFrom
to the top level and call it, say, validFromOverride
. Your sort script logic complexity will then significantly reduce.
Mappings & doc structure don't need to be immutable.
这篇关于Elasticsearch 脚本:可以将嵌套日期作为日期对象直接访问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!