Skip to content

Bucket aggregations along with results #78

@caufab

Description

@caufab

My goal

I need to retrieve the results of a search, more or less complex (could have search() and various where() filters), along with bucket aggregations to populate facets in a client.

What I found
Going a little deep in the code of this package I noticed that when I apply one or more buckets, they populate a $bucketAggregation field and when compileSelect() runs it resets size parameter in body to 0, causing the query to return with empty hits.
Elasticsearch can actually handle both results and bucket aggregations, so my question is: was this made for a performance or other specific reason?

Temporary solution
I'm currently bypassing this making two identical queries, one for the results and one with added buckets for the aggregations, which means that each request Laravel receives, it doubles them to ES. But I'm not sure this is the best solution.

====================

Second related issue
I'm also having trouble retrieving buckets aggregation data in the second query. What I did was:

$builder = Product::query();

// various search and filters
$builder->search( .... );
$builder->where( .... );

// add buckets
$options = [
        'field' => 'my_field',
        'size' => 99,
    ];

$builder->bucket('my_field', 'terms', $options);

$aggregations = $builder->getAggregationResults();

Now, $aggregation seems to be of type ElasticCollection but each item appear to be of the model type, so in my example Product, which I think is not right. From the dump I can see it has the values and doc_count in meta but I'm finding it hard to retrieve them.

Temporary (or final) solution
I'm not sure it is an actual issue, but since I'm interested in the whole data ES returns inside aggregations field I'm getting it with

$aggregations = $builder->getRaw()['aggregations'];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions