【发布时间】:2020-10-01 20:31:14
【问题描述】:
我有一个嵌套字段,我想使用脚本总结该嵌套对象的值。我不能使用聚合,因为这个求和是为范围过滤完成的。
我的映射:
{
"_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的价格。
【问题讨论】:
标签: elasticsearch