【问题标题】:aggregate on nested type properties - using NEST聚合嵌套类型属性 - 使用 NEST
【发布时间】:2015-06-26 18:36:04
【问题描述】:

我正在尝试聚合嵌套类型的计数或嵌套类型的属性总和,但无法让 NEST 在计算中包含多个嵌套文档。

        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")))
                 )
             )
        );

使用的类是这样的:

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; }
  }

如果每个 ItemIncident 有多个事件实例,则弹性仅计算索引总计数中列表中的最后一个。如果所有 IncidentInstances 的 Index = 3 的值,并且有五个文档,每个文档都有两个事件实例,那么我得到的结果是 15 (5*1*3),而不是 30 (5*2*3)。

这是我需要在索引字段上做一些特殊属性映射的问题吗?

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    您可以将nested aggregation 用于嵌套类型。

    所以,对于您的文件:

    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; }
    }
    

    使用聚合

    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))))));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2023-03-25
      • 2020-11-11
      • 2016-10-14
      相关资源
      最近更新 更多