2015-12-09 26 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

不用擔心,高興地幫助:) –