一、聚合分析
    准备:
    将文本fielddata的属性设置为true

    PUT /ecommerce/_mapping/produce
    {
    “properties”: {
    “tags”:{
    “type”: “text”,
    “fielddata”: true
    }
    }
    }

    1、计算每个tag下的商品数量

    GET /ecommerce/produce/_search
    {
    “aggs”: {
    “groupbytags”: {
    “terms”: {
    “field”: “tags”
    }
    }
    },”size”: 0
    }

    结果:

    {
    “took”: 8,
    “timed_out”: false,
    “_shards”: {
    “total”: 5,
    “successful”: 5,
    “skipped”: 0,
    “failed”: 0
    },
    “hits”: {
    “total”: 4,
    “max_score”: 0,
    “hits”: []
    },
    “aggregations”: {
    “groupbytags”: {
    “doc_count_error_upper_bound”: 0,
    “sum_other_doc_count”: 0,
    “buckets”: [
    {
    “key”: “fangzhu”,
    “doc_count”: 2
    },
    {
    “key”: “meibai”,
    “doc_count”: 2
    },
    {
    “key”: “qingxin”,
    “doc_count”: 1
    }
    ]
    }
    }
    }

    2、对名称中包含yagao的商品,计算每个tag

    GET /ecommerce/produce/_search
    {
    “aggs”: {
    “groupbytags”: {
    “terms”: {
    “field”: “tags”
    }
    }
    },
    “size”: 0,
    “query”: {
    “match”: {
    “name”: “yagao”
    }
    }
    }

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

    GET /ecommerce/produce/_search
    {
    “aggs”: {
    “groupbytags”: {
    “terms”: {
    “field”: “tags”
    },
    “aggs”: {
    “avgprice”: {
    “avg”: {
    “field”: “price”
    }
    }
    }
    }
    },
    “size”: 0
    }

    4、先分组,再计算每组的平均值,计算每个tag下商品的平均价格,再按照平均价格降序排序

    GET /ecommerce/produce/_search
    {
    “aggs”: {
    “groupbytags”: {
    “terms”: {
    “field”: “tags”,
    “order”: {
    “avgprice”: “desc”
    }
    },
    “aggs”: {
    “avgprice”: {
    “avg”: {
    “field”: “price”
    }
    }
    }
    }
    },
    “size”: 0
    }

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

    GET /ecommerce/produce/_search
    {
    “aggs”: {
    “groupbyprice”: {
    “range”: {
    “field”: “price”,
    “ranges”: [
    {
    “from”: 0,
    “to”: 20
    },
    {
    “from”: 20,
    “to”: 40
    },
    {
    “from”: 40,
    “to”: 60
    }
    ]
    },
    “aggs”: {
    “groupbytags”: {
    “terms”: {
    “field”: “tags”
    },
    “aggs”: {
    “avgprice”: {
    “avg”: {
    “field”: “price”
    }
    }
    }
    }
    }
    }
    },
    “size”: 0
    }

    结果

    {
    “took”: 2,
    “timed_out”: false,
    “_shards”: {
    “total”: 5,
    “successful”: 5,
    “skipped”: 0,
    “failed”: 0
    },
    “hits”: {
    “total”: 4,
    “max_score”: 0,
    “hits”: []
    },
    “aggregations”: {
    “groupbyprice”: {
    “buckets”: [
    {
    “key”: “0.0-20.0”,
    “from”: 0,
    “to”: 20,
    “doc_count”: 0,
    “groupbytags”: {
    “doc_count_error_upper_bound”: 0,
    “sum_other_doc_count”: 0,
    “buckets”: []
    }
    },
    {
    “key”: “20.0-40.0”,
    “from”: 20,
    “to”: 40,
    “doc_count”: 2,
    “groupbytags”: {
    “doc_count_error_upper_bound”: 0,
    “sum_other_doc_count”: 0,
    “buckets”: [
    {
    “key”: “fangzhu”,
    “doc_count”: 2,
    “avgprice”: {
    “value”: 27.5
    }
    },
    {
    “key”: “meibai”,
    “doc_count”: 1,
    “avgprice”: {
    “value”: 30
    }
    }
    ]
    }
    },
    {
    “key”: “40.0-60.0”,
    “from”: 40,
    “to”: 60,
    “doc_count”: 2,
    “groupbytags”: {
    “doc_count_error_upper_bound”: 0,
    “sum_other_doc_count”: 0,
    “buckets”: [
    {
    “key”: “meibai”,
    “doc_count”: 1,
    “avgprice”: {
    “value”: 50
    }
    },
    {
    “key”: “qingxin”,
    “doc_count”: 1,
    “avgprice”: {
    “value”: 40
    }
    }
    ]
    }
    }
    ]
    }
    }
    }