Nisar Ahmad
Nisar Ahmad

Reputation: 123

how to convert elastic query aggregate filter into nest query in .Net core

How can I convert this Elastic search query into nest query. the query is given Bellow . GET winlogbeat-6.6.0*/_search?size=0

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "success ": {
      "filter": {
        "term": {
          "event_id": 4624 
        }
      }

    },
    "failed": {
      "filter": {
        "term": {
          "event_id": 4625 
        }
      }
    }
  }
}

The desired out Output in Kibana is as follow

    {
      "took" : 13120,
      "timed_out" : false,
      "_shards" : {
        "total" : 37,
        "successful" : 37,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 299924794,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "failed" : {
          "doc_count" : 351643
        },
        "success " : {
          "doc_count" : 40375274
        }
      }
    }

this is my code and i need to convert it NEST to get the desired result . Thanks

Upvotes: 0

Views: 733

Answers (1)

Rob
Rob

Reputation: 9979

You are almost there, you just need to add another case by calling .Filter(..) on aggregations descriptor

var searchResponse = await client.SearchAsync<Document>(s => s
    .Query(q => q.MatchAll())
    .Aggregations(a => a
        .Filter("success", success => success
            .Filter(filter => filter
                .Term(t => t.Field(f => f.EventId).Value(4624))))
        .Filter("failed", failed => failed
            .Filter(filter => filter
                .Term(t => t.Field(f => f.EventId).Value(4625))))));

public class Document
{
    public int Id { get; set; }
    public int EventId { get; set; }
}

Hope that helps.

Upvotes: 1

Related Questions