2017-01-13 1 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"); 

이것은 한 단계 깊은 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

걱정하지 마시고 도와 드리겠습니다. :) –