Elasticsearch學習之嵌套聚合,下鑽分析,聚合分析


1. 計算每個tag下的商品數量

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

2. 將文本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" }
      }
    }
  }

3. 對名稱中包含yagao的商品,計算每個tag下的商品數量

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

4. 先分組,再算每組的平均值,計算每個tag下的商品的平均價格

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

5. 計算每個tag下的商品的平均價格,並且按照平均價格降序排序

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

6. 按照指定的價格范圍區間進行分組,然后在每組內再按照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"
               }
             }
           }
          }
       }
      }
     }
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM