gpt4 book ai didi

elasticsearch - 在elasticsearch中基于嵌套对象数组中的一个字段对父类型进行排序

转载 作者:行者123 更新时间:2023-12-03 00:41:19 31 4
gpt4 key购买 nike

我的索引中包含以下映射:

{
"testIndex": {
"mappings": {
"type1": {
"properties": {
"text": {
"type": "string"
},
"time_views": {
"properties": {
"timestamp": {
"type": "long"
},
"views": {
"type": "integer"
}
}
}
}
}
}
}
}

“time_views”实际上是一个数组,但内部属性不是数组。

我想根据每个type1记录的“views”属性的最大值对我的type1记录进行排序。我阅读了Elasticsearch排序文档,它为用例提供了基于单个嵌套对象的字段(单个或数组)进行排序的解决方案。但是我想要的是不同的。我想为每个文档选择“ View ”的最大值,然后根据这些值对文档进行排序

我做了这个json查询
{
"size": 10,
"query": {
"range": {
"timeStamp": {
"gte": 1468852617347,
"lte": 1468939017347
}
}
},
"from": 0,
"sort": [
{
"time_views.views": {
"mode": "max",
"nested_path": "time_views",
"order": "desc"
}
}
]
}

但是我得到这个错误
{
"error": {
"phase": "query",
"failed_shards": [
{
"node": "n4rxRCOuSBaGT5xZoa0bHQ",
"reason": {
"reason": "[nested] nested object under path [time_views] is not of nested type",
"col": 136,
"line": 1,
"index": "data",
"type": "query_parsing_exception"
},
"index": "data",
"shard": 0
}
],
"reason": "all shards failed",
"grouped": true,
"type": "search_phase_execution_exception",
"root_cause": [
{
"reason": "[nested] nested object under path [time_views] is not of nested type",
"col": 136,
"line": 1,
"index": "data",
"type": "query_parsing_exception"
}
]
},
"status": 400
}

正如我上面提到的,time_views是一个数组,我猜这个错误是由于这个原因。
即使我不能使用基于数组字段功能的排序,因为“time_views”不是原始类型。
我认为我最后的机会是通过脚本编写自定义排序,但是我不知道如何。
请告诉我我的错误,是否有可能实现我想要的目标,否则给我一个简单的脚本示例。

tnx :)

最佳答案

该错误消息在很大程度上解释了查询的问题。实际上,问题出在映射上。而且我认为您打算使用nested字段,因为您使用的是嵌套查询。

您只需要将time_views字段设置为nested即可:

  "mappings": {
"type1": {
"properties": {
"text": {
"type": "string"
},
"time_views": {
"type": "nested",
"properties": {
"timestamp": {
"type": "long"
},
"views": {
"type": "integer"
}
}
}
}
}
}

关于elasticsearch - 在elasticsearch中基于嵌套对象数组中的一个字段对父类型进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454742/

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