2015-12-09 62 views
0

我该如何做多个嵌套聚合?NEST - 我怎样才能做多个嵌套聚合?

我已经试过这样的事情:

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

但它返回Facets2Facets

孩子谁能帮助?

+0

筑巢的版本你使用? – Rob

回答

0

,你有工作,与NEST客户端版本1.7.1预期与该实例证明了该聚合

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

此输出下面的请求查询

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

是的,我认为我确实在我自己的代码中弄错了一些字符。它的作品。,谢谢:) –

+0

不用担心,高兴地帮助:) –