1. 程式人生 > >快速入門案例實戰之電商網站商品管理:巢狀聚合,下鑽分析,聚合分析

快速入門案例實戰之電商網站商品管理:巢狀聚合,下鑽分析,聚合分析

第一個分析需求:計算每個tag下的商品數量

GET /ecommerce/product/_search
{
  "aggs": {
    "group_by_tags": {
      "terms": { "field": "tags" }
    }
  }
}

將文字field的fielddata屬性設定為true

PUT /ecommerce/_mapping/product
{
  "properties": {
    "tags": {
      "type": "text",
      "fielddata": true
    }
  }
}

GET /ecommerce/product/_search
{
  "size": 0,
  "aggs": {
    "all_tags": {
      "terms": { "field": "tags" }
    }
  }
}

{
  "took": 20,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_tags": {
      "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
        }
      ]
    }
  }
}

----------------------------------------------------------------------------------------------------------------

第二個聚合分析的需求:對名稱中包含yagao的商品,計算每個tag下的商品數量

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

----------------------------------------------------------------------------------------------------------------

第三個聚合分析的需求:先分組,再算每組的平均值,計算每個tag下的商品的平均價格

GET /ecommerce/product/_search
{
    "size": 0,
    "aggs" : {
        "group_by_tags" : {
            "terms" : { "field" : "tags" },
            "aggs" : {
                "avg_price" : {
                    "avg" : { "field" : "price" }
                }
            }
        }
    }
}

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_tags": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "fangzhu",
          "doc_count": 2,
          "avg_price": {
            "value": 27.5
          }
        },
        {
          "key": "meibai",
          "doc_count": 2,
          "avg_price": {
            "value": 40
          }
        },
        {
          "key": "qingxin",
          "doc_count": 1,
          "avg_price": {
            "value": 40
          }
        }
      ]
    }
  }
}

----------------------------------------------------------------------------------------------------------------

第四個資料分析需求:計算每個tag下的商品的平均價格,並且按照平均價格降序排序

GET /ecommerce/product/_search
{
    "size": 0,
    "aggs" : {
        "all_tags" : {
            "terms" : { "field" : "tags", "order": { "avg_price": "desc" } },
            "aggs" : {
                "avg_price" : {
                    "avg" : { "field" : "price" }
                }
            }
        }
    }
}

我們現在全部都是用es的restful api在學習和講解es的所欲知識點和功能點,但是沒有使用一些程式語言去講解(比如java),原因有以下:

1、es最重要的api,讓我們進行各種嘗試、學習甚至在某些環境下進行使用的api,就是restful api。如果你學習不用es restful api,比如我上來就用java api來講es,也是可以的,但是你根本就漏掉了es知識的一大塊,你都不知道它最重要的restful api是怎麼用的
2、講知識點,用es restful api,更加方便,快捷,不用每次都寫大量的java程式碼,能加快講課的效率和速度,更加易於同學們關注es本身的知識和功能的學習
3、我們通常會講完es知識點後,開始詳細講解java api,如何用java api執行各種操作
4、我們每個篇章都會搭配一個專案實戰,專案實戰是完全基於java去開發的真實專案和系統

----------------------------------------------------------------------------------------------------------------

第五個資料分析需求:按照指定的價格範圍區間進行分組,然後在每組內再按照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_tags": {
          "terms": {
            "field": "tags"
          },
          "aggs": {
            "average_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}