您当前的位置: 首页 >  数据分析

Dongguo丶

暂无认证

  • 4浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

34深入聚合数据分析_家电卖场案例以及统计哪种颜色电视销量最高

Dongguo丶 发布时间:2021-11-24 07:47:26 ,浏览量:4

1、家电卖场案例背景

以一个家电卖场中的电视销售数据为背景,来对各种品牌,各种颜色的电视的销量和销售额,进行各种各样角度的分析

创建索引并手动设置映射

PUT /tvs
{
	"mappings": {
		"sales": {
			"properties": {
				"price": {
					"type": "long"
				},
				"color": {
					"type": "keyword"
				},
				"brand": {
					"type": "keyword"
				},
				"sold_date": {
					"type": "date"
				}
			}
		}
	}
}

四个field:价格、颜色、品牌、销售时间。

填充数据

POST /tvs/sales/_bulk
{ "index": {}}
{ "price" : 1000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-10-28" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 3000, "color" : "绿色", "brand" : "小米", "sold_date" : "2016-05-18" }
{ "index": {}}
{ "price" : 1500, "color" : "蓝色", "brand" : "TCL", "sold_date" : "2016-07-02" }
{ "index": {}}
{ "price" : 1200, "color" : "绿色", "brand" : "TCL", "sold_date" : "2016-08-19" }
{ "index": {}}
{ "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" }
{ "index": {}}
{ "price" : 8000, "color" : "红色", "brand" : "三星", "sold_date" : "2017-01-01" }
{ "index": {}}
{ "price" : 2500, "color" : "蓝色", "brand" : "小米", "sold_date" : "2017-02-12" }

响应结果

{
  "took": 61,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYE",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYF",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYG",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYH",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYI",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYJ",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYK",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYL",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    }
  ]
}

查询

GET tvs/sales/_search

响应结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 1,
    "hits": [
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYI",
        "_score": 1,
        "_source": {
          "price": 1200,
          "color": "绿色",
          "brand": "TCL",
          "sold_date": "2016-08-19"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYK",
        "_score": 1,
        "_source": {
          "price": 8000,
          "color": "红色",
          "brand": "三星",
          "sold_date": "2017-01-01"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYE",
        "_score": 1,
        "_source": {
          "price": 1000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-10-28"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYF",
        "_score": 1,
        "_source": {
          "price": 2000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-11-05"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYG",
        "_score": 1,
        "_source": {
          "price": 3000,
          "color": "绿色",
          "brand": "小米",
          "sold_date": "2016-05-18"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYJ",
        "_score": 1,
        "_source": {
          "price": 2000,
          "color": "红色",
          "brand": "长虹",
          "sold_date": "2016-11-05"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYH",
        "_score": 1,
        "_source": {
          "price": 1500,
          "color": "蓝色",
          "brand": "TCL",
          "sold_date": "2016-07-02"
        }
      },
      {
        "_index": "tvs",
        "_type": "sales",
        "_id": "AX1PKYdDl-iNu-QvdWYL",
        "_score": 1,
        "_source": {
          "price": 2500,
          "color": "蓝色",
          "brand": "小米",
          "sold_date": "2017-02-12"
        }
      }
    ]
  }
}
2、统计哪种颜色的电视销量最高
GET /tvs/sales/_search
{
  "aggs": {
    "popular_colors": {
      "terms": {
        "field": "color"
      }
    }
  },
  "size": 0
}

size:只获取聚合结果,而不要执行聚合的原始数据 aggs:固定语法,要对一份数据执行分组聚合操作 popular_colors:就是对每个aggs,都要起一个名字,这个名字是随意的,你随便取什么都ok terms:根据字段的值进行分组 field:根据指定的字段的值进行分组

响应结果

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "红色",
          "doc_count": 4
        },
        {
          "key": "绿色",
          "doc_count": 2
        },
        {
          "key": "蓝色",
          "doc_count": 2
        }
      ]
    }
  }
}

hits.hits:因为我们指定了size是0,所以hits.hits就是空的,否则会把执行聚合的那些原始数据给你返回回来 aggregations:聚合结果 popular_color:我们指定的某个聚合的名称 buckets:根据我们指定的field划分出的buckets key:每个bucket对应的那个值 doc_count:这个bucket分组内,有多少个数据 每种颜色对应的bucket中的数据的数量,其实就是这种颜色的销量, 默认的排序规则:按照doc_count降序排序

关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

1.2487s