elasticsearch - 如何使用 NEST 获得不同的文档字段列表?
问题描述
我刚刚开始使用 Elasticsearch,并且正在为我的 .Net 应用程序使用 NEST API。我有一个索引和一些插入的记录。我现在正在尝试获取文档字段值的不同列表。我有这个在邮递员工作。 我不知道如何将 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;
推荐阅读
- android - 使用基于火的推送通知清除系统托盘上的通知
- amazon-cloudformation - 将子网列表作为参数传递给嵌套 CloudFormation 模板失败并出现错误
- polymer - 组件类中的纸质对话框句柄 onclose
- anaconda - 使用 Rcpp 包的 Jupyter Notebook 的 C++ 编译器问题
- prolog - 如何在 Prolog 中使用“-”构造函数?
- c++ - 将坐标传输到 Qt 中的对话框
- sorting - 如何根据增加的 posn-x 值和 posn-y 值订购 posn?
- memory-leaks - Tomcat JVM 中 ParallelWebAppClassLoaders 的多个实例保留对象的行为
- django - 如何接收基本模板 django 的上下文?
- excel - 如何让 VBA 类代码仅在对象的一个实例上执行?