0

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"

anekix
  • 2,393
  • 2
  • 30
  • 57

1 Answers1

2

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"
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • i tried this `{ "size": 0, "aggs": { "total-new-sessions": { "sum": { "script": "doc['ga:percentNewSessions'].value/100*doc['ga:sessions'].value" } }, "total-sessions": { "sum": { "field": "ga:sessions" } }, "bucket_script": { "buckets_path": { "total_new": "total-new-sessions", "total": "total-sessions" }, "script": "total_new / total*100" } } }` but i am getting error: `"reason": "Could not find aggregator type [buckets_path] in [bucket_script]` – anekix Aug 16 '16 at 07:01
  • Which version of ES are you running? – Val Aug 16 '16 at 07:02
  • my ES version is 2.2.1 – anekix Aug 16 '16 at 07:10
  • there was small typing mistkae in my old query : The error is :` "reason": "Invalid pipeline aggregation named [sessions] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"` – anekix Aug 16 '16 at 07:22
  • I've updated my answer and it should work. Note that you need a parent multi-bucket aggregation in order for this to work, so I've added a yearly date_histogram parent aggregation on the timestamp field. – Val Aug 16 '16 at 07:29
  • Thanks a lot it worked . can you please explain what was causing the issue or point me to the right documentation. Thanks a lot for help – anekix Aug 16 '16 at 07:32
  • In my initial answer, I didn't have a parent multi-bucket aggregation and bucket_script needs one in order to work. – Val Aug 16 '16 at 07:32