# Bucket selector null pointer exception

**URL:** <https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543>\
**Category:** Elasticsearch\
**Created:** [July 1, 2021, 11:18am UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543 "2021-07-01T11:18:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Levy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roy_levy/32/61613_2.png) [@Roy\_Levy](https://discuss.elastic.co/u/Roy_Levy)\
**Post date:** [July 1, 2021, 11:18am UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/1 "2021-07-01T11:18:41Z")

</div>

Hey,

I'm trying to get an overall avg vs grouped avg aggregation in DevTools:

```auto
GET index1/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "my_measurement"
          }
        },
        {
          "exists": {
            "field": "my_measurement_weight"
          }
        }
      ]
    }
  }, 
  "aggs": {
    "location_agg": {
      "terms": {
        "field": "key.location.keyword"
      },
      "aggs": {
        "overall_avg_by_location": {
          "weighted_avg": {
            "value": {"field": "my_measurement"},
            "weight": {"field": "my_measurement_weight"}
          }
        },
        "device_location_agg": {
          "terms": {
            "field": "key.device.keyword"
          },
          "aggs": {
            "overall_avg_by_location_and_device": {
              "weighted_avg": {
                "value": {"field": "my_measurement"},
                "weight": {"field": "my_measurement_weight"}
              }
            }
          }
        },
        "KeepBadDevices": {
          "bucket_selector": {
            "buckets_path": {
              "overall_avg_by_location": "overall_avg_by_location.value",
              "overall_avg_by_location_and_device": "device_location_agg.value"
            },
            "script": "params.overall_avg_by_location > 20"
          }
        }
      }
    }
  }
}

```

When executing the request I'm getting the exception:

```auto
"type" : "illegal_argument_exception",
      "reason" : "No aggregation found for path [device_location_agg.overall_avg_by_location_and_device.value]

```

Is there a way to access the `overall_avg_by_location_and_device` nested aggregation so I won't be getting this error?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 1, 2021, 2:16pm UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/2 "2021-07-01T14:16:14Z")

</div>

Take a look at the `buckets_path` syntax at [Pipeline aggregations | Elasticsearch Guide [7.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/search-aggregations-pipeline.html#buckets-path-syntax) - it's a little different notation using `>` instead of `.` at some positions (including the reasoning for that).

A small reproducible example with a few sample documents might help as well, otherwise this is mostly guessing 🙂

---

<div class="post-metadata">

**Author:** ![Roy\_Levy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roy_levy/32/61613_2.png) [@Roy\_Levy](https://discuss.elastic.co/u/Roy_Levy)\
**Post date:** [July 1, 2021, 2:29pm UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/3 "2021-07-01T14:29:29Z")

</div>

Ok, that's great to know about this way of accessing sibling aggs! I've changed the KeepBadDevices to:

```auto
"KeepBadDevices": {
          "bucket_selector": {
            "buckets_path": {
              "overall_avg_by_location": "overall_avg_by_location.value",
              "overall_avg_by_location_and_device": "device_location_agg>overall_avg_by_location_and_device"
            },
            "script": "params.overall_avg_by_location > 20"
          }
        }

```

And it access it successfully, but now I'm getting the following error for it:

```auto
"aggregation_execution_exception",
      "reason" : "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [device_location_agg]"

```

It seems to be a list of Objects. Is there any way to access it in this query? Or unpack the object?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 1, 2021, 6:40pm UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/4 "2021-07-01T18:40:46Z")

</div>

again a reproducible example would be of great use, because this would explain the document structure and the aggs structure much better than any error message could 😉

---

<div class="post-metadata">

**Author:** ![Roy\_Levy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roy_levy/32/61613_2.png) [@Roy\_Levy](https://discuss.elastic.co/u/Roy_Levy)\
**Post date:** [July 3, 2021, 5:57am UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/5 "2021-07-03T05:57:12Z")

</div>

Hey, here is an example for the documents in the index which I'm trying to aggregate:

```auto
{
  "_index": "index1",
  "_type": "_doc",
  "_id": "abc",
  "_version": 1,
  "_score": null,
  "_source": {
    "key": {
      "location": "LA",
      "device": "IPHONE"
    },
    "my_measurement": 0.8,
    "my_measurement_weight": 352
},
{
  "_index": "index1",
  "_type": "_doc",
  "_id": "abd",
  "_version": 1,
  "_score": null,
  "_source": {
    "key": {
      "location": "UA",
      "device": "TABLET"
    },
    "my_measurement": 0.7,
    "my_measurement_weight": 276
}

```

Hope this explains the situation better 😄  
Again, the problem is that I'm getting an object type instead single value. Is there anything I'm missing here?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 6, 2021, 4:01pm UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/6 "2021-07-06T16:01:27Z")

</div>

You are trying to access a value within another `buckets` array from the `device_location_agg.buckets` field. That one is not uniquely addressed...

---

<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:** [August 3, 2021, 4:02pm UTC](https://discuss.elastic.co/t/bucket-selector-null-pointer-exception/277543/7 "2021-08-03T16:02:12Z")

</div>

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