# Weighted Average and aggregations

**URL:** <https://discuss.elastic.co/t/weighted-average-and-aggregations/67755>\
**Category:** Elasticsearch\
**Created:** [December 1, 2016, 11:56am UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755 "2016-12-01T11:56:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 1, 2016, 11:56am UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/1 "2016-12-01T11:56:46Z")

</div>

Hi,

I did query, aggregations and avg on ES-5.0.0.

```
GET titubb56k/ihale/_search 
{"size":0,
  "query": {"match": { "UrunNo": "99" } },
  "aggs":{
      "group_by_tarih":{
        "terms":{
          "field": "AlimTarihi",
          "order":{
            "Birim_Fiyat_ort":"asc"
          }},
          "aggs" : {
            "Birim_Fiyat_ort" : { "avg" : { "field": "BirimFiyat"}}
          } } }}

```

It works true.

Now I want to do weighted average and aggregations.

for example:

my fields:

1. Adet
2. BirimFiyat
3. UrunNo

weigheted average formula:

> sum(Adet\*BirimFiyat)/sum(Adet).

It is possible? How can I do?

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 1, 2016, 12:22pm UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/2 "2016-12-01T12:22:36Z")

</div>

Actually I did something:

```
GET titubb56k/ihale/_search 
{
"size":0,
  "query": {"match": { "UrunNo": "99" } },
  "aggs":{
      "group_by_tarih":{
        "terms":{ "field": "AlimTarihi",
        "order":{
            "Birim_Fiyat_agirlik_ort":"asc"
          }},
          "aggs" : {
         "Birim_Fiyat_agirlik_ort": {"avg" : { "script" : "[value: doc['BirimFiyat'].value, weight: doc['Adet'].value]" }}
          } } }}

```

but I have error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "Unexpected token VALUE_STRING [script] in [Birim_Fiyat_agirlik_ort].",
        "line": 11,
        "col": 72
      }
    ],
    "type": "parsing_exception",
    "reason": "Unexpected token VALUE_STRING [script] in [Birim_Fiyat_agirlik_ort].",
    "line": 11,
    "col": 72
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 1, 2016, 1:04pm UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/3 "2016-12-01T13:04:18Z")

</div>

Have you seen the docs for using scripts and the avg aggregation?

[https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-aggregations-metrics-avg-aggregation.html#\_script](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-aggregations-metrics-avg-aggregation.html#_script)

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 1, 2016, 2:50pm UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/6 "2016-12-01T14:50:46Z")

</div>

The context of a script is a single document. It is passed the current doc and it is expected to return a value for that single document. It could for example subtract a "startTime" and "endTime" property held on a document to derive a duration value

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 4, 2016, 4:18pm UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/8 "2016-12-04T16:18:53Z")

</div>

I tried other things:

```
GET titubb56k/ihale/_search 
{
  "aggs": {
    "myAggr": {
      "terms": {
        "field": "UrunNo",
        "order": { "weightedAvg": "desc"}
      },  
      "aggs": {
          "weightedAvg": { "avg" : { "script" : "[values: doc['BirimFiyat'].value, weights: doc['Adet'].value]" }}
       } } } }

```

But I have error:

```
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "Unexpected token VALUE_STRING [script] in [weightedAvg].",
        "line": 9,
        "col": 49
      }
    ],
    "type": "parsing_exception",
    "reason": "Unexpected token VALUE_STRING [script] in [weightedAvg].",
    "line": 9,
    "col": 49
  },
  "status": 400
}

```

I don't understand.  
Can you help me please?

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 5, 2016, 12:26pm UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/9 "2016-12-05T12:26:51Z")

</div>

What is the problem? Or Is weighted average possible on ES-5.0.0 ? How it is done if possible?

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 7, 2016, 10:46am UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/10 "2016-12-07T10:46:04Z")

</div>

I did.

```
POST ortalama/avg/_search
{
  "size": 3,
  "aggs": {
    "gunluk_satis": {
      "terms": {
        "field": "IdareKodu"
      },
      "aggs": {
        "pay": {
          "sum": {
            "script": {
              "lang": "expression",
              "inline": "doc['BirimFiyat'] * doc['Adet']"
            }
          }
        },
        "payda": {
          "sum": {
            "script": {
              "lang": "expression",
              "inline": "doc['Adet']"
            }
          }
        },
        "sonuc": {
                    "bucket_script": {
                        "buckets_path": {
                          "Pay": "pay",
                          "Payda": "payda"
                        },
                        "script": "params.Pay / params.Payda"
                    }
                }
      }
    }
  }
}

```

that worked.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 4, 2017, 10:46am UTC](https://discuss.elastic.co/t/weighted-average-and-aggregations/67755/11 "2017-01-04T10:46:32Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
