# Bucket Script Aggregation - parsing\_exception

**URL:** <https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421>\
**Category:** Kibana\
**Created:** [August 26, 2020, 9:17am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421 "2020-08-26T09:17:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiko](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@shiko](https://discuss.elastic.co/u/shiko)\
**Post date:** [August 26, 2020, 9:17am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421/1 "2020-08-26T09:17:30Z")

</div>

I'm kind of a new user for ES and Kibana.  
I have a question about the _ **Bucket Script Aggregation** _  
I'm trying to do some calculations with ES, the division of 2 queries ( the ratio).

when I send the queries , it's working .  
when I'm using the bucket script aggs to do the devison , it gives me an erorr 🙁

 ![es_q](https://us1.discourse-cdn.com/elastic/original/3X/5/a/5afb51837ae8ea45067dcdb5f0d30b61a1e52aa9.png)

# // `"type": "parsing_exception", "reason": "Unknown key for a START_OBJECT in [news_attention].", "line": 31, "col": 21 `

this is my query : photo attached

---

<div class="post-metadata">

**Author:** ![Nathan\_Reese](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathan_reese/32/84829_2.png) [@Nathan\_Reese](https://discuss.elastic.co/u/Nathan_Reese)\
**Post date:** [August 28, 2020, 1:32pm UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421/2 "2020-08-28T13:32:02Z")

</div>

The problem is in your nesting. `Count_articles_1` and your bucket script agg need to be nested under `aggs` and not siblings to `aggs`

---

<div class="post-metadata">

**Author:** ![shiko](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@shiko](https://discuss.elastic.co/u/shiko)\
**Post date:** [August 29, 2020, 7:16am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421/4 "2020-08-29T07:16:25Z")

</div>

this is my query after the modification you proposed,  
Thanks alot for helping out.

```auto
Blockquote

GET /index/_search
{
  "size": 0,
  "aggs": {
    "count_for_7d": {
      "date_range": {
        "field": "publishDate",
        "format": "yyyy-MM-dd", 
        "ranges": [
          {
            "from":"now-8d",
            "to": "now-1d",
            "key": "count_for_7d"
          }
        ]
      }
    },
  "count_for_1d": {
      "date_range": {
        "field": "publishDate",
        "format": "yyyy-MM-dd", 
        "ranges": [
          {
            "from": "now-1d",
            "to": "now",
            "key": "count_for_1d"
          }
        ]
      }
    }
    ,
  "my_bucket": {
    "bucket_script" :{
      "buckets_path":{
        "count_for_7d" :"count_for_7d",
        "count_for_1d":"count_for_1d",
         "key": "my_bucket"
      },
      "script" : "params.count_for_1d / (params.count_for_1d / 7)"
    }
  }
  }
  
}

> Blockquote

```

---

<div class="post-metadata">

**Author:** ![shiko](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@shiko](https://discuss.elastic.co/u/shiko)\
**Post date:** [September 7, 2020, 9:32am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421/5 "2020-09-07T09:32:02Z")

</div>

Hello,  
It took me some time to have the solution, due to I'm new to ES.

There is the final query that works

```auto
GET /index/_search
{
  "size": 0,
  "aggs": {
    "last_week": {
      "date_range": {
        "field": "publishDate",
        "ranges": [
          {
            "from": "now-8d/d",
            "to": "now"
          }
        ]
      },
      "aggs": {
        "count_for_7d": {
          "value_count": {
            "field": "id"
          }
        },
        "count_for_1d": {
          "filter": {
            "range": {
              "publishDate": {
                "gte": "now-1d/d",
                "lte": "now"
              }
            }
          },
          "aggs": {
            "count_1d": {
              "value_count": {
                "field": "id"
              }
            }
          }
        },
        "My_Bucket": {
          "bucket_script": {
            "buckets_path": {
              "count_7d": "count_for_7d",
              "count_1d": "count_for_1d>count_1d"
            },
            "script": "(params.count_1d / (params.count_7d - params.count_1d / 7))"
          }
        }
      }
    }
  }
}

```

Cheers

---

<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:** [October 5, 2020, 9:32am UTC](https://discuss.elastic.co/t/bucket-script-aggregation-parsing-exception/246421/6 "2020-10-05T09:32:22Z")

</div>

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