elasticsearch - ElasticSearch总结嵌套对象字段

标签 elasticsearch

我有一个嵌套字段,我想使用脚本总结该嵌套对象的值。我不能使用聚合,因为此求和已完成范围过滤。
我的映射:

    {
      "_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/

相关文章:

hadoop - 自动从Hive插入Elasticsearch

elasticsearch - 如何获取达到某个最小日期的不同值的计数

.net - ElasticSearch NEST 检查空值

api - 有没有一种方法可以将具有特定_id的文档索引到Elasticsearch中?

elasticsearch - Elasticsearch-使用 “tags”索引发现给定字符串中的所有标签

lucene - elasticsearch/lucene 高亮

java - ElasticSearch搜索字段值不返回

elasticsearch - 对top_hits聚合的总和

geolocation - ElasticSearch 中的地理排序和距离计算不起作用

python - 如何为 Python Elasticsearch mSearch 创建请求主体