【发布时间】:2019-02-27 04:03:34
【问题描述】:
有谁知道如何使用嵌套进行多个聚合? 我发现了很多例子,不幸的是它们都不起作用。
这是我所拥有的:
Vehicles fields = new Vehicles();
//create a terms query
var query = new TermsQuery
{
IsVerbatim = true,
Field = "VehicleOwnerId",
Terms = new string[] { 25 },
};
var aggregations = new Dictionary<string, IAggregationContainer>
{
{ "years", new AggregationContainer
{
Terms = new TermsAggregation(nameof(fields.Year))
{
Field = new Field(nameof(fields.Year))
}
}
}
//,
//{ "makes", new AggregationContainer
// {
// Terms = new TermsAggregation("Make")
// {
// Field = new Field(nameof(fields.Make))
// }
// }
//}
};
//create the search request
var searchRequest = new SearchRequest
{
Query = query,
From = 0,
Size = 100,
Aggregations = aggregations
};
var result = client.SearchAsync<InventoryLiveView>(searchRequest).Result;
var years = result.Aggregations.Terms("years");
Dictionary<string, long> yearCounts = new Dictionary<string, long>();
foreach (var item in years.Buckets)
{
yearCounts.Add(item.Key, item.DocCount ?? 0);
}
如果我像这样执行代码,它就可以工作。 Years 按预期返回聚合。如果我尝试添加另一个字段(如上面注释掉的那个),它会失败并且我得到零记录。 如何在一个查询中获取多个聚合?我到处都看到了它的例子,但我尝试过的例子似乎都不起作用,而且大多数似乎已经过时(包括 Nest 文档中的一些例子)。 我也尝试过这种非常接近文档的方法。
//create the search request
var searchRequest = new SearchRequest
{
Query = query,
From = 0,
Size = 100,
//Aggregations = aggregations
Aggregations = new AggregationDictionary
{
{
"childAgg", new ChildrenAggregation("childAgg", typeof(Vehicles ))
{
Aggregations = new AggregationDictionary
{
{"years", new TermsAggregation(nameof(fields.VehicleYear))},
{"makes", new TermsAggregation(nameof(fields.VehicleMakeName))},
{"models", new TermsAggregation(nameof(fields.VehicleModelName))},
}
}
}
}
};
var result = client.SearchAsync<Vehicles>(searchRequest).Result;
这只会产生一个空引用异常。
【问题讨论】:
-
哪些 NEST 文档已过时?你见过elastic.co/guide/en/elasticsearch/client/net-api/current/…
-
我认为该页面已过时。它显示了这个例子: new MaxAggregation("max_per_child", Field
(p => p.ConfidenceFactor)) 应该是 MaxAggregation("max_per_child", new Field("ConfidenceFactor")) 或者尽可能地租赁告诉。 -
现在,当我尝试页面中的一个示例时,我看到了一个错误。错误是“类型:null_pointer_exception 原因:”“”不确定它为什么会发生或这意味着什么。
-
页面上的文档是从测试源代码生成的:github.com/elastic/elasticsearch-net/blob/… 这些测试在每次签入时运行,以通知客户端代码是否存在编译错误或更改,以便最终,文档与该分支中的最新客户端代码保持一致。在这种情况下,我看不到文档有任何问题,但如果您确实遇到了一些问题,请随时在 github.com/elastic/elasticsearch-net/issues 上打开问题,以便我们解决
-
似乎对我不起作用的代码是术语聚合的一个示例。它显示 `Field = Field
(p => p.State)` 但我无法让 Field 工作。但是,字段(字符串)工作得很好。我不记得确切的错误,但它只是没有识别它。谢谢你提供的信息——我可能做错了什么。
标签: c# elasticsearch nest