2017-10-05 91 views
0

外场我有这样的映射:Elasticsearch - 骨料对嵌套场,然后在嵌套

{ 
    "event": { 
     "properties": { 
      "visitor": { 
       "type": "keyword" 
      }, 
      "location": { 
       "type": "nested", 
       "properties": { 
        "country": { 
         "type": "keyword" 
        }, 
        "region": { 
         "type": "keyword" 
        }, 
        "city": { 
         "type": "keyword" 
        } 
       } 
      } 
     } 
    } 
} 

这两个集合工作打算:

{ 
    "size": 0, 
    "aggs": { 
     "location": { 
     "nested": { 
      "path": "location" 
     }, 
     "aggs": { 
      "by_country": { 
       "terms": { 
        "field": "location.country" 
       } 
      } 
     } 
     } 
    } 
} 

{ 
    "size": 0, 
    "aggs": { 
     "visitor_count": { 
     "cardinality": { 
      "field": "visitor" 
     } 
     } 
    } 
} 

但是当我尝试将它们合并像这样,国家聚合工作正常,但访问者数量都等于0,这是错误的。

{ 
    "size": 0, 
    "aggs": { 
     "location": { 
     "nested": { 
      "path": "location" 
     }, 
     "aggs": { 
      "by_country": { 
       "terms": { 
        "field": "location.country" 
       }, 
       "aggs": { 
        "visitor_count": { 
        "cardinality": { 
         "field": "visitor" 
        } 
        } 
       } 
      } 
     } 
     } 
    } 
} 

有人能告诉我如何实现我想要做的事吗? 我想问题是visitor字段不是嵌套location字段的一部分,但我找不到解决方法。 (当然,实际映射更复杂,我真的想避免改变它)

回答

0

好吧,原来神奇的关键词是“reverse_nested”。这段代码对我有用:

{ 
    "size": 0, 
    "aggs": { 
     "location": { 
     "nested": { 
      "path": "location" 
     }, 
     "aggs": { 
      "by_country": { 
       "terms": { 
        "field": "location.country" 
       }, 
       "aggs": { 
        "reverse": { 
        "reverse_nested": {}, 
        "aggs": { 
         "visitor_count": { 
          "cardinality": { 
           "field": "visitor" 
          } 
         } 
        } 
        } 
       } 
      } 
     } 
     } 
    } 
}