Elasticsearch top players (6) - Quick Start real case: group by + avg + sort and other polymerization analysis (c)

1. Calculate the number of items in each tag

GET /ecommerce/product/_search

{

     “aggs”:{

          “group_by_tags”:{

                 “terms”:{“field”:”tags”}

           }

     }

}

image

The text field of fielddate property is set to true

PUT /ecommerce/_mapping/product

{

      “properties”:{

            “tags”:{

                “type”:”text”,

                “fielddata”:true

           }

      }

}

image

Re-execute the command packet

“size”:0

The results may not display information about the analysis of the data, return only after polymerization Analysis

image


2. a name which includes yagao goods, quantity of goods calculated at each tag

GET /ecommerce/product/_search
{
   "size": 0,
   "query": {
     "match": {
       "name": "yagao"
     }
   },
   "aggs": {
     "group_by_tags": {
       "terms": {
         "field": "tags"
       }
     }
   }
}

image

结果一样,因为每个商品name中都包含有yagao

3.先分组,再算每组的平均值,计算每个tag下的商品的平均价格

GET /ecommerce/product/_search

{

      “size”:0,

      “aggs”:{

            “group_by_tags”:{

                     “terms”:{“fiedd”:”tags”},

                     “aggs”:{

                           “avg_price”:{

                                    “avg”:{“field”:”price”}

                             }

                     }

             }

      }

}

image


4.在3的基础上,按照平均价格进行排序

GET /ecommerce/product/_search

{

      “size”:0,

      “aggs”:{

            “group_by_tags”:{

                     “terms”:{“fiedd”:”tags”,“order”:{“avg_price”:”desc”}},

                     “aggs”:{

                           “avg_price”:{

                                    “avg”:{“field”:”price”}

                             }

                     }

             }

      }

}

image

5.按照指定的价格范围区间进行分组,然后在每组内按照tag进行分组,最后再计算每组的平均价格

GET /ecommerce/product/_search
{
   "size": 0,
   "aggs": {
   "group_by_price": {
       "range": {
         "field": "price",
         "ranges": [
           {
             "from": 0,
             "to": 20
           },
           {
             "from": 20,
             "to": 40
           },
           {
             "from": 40,
             "to": 50
           }
         ]
       },
       "aggs": {
         "group_by_tag": {
           "terms": {
             "field": "tags",
             "order": {
               "avg_by_price": "desc"
             }
           },
           "aggs": {
             "avg_by_price": {
               "avg": {
                 "field": "price"
               }
             }
           }
         }
       }
     }
   }
}

image

Guess you like

Origin www.cnblogs.com/Mr-WangYue/p/11518866.html