2017-01-13 5 views
0

私は今日間これを理解しようとしたC#のNESTといくつかの助けが必要です:弾性検索NEST:マルチレベルの深いAggregrationsを読む

状況: 私は複数の種類の文書をバック与えクエリを持って、私対応するdoc_countを使用してすべての型をリストする集約を行いました。したがって、インスタントタイプのチケットでは、この検索アクションで299のチケットがあります。 (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nested-aggregation-usage.html#_handling_responses_25

var tags = response.Aggs.Nested("tags"); 
var tagNames = tags.Terms("tag_names"); 

これは1つのレベルの深aggegrationsで動作しますが、鉱山は、いくつかのレベルが深い:

は今、私は次の操作を実行する必要がマニュアルに従って、データを取得する必要があります。

これはAggs句です:

"aggs": { 
     "filtered_types": { 
      "terms": { 
       "field": "_type" 
      } 
     }, 
     "all_types": { 
      "global": {}, 
      "aggs": { 
       "all_result_types": { 
        "filter": { 
         "bool": { 
          "must": [ 
           { 
            "query_string": { 
             "default_field": "_all", 
             "query": "\"test\"" 
            } 
           } 
          ] 
         } 
        }, 
        "aggs": { 
         "result_types": { 
          "terms": { 
           "field": "_type" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

私の応答は、次のようになります。応答が正しい

"aggregations": { 
     "filtered_types": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": "ticket", 
       "doc_count": 105 
      }, 
      { 
       "key": "iteration", 
       "doc_count": 10 
      } 
     ] 
     }, 
     "all_types": { 
     "doc_count": 2516, 
     "all_result_types": { 
      "doc_count": 193, 
      "result_types": { 
       "doc_count_error_upper_bound": 0, 
       "sum_other_doc_count": 0, 
       "buckets": [ 
        { 
        "key": "ticket", 
        "doc_count": 105 
        }, 
        { 
        "key": "comment", 
        "doc_count": 67 
        }, 
        { 
        "key": "iteration", 
        "doc_count": 10 
        }, 
        { 
        "key": "profile", 
        "doc_count": 6 
        }, 
        { 
        "key": "project", 
        "doc_count": 3 
        }, 
        { 
        "key": "company", 
        "doc_count": 1 
        }, 
        { 
        "key": "sla", 
        "doc_count": 1 
        } 
       ] 
      } 
     } 
     } 
    } 

、これは私が必要とする正確に何ですが、NESTで私が取得するように見えることはできません私のデータが存在する "result_types"に変換します。

私は誰でもデータにアクセスするための解決策を教えてくれることを願っています。

答えて

0

"result_types"用語アグリゲーションのバケットは、"all_result_types"フィルタアグリゲーションのサブアグリゲーションです。これ自体は、"all_types"グローバルアグリゲーションのサブアグリゲーションです。バケットに到達するには、応答タイプの集計をトラバースする必要があります。

var searchResponse = client.Search<Document>(s => s 
    .Aggregations(a => a 
     .Terms("filtered_types", t => t 
      .Field("_type") 
     ) 
     .Global("all_types", g => g 
      .Aggregations(aa => aa 
       .Filter("all_result_types", f => f 
        .Filter(ff => ff 
         .Bool(b => b 
          .Must(q => q 
           .QueryString(qs => qs 
            .DefaultField("_all") 
            .Query("\"test\"") 
           ) 
          ) 
         ) 
        ) 
        .Aggregations(aaa => aaa 
         .Terms("result_types", t => t 
          .Field("_type") 
         ) 
        ) 
       ) 
      ) 
     ) 
    ) 
); 

// get the global aggregation 
var globalAggs = searchResponse.Aggs.Global("all_types"); 

// get the filter aggregation 
var filterAggs = globalAggs.Filter("all_result_types"); 

// get the terms aggregation 
var termsAggs = filterAggs.Terms("result_types"); 

// do something with the buckets 
foreach (var bucket in termsAggs.Buckets) 
{ 
    Console.WriteLine($"key: {bucket.Key}, count: {bucket.DocCount}"); 
} 
+0

ありがとうございます、あなたは非常に明確な答えです!私は.Filter()について知らなかった: –

+0

心配しないで、喜んで:) –