为Elasticsearch查询结果进行转换

{
   'key': factoryA,
   'buckets': [
       {
          'key': 'productX'
       }
    ]

},
{
   'key': factoryB,
   'buckets': [
       {
          'key': 'productX'
       },
       {
          'key': 'productY'
       }
    ]

}

我想要一个列表,该列表的项是工厂聚合及其嵌套聚合产品的组合:
[
   {'factory': 'factoryA', 'product': 'productX'},
   {'factory': 'factoryB', 'product': 'productX'},
   {'factory': 'factoryB', 'product': 'productY'},
]

关于无痛脚本的任何建议吗?先感谢您。或在任何地方,我都能找到全面的无痛指南。没有医生的无痛是完全痛苦的。

最佳答案

我认为以下查询是您要查找的内容:

询问

POST <your_index_name>/_search
{
   "size":0,
   "aggs":{
      "myagg":{
         "terms":{
            "script":{
               "source":"'factory: ' + doc['factory.keyword'].value + params.param + 'product: ' + doc['product.keyword'].value",
               "lang":"painless",
               "params":{
                  "param":", "
               }
            },
            "order":{
               "_term":"asc"
            }
         }
      }
   }
}

查询回应
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "myagg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "factory: factory A, product: product A",
          "doc_count": 1
        },
        {
          "key": "factory: factory B, product: product B",
          "doc_count": 1
        }
      ]
    }
  }
}

注意,我假设factoryproduct字段均为keyword类型

让我知道是否有帮助!

10-06 06:11