Search code examples
elasticsearchelasticsearch-aggregation

compute over results of elasticsearch aggregations


i have a documents with following structure:

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

i am applying two aggregations using this query:

{
  "size": 0,
  "aggs": {
    "total-new-sessions": {
      "sum": {
        "script": "doc['percentNewSessions'].value/100*doc['sessions'].value"
      }
    },
    "total-sessions": {
      "sum": {
        "field": "ga:sessions"
      }
    }
  }
}

and this is the ouput i am getting which is exactly what i want:

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

}

Now what i want is to divide the output of two aggregations together for some reason. how should i do that in the above query the final output is the only one that i want.

UPDATE: i tried using this query:

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

But getting this error :"reason": "Invalid pipeline aggregation named [sessions] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"


Solution

  • You can use a bucket_script aggregation to achieve this:

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