elasticsearch 对嵌套类型属性进行聚合-使用嵌套
我试图对嵌套类型的计数或嵌套类型属性的总和进行聚合,但无法使嵌套在计算中包含多个嵌套文档elasticsearch 对嵌套类型属性进行聚合-使用嵌套,elasticsearch,nest,elasticsearch,Nest,我试图对嵌套类型的计数或嵌套类型属性的总和进行聚合,但无法使嵌套在计算中包含多个嵌套文档 var result = elasticClient.Search<ItemIncidents>(s => s .Aggregations(a => a .Terms("group by role", ts => ts .Field(o => o.LabelName)
var result = elasticClient.Search<ItemIncidents>(s => s
.Aggregations(a => a
.Terms("group by role", ts => ts
.Field(o => o.LabelName)
.d
.Aggregations(aa => aa
.Sum("sum incidents", sa => sa
.Field("incidents.index")))
)
)
);
var result=elasticClient.Search(s=>s
.聚合(a=>a
.Terms(“按角色分组”,ts=>ts
.Field(o=>o.LabelName)
D
.聚合(aa=>aa
.Sum(“事件总数”,sa=>sa
.Field(“事件索引”))
)
)
);
使用的类别如下:
public class ItemIncidents
{
public int Id{get;set;}
public string LabelName { get; set; }
// [ElasticProperty(Type = FieldType.Nested)]
public List<IncidentInstance> Incidents { get; set; }
}
public partial class IncidentInstance: {
public string Id { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
公共类事件
{
公共int Id{get;set;}
公共字符串LabelName{get;set;}
//[ElasticProperty(类型=FieldType.Nested)]
公共列表事件{get;set;}
}
公共部分类突发事件:{
公共字符串Id{get;set;}
公共int索引{get;set;}
公共整数计数{get;set;}
}
如果每个ItemIncident有多个incidentinstance,则elastic仅在索引的聚合计数中计算列表中的最后一个incidentinstance。如果所有incidentinstance的Index=3,并且有五个文档,每个文档有两个incidentinstance,那么我得到的结果是15(5*1*3),而不是30(5*2*3)
在这个问题上,我需要对索引字段进行一些特殊的属性映射吗?可以用于嵌套类型
因此,对于您的文档:
public class ItemIncidents
{
public int Id { get; set; }
public string LabelName { get; set; }
[ElasticProperty(Type = FieldType.Nested)]
public List<IncidentInstance> Incidents { get; set; }
}
public class IncidentInstance
{
public string Id { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
公共类事件
{
公共int Id{get;set;}
公共字符串LabelName{get;set;}
[ElasticProperty(类型=FieldType.Nested)]
公共列表事件{get;set;}
}
公共类突发事件
{
公共字符串Id{get;set;}
公共int索引{get;set;}
公共整数计数{get;set;}
}
使用聚合
var searchResponse = client.Search<ItemIncidents>(s => s.Aggregations(a => a
.Nested("indexCount", nested => nested
.Path(p => p.Incidents)
.Aggregations(aa => aa.Sum("sum", sum => sum.Field(f => f.Incidents.First().Index))))));
var searchResponse=client.Search(s=>s.Aggregations(a=>a
.Nested(“indexCount”,Nested=>Nested
.Path(p=>p.Events)
.Aggregations(aa=>aa.Sum(“Sum”,Sum=>Sum.Field(f=>f.incents.First().Index‘‘‘‘‘‘)’);
可用于嵌套类型
因此,对于您的文档:
public class ItemIncidents
{
public int Id { get; set; }
public string LabelName { get; set; }
[ElasticProperty(Type = FieldType.Nested)]
public List<IncidentInstance> Incidents { get; set; }
}
public class IncidentInstance
{
public string Id { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
公共类事件
{
公共int Id{get;set;}
公共字符串LabelName{get;set;}
[ElasticProperty(类型=FieldType.Nested)]
公共列表事件{get;set;}
}
公共类突发事件
{
公共字符串Id{get;set;}
公共int索引{get;set;}
公共整数计数{get;set;}
}
使用聚合
var searchResponse = client.Search<ItemIncidents>(s => s.Aggregations(a => a
.Nested("indexCount", nested => nested
.Path(p => p.Incidents)
.Aggregations(aa => aa.Sum("sum", sum => sum.Field(f => f.Incidents.First().Index))))));
var searchResponse=client.Search(s=>s.Aggregations(a=>a
.Nested(“indexCount”,Nested=>Nested
.Path(p=>p.Events)
.Aggregations(aa=>aa.Sum(“Sum”,Sum=>Sum.Field(f=>f.incents.First().Index‘‘‘‘‘‘)’);