2017-11-12 1 views
2

시티와 홈 유형은 다음 문서 매핑이 중첩 된 객체 :elasticsearch에서 두 중첩 집계를 수행하는 방법은 무엇입니까?

"mappings" : { 
    "home_index_doc" : { 
    "properties" : { 
     "city" : { 
     "type" : "nested", 
     "properties" : { 
      "country" : { 
      "type" : "nested", 
      "properties" : { 
       "name" : { 
       "type" : "text" 
       } 
      } 
      }, 
      "name" : { 
      "type" : "keyword" 
      } 
     } 
     }, 
     "home_type" : { 
     "type" : "nested", 
     "properties" : { 
      "name" : { 
      "type" : "keyword" 
      } 
     } 
     }, 
     ... 
    } 
    } 
} 

나는 다음과 같은 집계을하려고 오전 : 모든 존재하는 문서를 타고 도시마다 모든 home_types을 보여줍니다.

"aggregations": { 
    "all_cities": { 
    "buckets": [ 
     { 
     "key": "Tokyo", 
     "doc_count": 12, 
     "home_types": { 
      "buckets": [ 
       { 
        "key": "apartment", 
        "doc_count": 5 
       }, 
       { 
        "key": "house", 
        "doc_count": 12 
       } 
      ] 
     } 
     }, 
     { 
     "key": "New York", 
     "doc_count": 1, 
     "home_types": { 
      "buckets": [ 
       { 
        "key": "house", 
        "doc_count": 1 
       } 
      ] 
     } 
     } 
    ] 
    } 
} 

gazzilion의 aproaches와 조합을 시도 후, 나는 키바와 멀리 그것을 만들었어요 :

가 나는 비슷한에 보일 것입니다 상상

GET home-index/home_index_doc/_search 
{ 
    "size": 0, 
    "aggs": { 
    "all_cities": { 
    "nested": { 
     "path": "city" 
     }, 
     "aggs": { 
     "city_name": { 
      "terms": { 
      "field": "city.name" 
      } 
     } 
     } 
    }, 
    "aggs": { 
     "all_home_types": { 
     "nested": { 
      "path": "home_type" 
     }, 
     "aggs": { 
      "home_type_name": { 
      "terms": { 
       "field": "home_type.name" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

나는 다음과 같은 예외를 얻을 :

"type": "unknown_named_object_exception", 
    "reason": "Unknown BaseAggregationBuilder [all_home_types]", 

답변

5

당신은 O를 뛰어하기 위해 reverse_nested를 사용할 필요가 f city 중첩 유형을 루트 수준으로 되돌리고 home_type 중첩 유형에 대해 nested 집계를 수행하십시오. 기본적으로 다음과 같이 :

{ 
    "size": 0, 
    "aggs": { 
    "all_cities": { 
     "nested": { 
     "path": "city" 
     }, 
     "aggs": { 
     "city_name": { 
      "terms": { 
      "field": "city.name" 
      }, 
      "aggs": { 
      "by_home_types": { 
       "reverse_nested": {}, 
       "aggs": { 
       "all_home_types": { 
        "nested": { 
        "path": "home_type" 
        }, 
        "aggs": { 
        "home_type_name": { 
         "terms": { 
         "field": "home_type.name" 
         } 
        } 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
관련 문제