zoukankan      html  css  js  c++  java
  • NEST

    question:

    How can I do multiple nested aggregation?

    I have tried something like this:

    Aggregations(x => x
                      .Nested("Facets", y => y.Path("categories")
                        .Aggregations(r => r.Terms("categories", w => w.Field(q => q.Categories.FirstOrDefault().Id))
                      )).Nested("Facets2", s => s.Path("brand")
                        .Aggregations(e => e.Terms("brand", w => w.Field(q => q.Brand.Id))
                      )));
    

    But it returns Facets2 as a child of Facets

    Can anyone help?

    Answer:

    The aggregations that you have work as expected with NEST client version 1.7.1 as demonstrated with this example

    void Main()
    {
        var settings = new ConnectionSettings();
        var connection = new InMemoryConnection(settings);
        var client = new ElasticClient(connection : connection);
    
        var response = client.Search<Example>(s => s
            .Aggregations(aggs => aggs
                .Nested("Facets", nested => nested
                    .Path(p => p.Categories)
                    .Aggregations(r => r
                        .Terms("categories", w => w
                            .Field(q => q.Categories.FirstOrDefault().Id)
                        )
                    )
                )
                .Nested("Facets2", nested => nested
                    .Path(p => p.Brand)
                    .Aggregations(e => e
                        .Terms("brand", w => w
                            .Field(q => q.Brand.Id)
                        )
                    )
                )
            )
        );
    
        Console.WriteLine(Encoding.UTF8.GetString(response.RequestInformation.Request));
    }
    
    public class Example
    {
        public IList<Category> Categories { get; set; }
        public Brand Brand { get; set; }
    }
    
    public class Brand
    {
        public int Id { get; set; }
    }
    
    public class Category
    {
        public int Id { get; set; }
    }
    

    This outputs the following request query

    {
      "aggs": {
        "Facets": {
          "nested": {
            "path": "categories"
          },
          "aggs": {
            "categories": {
              "terms": {
                "field": "categories.id"
              }
            }
          }
        },
        "Facets2": {
          "nested": {
            "path": "brand"
          },
          "aggs": {
            "brand": {
              "terms": {
                "field": "brand.id"
              }
            }
          }
        }
      }
    }
    

     

  • 相关阅读:
    [WPF系列]基础学习(一) WPF是什么?
    [WPF系列]从基础起步学习系列计划
    [WPF系列]Adorner应用-自定义控件ImageHotSpot
    Windows Phone 8 开发必备资源
    WPF:数据和行为
    WPF触发器(Trigger)
    【WPF】Silverlight中的Action与Trigger
    【WPF】 Behavior
    Struts2 拦截器配置及使用
    在新建FileInputStream时使用当前相对路径或者绝对路径作为参数的问题
  • 原文地址:https://www.cnblogs.com/a-du/p/7417481.html
Copyright © 2011-2022 走看看