MortenMoulder
MortenMoulder

Reputation: 6646

Elastic NEST won't allow me to create multiple aggregations

By using Kibana, I was able to make some nice graphs I am trying to convert to NEST. The JSON request from Kibana looks like this:

{
    "aggs": {
        "2": {
            "date_histogram": {
                "field": "@timestamp",
                "fixed_interval": "30m",
                "min_doc_count": 1
            },
            "aggs": {
                "1": {
                    "cardinality": {
                        "field": "clientip"
                    }
                }
            }
        }
    }

And this is what my NEST looks like:

query.Aggregations(aggs => aggs
    .DateHistogram("total_count", d => d
        .Field(f => f.Timestamp)
        .FixedInterval("30m")
        .MinimumDocumentCount(1)
    )
    .Cardinality("unique_count", c => c
        .Field(f => f.ClientIp)
    )
)

However, the produced JSON looks like this:

{
    "aggs": {
        "total_count": {
            "date_histogram": {
                "field": "@timestamp",
                "fixed_interval": "30m",
                "min_doc_count": 1
            }
        },
        "unique_count": {
            "cardinality": {
                "field": "clientip"
            }
        }
    }

That kind of makes sense, because that's what I wrote in the code. However, I need to add the cardinality to the date_histogram, but I cannot figure it out. I cannot add an additional .Aggregations() to the .DateHistogram(), because it does not exist.

What am I doing wrong here?

Upvotes: 1

Views: 82

Answers (1)

Val
Val

Reputation: 217334

This should work:

query.Aggregations(aggs => aggs
    .DateHistogram("total_count", d => d
        .Field(f => f.Timestamp)
        .FixedInterval("30m")
        .MinimumDocumentCount(1)
        .Aggregations(childAggs => childAggs
           .Cardinality("unique_count", c => c
             .Field(f => f.ClientIp)
           )
        )
    )
)

Upvotes: 2

Related Questions