计算弹性搜索聚合的结果
compute over results of elasticsearch aggregations
我有一个结构如下的文档:
{
"ga:bounces": "1",
"timestamp": "20160811",
"viewId": "125287857",
"ga:percentNewSessions": "100.0",
"ga:bounceRate": "100.0",
"ga:avgSessionDuration": "0.0",
"ga:sessions": "1",
"user": "xxcgf",
"ga:pageviewsPerSession": "1.0",
"webPropertyId": "UA-80489737-1",
"ga:pageviews": "1",
"dimension": "date",
"ga:users": "1",
"accountId": "80489737"
}
我正在使用此查询应用两个聚合:
{
"size": 0,
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['percentNewSessions'].value/100*doc['sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
}
}
}
这就是我得到的输出,这正是我想要的:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 32,
"max_score": 0,
"hits": [ ]
},
"aggregations": {
"total-new-sessions": {
"value": 386.0000003814697
},
"total-sessions": {
"value": 516
}
}
}
现在我想要的是出于某种原因将两个聚合的输出分开。在上面的查询中我应该怎么做最终输出是我唯一想要的。
更新:
我尝试使用此查询:
{
"size": 0,
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['ga:percentNewSessions'].value/100*doc['ga:sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
},
"sessions": {
"bucket_script": {
"buckets_path": {
"total_new": "total-new-sessions",
"total": "total-sessions"
},
"script": "total_new / total"
}
}
}
}
但是出现这个错误:"reason": "Invalid pipeline aggregation named [sessions] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"
您可以使用 bucket_script
aggregation 来实现:
{
"size": 0,
"aggs": {
"all": {
"date_histogram": {
"field": "timestamp",
"interval": "year"
},
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['percentNewSessions'].value/100*doc['sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
},
"ratio": {
"bucket_script": {
"buckets_path": {
"total_new": "total-new-sessions",
"total": "total-sessions"
},
"script": "total_new / total"
}
}
}
}
}
}
我有一个结构如下的文档:
{
"ga:bounces": "1",
"timestamp": "20160811",
"viewId": "125287857",
"ga:percentNewSessions": "100.0",
"ga:bounceRate": "100.0",
"ga:avgSessionDuration": "0.0",
"ga:sessions": "1",
"user": "xxcgf",
"ga:pageviewsPerSession": "1.0",
"webPropertyId": "UA-80489737-1",
"ga:pageviews": "1",
"dimension": "date",
"ga:users": "1",
"accountId": "80489737"
}
我正在使用此查询应用两个聚合:
{
"size": 0,
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['percentNewSessions'].value/100*doc['sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
}
}
}
这就是我得到的输出,这正是我想要的:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 32,
"max_score": 0,
"hits": [ ]
},
"aggregations": {
"total-new-sessions": {
"value": 386.0000003814697
},
"total-sessions": {
"value": 516
}
}
}
现在我想要的是出于某种原因将两个聚合的输出分开。在上面的查询中我应该怎么做最终输出是我唯一想要的。
更新: 我尝试使用此查询:
{
"size": 0,
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['ga:percentNewSessions'].value/100*doc['ga:sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
},
"sessions": {
"bucket_script": {
"buckets_path": {
"total_new": "total-new-sessions",
"total": "total-sessions"
},
"script": "total_new / total"
}
}
}
}
但是出现这个错误:"reason": "Invalid pipeline aggregation named [sessions] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"
您可以使用 bucket_script
aggregation 来实现:
{
"size": 0,
"aggs": {
"all": {
"date_histogram": {
"field": "timestamp",
"interval": "year"
},
"aggs": {
"total-new-sessions": {
"sum": {
"script": "doc['percentNewSessions'].value/100*doc['sessions'].value"
}
},
"total-sessions": {
"sum": {
"field": "ga:sessions"
}
},
"ratio": {
"bucket_script": {
"buckets_path": {
"total_new": "total-new-sessions",
"total": "total-sessions"
},
"script": "total_new / total"
}
}
}
}
}
}