gpt4 book ai didi

elasticsearch - 过滤对象数组的不同字段

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

在Elasticsearch中,说我有这样的文档:

{
"id": "testId",
"inputs": [
{
"status": "STARTED",
"lastUpdatedTime": "2020-06-10T00:00:00.000Z"
},
{
"status": "STARTED",
"lastUpdatedTime": "2020-05-11T00:00:00.000Z"
},
{
"status": "ENDED",
"lastUpdatedTime": "2020-06-11T00:00:00.000Z"
}
]
}

现在,我想过滤所有文档,以便获得状态为ENDED的所有文档,并且lastUpdatedTime应该在输入数组中最高。例如。在上述情况下,它将以2020-06-11T00:00:00.000Z> 2020-06-10T00:00:00.000Z和2020-05-11T00:00:00.000Z返回此文档,状态为ENDED。但是请说,对于下面的文档,它不会返回:
{
"id": "testId2",
"inputs": [
{
"status": "STARTED",
"lastUpdatedTime": "2020-06-10T00:00:00.000Z"
},
{
"status": "STARTED",
"lastUpdatedTime": "2020-05-11T00:00:00.000Z"
},
{
"status": "ENDED",
"lastUpdatedTime": "2020-05-11T00:00:00.000Z"
}
]
}

这是因为在本文档中STARTED具有最大的lastUpdatedTime。我该如何在Elasticsearch中轻松进行这种过滤或无法进行过滤?

最佳答案

通过此操作,在下面的查询中,您将获得结果,使得lastUpdatedTime应该在与STATUS="ENDED" 对应的输入数组中最高

但这只能解决一部分答案,同一查询不会给您想要的结果(即,对于lastUpdatedTime的最大值是STARTED的第二种情况,该查询将不起作用)

映射:

{
"mappings": {
"properties": {
"inputs": {
"type": "nested"
},
"lastUpdatedTime": { "type": "date" }
}
}
}

搜索查询:
{
"query": {
"nested": {
"path": "inputs",
"query": {
"match": {"inputs.status":"ENDED"}
},

"inner_hits": {
"sort": [
{
"lastUpdatedTime": {
"order": "desc"
}
}
],
"size": 1
}

}
}
}

结果:
"inner_hits": {
"inputs": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "-s2KpnIBXf9A6l_vBbmP",
"_nested": {
"field": "inputs",
"offset": 2
},
"_score": null,
"_source": {
"status": "ENDED",
"lastUpdatedTime": "2020-05-11T00:00:00.000Z"
},
"sort": [
-9223372036854775808
]
}
]
}
}
}

关于elasticsearch - 过滤对象数组的不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62336725/

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