如何在elasticsearch中的嵌套对象上进行聚合

时间:2016-03-05 21:29:25

标签: elasticsearch aggregate

我在ES中有以下映射:

"mappings": {
    "products": {
        "properties": {
            "product": {
                "type" : "nested",
                "properties": {
                    "features": {
                        "type": "nested"
                    },
                    "sitedetails": {
                        "type": "nested"
                    }
                }
            }
        }
    }
}

然后是3个这样的产品:

 "hits": [
     {
        "_index": "catalog",
        "_type": "products",
        "_id": "AVNE8F4mFYOWvB4rMqdO",
        "_score": 1,
        "_source": {
           "product": {
              "ean": "abc",
              "features": {
                 "productType": "DVD player"
              },
              "color": "Black",
              "manufacturer": "Sony",
              "sitedetails": [
                 {
                    "name": "amazon.com",
                    "sku": "zzz",
                    "url": "http://www.amazon.com/dp/zzz"
                 }
              ],
              "category": "Portable DVD Players"
           }
        }
     },
     {
        "_index": "catalog",
        "_type": "products",
        "_id": "AVNE8XkXFYOWvB4rMqdQ",
        "_score": 1,
        "_source": {
           "product": {
              "ean": "def",
              "features": {
                 "ProductType": "MP3 player"
              },
              "color": "Black",
              "manufacturer": "LG",
              "sitedetails": [
                 {
                    "name": "amazon.com",
                    "sku": "aaa",
                    "url": "http://www.amazon.com/dp/aaa"
                 }
              ],
              "category": "MP3 Players"
           }
        }
     },
     {
        "_index": "catalog",
        "_type": "products",
        "_id": "AVNIh-xVWwxj6Cz_r8AT",
        "_score": 1,
        "_source": {
           "product": {
              "ean": "abc",
              "features": {
                 "productType": "DVD player"
              },
              "color": "White",
              "manufacturer": "Sony",
              "sitedetails": [
                 {
                    "name": "amazon.com",
                    "sku": "ggg",
                    "url": "http://www.amazon.com/dp/ggg"
                 }
              ],
              "category": "Portable DVD Players"
           }
        }
     }
  ]

我需要在UI端显示2个过滤器,一个用于制造商,一个用于网站。 如何在product.manufacturer和product.sitedetails.name上汇总?

TNX!

1 个答案:

答案 0 :(得分:0)

想出来:

GET /catalog/products/_search
{
"aggs": {
    "byManufacturer": {
        "nested": {
            "path": "product"
            },
            "aggs": {
                "byManufacturer": {
                    "terms": {
                        "field": "product.manufacturer"
                    }
                }
            }
        },
    "bySeller": {
        "nested": {
            "path": "product.sitedetails"
            },
            "aggs": {
                "bySeller": {
                    "terms": {
                        "field": "product.sitedetails.name"
                    }
                }
            }
        }    
    }
}