# Variable\_width\_histogram cannot be nested in sampler

**URL:** <https://discuss.elastic.co/t/variable-width-histogram-cannot-be-nested-in-sampler/361822>\
**Category:** Elasticsearch\
**Tags:** aggregations\
**Created:** [June 21, 2024, 6:26am UTC](https://discuss.elastic.co/t/variable-width-histogram-cannot-be-nested-in-sampler/361822 "2024-06-21T06:26:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jewell](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@jewell](https://discuss.elastic.co/u/jewell)\
**Post date:** [June 21, 2024, 6:26am UTC](https://discuss.elastic.co/t/variable-width-histogram-cannot-be-nested-in-sampler/361822/1 "2024-06-21T06:26:06Z")

</div>

Greetings

The variable\_width\_histogram works fine as the first aggregation

```auto
GET ntsp_dbas_bfg/_search
{
  "query": {
    "term": {
      "name": {
        "value": "Valsartanic acid"
      }
    }
  },
  "size": 0,
  "aggs": {
    "ms2": {
      "nested": {
        "path": "ms2"
      },
      "aggs": {
        "averaged": {
          "variable_width_histogram": {
            "field": "ms2.mz",
            "buckets": 8000
          },
          "aggs": {
            "bucket_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "numBuckets": "_count"
                },
                "script": "params.numBuckets > 1"
              }
            }
          }
        }
      }
    }
  }
}

```

But if I use the `sampler` aggregation to limit the number of buckets to aggregate, I get an error.

```auto
GET ntsp_dbas_upb/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "filter_docs": {
      "sampler": {
        "shard_size": 100
      },
      "aggs": {
        "ms2": {
          "nested": {
            "path": "ms2"
          },
          "aggs": {
            "averaged": {
              "variable_width_histogram": {
                "field": "ms2.mz",
                "buckets": 8000
              },
              "aggs": {
                "bucket_filter": {
                  "bucket_selector": {
                    "buckets_path": {
                      "numBuckets": "_count"
                    },
                    "script": "params.numBuckets > 1"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

The error is:

```auto
"type": "illegal_argument_exception",
"reason": "[variable_width_histogram] cannot be nested inside an aggregation that collects more than a single bucket."

```

Is there an error in the way I am using the aggregation? Or maybe the variable\_width\_histogram is throwing an error when it doesn't need to?

Any advice or thoughts would be much appreciated

Best regards

---

<div class="post-metadata">

**Author:** ![jewell](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@jewell](https://discuss.elastic.co/u/jewell)\
**Post date:** [June 24, 2024, 9:39am UTC](https://discuss.elastic.co/t/variable-width-histogram-cannot-be-nested-in-sampler/361822/2 "2024-06-24T09:39:24Z")

</div>

Greetings again,

the `histogram` aggregation works when nested inside a `terms` aggregation. So I've gotten around the problem that way. It seems as though `variable_width_histogram` throws a (for me) unexpected error when nested inside other aggregations.

```auto
GET ntsp_dbas*/_search
{
  "query": {
    "term": {
      "name": {
        "value": "Caprolactam"
      }
    }
  },
  "size": 0,
  "aggs": {
    "ms2": {
      "nested": {
        "path": "ms2"
      },
      "aggs": {
        "averaged": {
          "histogram": {
            "field": "ms2.mz",
            "interval": 0.015,
            "min_doc_count": 2
          },
          "aggs": {
            "avgmz": {
              "avg": {
                "field": "ms2.mz"
              }
            },
            "sumint": {
              "sum": {
                "field": "ms2.int"
              }
            }
          }
        }
      }
    }
  }
}

```
