0
votes

I have just started with Elasticsearch and am using the NEST API for my .Net application. I have an index and some records inserted. I am now trying to get a distinct list of document field values. I have this working in Postman. I do not know how to port the JSON aggregation body to a NEST call. Here is the call I am trying to port to the NEST C# API:

{
"size": 0,
"aggs": {
    "hosts": {
        "terms": {
            "field": "host"
        }
    }
}

Here is the result which is my next question. How would I parse or assign a POCO to the result? I am only interested in the distinct list of the field value in this case 'host'. I really just want an enumerable of strings back. I do not care about the count at this point.

{
"took": 0,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},
"hits": {
    "total": {
        "value": 3,
        "relation": "eq"
    },
    "max_score": null,
    "hits": []
},
"aggregations": {
    "hosts": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "hoyt",
                "doc_count": 3
            }
        ]
    }
}

}

1

1 Answers

0
votes

I was able to get the results I am after with the following code:

        var result = await client.SearchAsync<SyslogEntryIndex>(s => s.Size(0).Aggregations(a => a.Terms("hosts", t => t.Field(f => f.Host))));

        List<string> hosts = new List<string>();
        foreach (BucketAggregate v in result.Aggregations.Values)
        {
           foreach (KeyedBucket<object> item in v.Items)
            {
                hosts.Add((string)item.Key);
            }
        }
        return hosts;