是否有一个“问题”;“好”;从ES嵌套聚合中获取标准化数据行的方法?

是否有一个“问题”;“好”;从ES嵌套聚合中获取标准化数据行的方法?,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,Elasticsearch嵌套聚合允许您按多个字段进行有效分组。但它返回的是为分组所依据的每个字段嵌套的桶 我需要的是每个组组合的一个对象数组 我的问题是: { index : 'stats', type : 'click', size : 0, body : { aggs : { publisher : { terms : { field : 'publisherData.id' },

Elasticsearch嵌套聚合允许您按多个字段进行有效分组。但它返回的是为分组所依据的每个字段嵌套的桶

我需要的是每个组组合的一个对象数组

我的问题是:

{
  index : 'stats',
  type  : 'click',
  size  : 0,
  body  : {
    aggs : {
      publisher : {
        terms : {
          field : 'publisherData.id'
        },
        aggs  : {
          advertiser : {
            terms : {
              field : 'advertiserData.id'
            },
            aggs  : {
              country : {
                terms : {
                  field : 'request.location.country.iso_code'
                },
                aggs  : {
                  revenue : {
                    sum : {
                      field : 'revenueData.data.USD'
                    }
                  },
                  cost    : {
                    sum : {
                      field : 'costData.data.USD'
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
结果,每个字段仅限一个条目。通常会有更多的嵌套字段,因此所有嵌套字段的组合都必须映射到一个数组,以便在表中显示

{
  "took": 562,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4812178,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "publisher": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 3114671,
      "buckets": [
        {
          "key": 4,
          "doc_count": 1697507,
          "advertiser": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 555390,
            "buckets": [
              {
                "key": 5,
                "doc_count": 1142117,
                "country": {
                  "doc_count_error_upper_bound": 13807,
                  "sum_other_doc_count": 544585,
                  "buckets": [
                    {
                      "key": "us",
                      "doc_count": 424137,
                      "revenue": {
                        "value": 772282
                      },
                      "cost": {
                        "value": 53698.84903321415
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
我需要什么(通常这里有多个对象,每个嵌套字段组合一个):

从上面的嵌套结构或者更好的,如果可能的话,从elasticsearch本身获得这个结果的最佳方法是什么


非常感谢您的帮助。

在纯Javascript中,您可以使用迭代和递归方法,但我建议使用ES的某些功能来获得想要的结果

函数获取值(对象){
功能iter(o,p){
var-add=false;
Object.keys(o).forEach(函数(k){
如果(['key','doc_count'].indexOf(k)!=-1){
返回;
}
if(Array.isArray(o[k].bucket)){
o[k].bucket.forEach(函数(a){
iter(a,p.concat([[k,a.key]]);
});
返回;
}
加法=真;
p、 push([k,o[k].value]);
});
add&&result.push(Object.assign({},…p.map(a=>({[a[0]]:a[1]}));
}
var结果=[];
iter(object.aggregations,[]);
返回结果;
}
var data={take:562,timed\u out:false,{u shards:{total:5,successful:5,failed:0},hits:{total:4812178,max\u score:0,hits:[]},聚合:{publisher:{doc count\u error\u上限:0,sum\u other\u doc count:3114671,bucket:[{key:4,doc count:1697507,广告商:{doc_count_error_上限:0,sum_other_doc_count:555390,bucket:[{key:5,doc_count:1142117,country:{doc_count error_上限:13807,sum_other_doc_count:544585,bucket:[{key:“us”,doc count:424137,收入:{value:772282},成本:{value:53698.84903321415}}}}};

console.log(getValues(data));
这很有效。你知道有没有办法直接从elasticsearch获取此输出?@Franci,对不起,我不知道。
[{
    publisher:4,
    advertiser:5,
    country:'us',
    cost:53698.84903321415,
    revenue:772282
}]