我有一个嵌套字段,我想使用脚本总结该嵌套对象的值。我不能使用聚合,因为此求和已完成范围过滤。
我的映射:
{
"_doc" : {,
"properties" : {
"searchPrices" : {
"type" : "nested",
"properties" : {
"price" : {
"type" : "double"
},
"type" : {
"type" : "keyword"
}
}
}
}
}
}
我试过的查询:{
"size": 100,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "searchPrices",
"query": {
"bool": {
"must": [
{
"script": {
"script": """
int sum = 0;
for (obj in doc['searchPrices.price']) {
sum = sum + obj;
}
sum >= 200;"""
}
},
{
"term": {
"searchPrices.type": "recurringPrice"
}
}
]
}
}
}
}
]
}
}
}
问题在于该脚本仅选择第一个嵌套文档,而不管文档中有多个搜索价格的事实。样本文档:
{
"id" : "v2",
"searchPrices" : [
{
"price" : 2000,
"type" : "recurringPrice",
},
{
"price" : 200,
"type" : "recurringPrice",
"conditions" : [
"addon1"
]
},
{
"price" : 400,
"type" : "recurringPrice",
"conditions" : [
"addon2"
]
}
]
}
因此,与其以2600作为价格,不如以2000作为价格。
最佳答案
不幸的是,似乎没有痛苦的脚本是不可能的。
根据Elastic Team Member答复的Help for painless iterate nested fields文档,在查询时使用无痛脚本只能一次访问一个嵌套对象。
因此,您遭受的情况-仅考虑2000作为价格-是自然的。
关于elasticsearch - ElasticSearch总结嵌套对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64140179/