Skip to content

Nested bucket aggregations not applied when using callback in bucketAggregation() #90

@shoxrux1996

Description

@shoxrux1996

I'm trying to create a nested bucket aggregation using a callback in bucket(), but the nested aggregation never appears in the final DSL:

$regions = Enrollment::viaIndex()
    ->where('status', EnrollmentStatus::Studying)
    ->bucket(
        'regions',
        'terms',
        ['field' => 'student.region.id'],
        fn (Builder $builder) => $builder->bucket('student_count', 'terms', ['field' => 'student.id'])
    );

dd($regions->toSql());

Actual output:

"aggs": {
  "regions": {
    "terms": {
      "field": "student.region.id",
      "size": 1000
    }
  }
}

Expected output:

"aggs": {
  "regions": {
    "terms": { "field": "student.region.id" },
    "aggs": {
      "student_count": {
        "terms": { "field": "student.id" }
      }
    }
  }
}

Question
Is this the correct way to define nested bucket aggregations using a callback?
Or is there a different recommended approach for creating nested aggregations inside a bucket() call?
Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions