Skip to content

Looping Named Buckets #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RobinRieger opened this issue Jan 19, 2016 · 5 comments
Closed

Looping Named Buckets #1742

RobinRieger opened this issue Jan 19, 2016 · 5 comments

Comments

@RobinRieger
Copy link
Contributor

Is there a way in 2.0 to loop over named buckets (aggregations)?

In 1.7 I was able to do the following

foreach (var row in ((FiltersBucket) searchResultForTimePeriod.Aggregations["repeatBuyers"]).Aggregations.Aggregations) {}

The search looked something like this:

Filters
    ("repeatBuyers",
        fil => fil.Filters
            (f => f.Name(((int) RepeatBuyersGrouping.One).ToString())
                .Range
                    (ra => ra.OnField(of => of.TotalOrders)
                        .LowerOrEquals(1)),
            f => f.Name(((int) RepeatBuyersGrouping.TwoFive).ToString())
                .Range
                    (ra => ra.OnField(of => of.TotalOrders)
                        .Greater(1)
                        .LowerOrEquals(5)),
...

This way I could use the bucket name and the value in the output. I.e. The name of each bucket is a integer and it maps to an enum which I can then use to get the proper 'display' text' for the output.

With 2.0, I can't seem to loop the 'NamedBucket'. Do I need to call them separately now or was this accidentally dropped?

@russcam
Copy link
Contributor

russcam commented Jan 21, 2016

Here's how you would do it in NEST 2.x

var names = new[] {
    ((int)RepeatBuyersGrouping.One).ToString(),
    ((int)RepeatBuyersGrouping.TwoFive).ToString()
};

var response = client.Search<Example>(s => s
    .Aggregations(aggs => aggs
        .Filters("repeatBuyers", fil => fil
            .NamedFilters(f => f
                .Filter(names[0], q => q
                    .Range(ra => ra
                        .Field(of => of.TotalOrders)
                        .LessThanOrEquals(1)))
                .Filter(names[1], q => q
                    .Range(ra => ra
                        .Field(of => of.TotalOrders)
                        .GreaterThan(1)
                        .LessThanOrEquals(5))
                )
            )
        )
    )
);

var filters = response.Aggs.Filters("repeatBuyers");
foreach (var name in names)
{
    var filter = filters.NamedBucket(name);
    // do something with filter
}

The Aggegations property on FiltersBucket is private - @Mpdreamz / @gmarz, is this an intentional change?

@RobinRieger
Copy link
Contributor Author

To clarify, what you are suggesting is to no longer loop the buckets as such but the array I have that I used to make the different buckets. I tried your code and it seems to work (will check more over coming days)

I feel like the Aggegations/Items being a public property could be useful. I can't see any obvious reason to restrict them..... 😕... definitely looking forward to hearing more on this.

Thanks!

@RobinRieger
Copy link
Contributor Author

As a quick follow up, I wanted to test making these public, so I compiled the repo locally and changed just the FiltersBucket setting the Aggregations and Items to be public. My code then all compiled again and seems to be working as expected.

@russcam
Copy link
Contributor

russcam commented Jan 21, 2016

@RobinRieger cool! Feel free to submit a PR with the change 👍

@gmarz
Copy link
Contributor

gmarz commented Jan 25, 2016

Closed via #1752

@gmarz gmarz closed this as completed Jan 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants