How to aggregate over dynamic fields in elasticsearch?

后端 未结 2 1003
不知归路
不知归路 2020-12-29 13:18

I am trying to aggregate over dynamic fields (different for different documents) via elasticsearch. Documents are like following:

[{
   \"name\": \"galaxy no         


        
2条回答
  •  抹茶落季
    2020-12-29 14:01

    Not sure if this is what you mean, but this is fairly simple with basic aggregation functionality. Beware I did not include a mapping so with type of multiple words you are getting double results.

    POST /product/atype
    {
       "name": "galaxy note",
       "price": 123,
       "attributes": {
          "type": "phone",
          "weight": "140gm"
       }
    }
    
    POST /product/atype
    {
       "name": "shirt",
       "price": 123,
       "attributes": {
          "type": "clothing",
          "size": "m"
       }
    }
    
    GET /product/_search?search_type=count
    {
      "aggs": {
        "byType": {
          "terms": {
            "field": "attributes.type",
            "size": 10
          }
        }
      }
    }
    

提交回复
热议问题