Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation in ElasticSearch (Nest)

I'm quite new to elasticsearch and I have yet to find a question specifically about this. If it is already answered, I apologize in advanced and I hope you can point me to the correct direction.

I was looking for a way to implement the following in NEST:

"aggs" : {
    "fieldA" : {
        "terms" : {
            "field" : "fieldA"

        }
    },

    "fieldB" : {
        "terms" : {
            "field" : "fieldB"

        }
    }
}

I have tried this:

.Aggregations(q => q.Terms("fieldA", r => r.Field(s => s.fieldA)) && q.Terms("fieldB", r => r.Field(s => s.fieldB)))

and this:

.Aggregations(q => q.Terms("fieldA", r => r.Field(s => s.fieldA)))
.Aggregations(q => q.Terms("fieldB", r => r.Field(s => s.fieldB)))

Which both failed to work. Am I missing something else?

like image 721
Sum NL Avatar asked Jul 25 '14 10:07

Sum NL


People also ask

What is nested aggregation?

Nested aggregationeditA special single bucket aggregation that enables aggregating nested documents. For example, lets say we have an index of products, and each product holds the list of resellers - each having its own price for the product.

Is Elasticsearch good for aggregation?

Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.

What is aggregation query Elasticsearch?

Elasticsearch organizes aggregations into three categories: Metric aggregations that calculate metrics, such as a sum or average, from field values. Bucket aggregations that group documents into buckets, also called bins, based on field values, ranges, or other criteria.


1 Answers

You can specify multiple aggregations like so:

.Aggregations(a => a
    .Terms("fieldA", t => t.Field(s => s.FieldA))
    .Terms("fieldB", t => t.Field(s => s.FieldB))
)

Each aggregation descriptor, internally, adds itself to a dictionary (using the agg name as a key) and then returns itself so you can continually add more.

Apologies for the lack of documentation around aggs in NEST. We are in the process of revamping the docs and we'll be sure to include an example of the above use case.

like image 161
Greg Marzouka Avatar answered Oct 19 '22 02:10

Greg Marzouka