gpt4 book ai didi

node.js - 获取嵌套字段ElasticSearch中的最后一项

转载 作者:行者123 更新时间:2023-12-03 02:34:08 25 4
gpt4 key购买 nike

我使用ElasticSearch来存储数据。我想获取嵌套字段中的最后一项。更具体地说,是消息数组的最后一个元素。数据结构enter image description here

最佳答案

您可以在搜索查询中使用scripted_fields来查找数组的最后一个元素。

例如,样本数据如下所示:

POST room/_doc
{
"message": [
{"date": "2019-12-11T11:21:29.060Z", "name":"", "id":"SYSTEM_MESSAGE"},
{"date": "2019-12-11T11:21:41.525Z", "name":"n910827m", "id":"SYSTEM_MESSAGE"}
],
"name": "hello",
"superuser": "9hxj0BNxs4AppB7kAAAD"
}

并且,如果要查找 message字段的最后一个元素,搜索查询如下所示:
POST room/_search
{
"script_fields": {
"message": {
"script": {
"source":
"""
def messages = params['_source']['message'];
return messages[messages.length - 1];
""",
"lang": "painless"
}
}
}
}

结果:

enter image description here

关于node.js - 获取嵌套字段ElasticSearch中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59282797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com