gpt4 book ai didi

elasticsearch - 如何使用 NEST 获得不同的文档字段列表?

转载 作者:行者123 更新时间:2023-12-03 02:26:55 25 4
gpt4 key购买 nike

我刚刚开始使用 Elasticsearch,并且正在为我的 .Net 应用程序使用 NEST API。我有一个索引和一些插入的记录。我现在正在尝试获取文档字段值的不同列表。我有这个在 postman 工作。 我不知道如何将 JSON 聚合体移植到 NEST 调用 .这是我试图移植到 NEST C# API 的调用:

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

这是我的下一个问题的结果。 我将如何解析或将 POCO 分配给结果? 在这种情况下,我只对“主机”中的字段值的不同列表感兴趣。我真的只是想要一个可枚举的字符串。我现在不在乎计数。
{
"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
}
]
}
}

}

最佳答案

我能够使用以下代码获得我想要的结果:

        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;

关于elasticsearch - 如何使用 NEST 获得不同的文档字段列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60879799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com