0

我有一个具有以下结构的文件:

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

4

1 回答 1

2

您可以使用bucket_script聚合来实现此目的:

{
  "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"
          }
        }
      }
    }
  }
}
于 2016-08-16T06:44:58.270 回答