2017-06-29 175 views
0

我有一個Elasticsearch數據庫和我有一個索引test轉換Elasticsearch JSON查詢到C#NEST

這裏的模式:

PUT test 
{ 
    "settings" : { 
     "number_of_shards" : 1 
    }, 
    "mappings" : { 
     "channel" : { 
      "properties" : { 
       "id" : { "type" : "integer" }, 
       "name" : { "type" : "string" } 
      } 
     }, 
     "segment" : { 
      "properties" : { 
       "groupid" : { "type" : "text", "fielddata": true }, 
       "instrName" : { "type" : "text", "fielddata": true }, 
       "channelList" : { "type" : "object" } 
      } 
     } 
    } 
} 

我想這個查詢轉換成C#NEST代碼:

GET /test/segment/_search 
    { 
     "aggs": { 
     "agg": { 
      "terms": { 
      "field": "instrName" 
      }, 
      "aggs": { 
      "agg2": { 
       "terms": { 
       "field": "groupid" 
       } 
      } 
      } 
     } 
     } 
} 

我懂得一個聚集查詢,但不能嵌套聚合

轉換10

編輯

這裏目前的代碼,但我得到一個500錯誤從ES

var res = elastic.Search<SegmentRecord>(
     s => s.Index(esIndex).Aggregations(a => a.Terms("instrName", x => x.Aggregations(w => w.Terms("groupid", sel => sel))))); 
+1

你能告訴我們你現有的C#代碼? – mjwills

+0

看看嵌套的彙總用法示例:https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nested-aggregation-usage.html –

+0

@mjwills剛剛做到了。請看看,謝謝 –

回答

1

您還沒有指定每個terms aggregation應該運行在球場上。一個terms聚集了terms子聚集看起來像

var res = elastic.Search<SegmentRecord>(s => s 
    .Index(esIndex) 
    .Aggregations(a => a 
     .Terms("agg", t => t 
      .Field("instrName") 
      .Aggregations(sa => sa 
       .Terms("agg2", tt => tt 
        .Field("groupid") 
       ) 
      ) 
     ) 
    ) 
);