elasticsearch 中关于bucket_script的使用

1. 添加测试数据
点击add data
在这里插入图片描述
然后点击sample data 添加数据
elasticsearch 中关于bucket_script的使用_第1张图片
2. 按照天进行聚合

GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "sales_per_day": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      }
    }
  }
}

  1. 计算每天的销售总额
GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "sales_per_day": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "products.base_price"
          }
        },
        "sorts":{
          "bucket_sort": {
            "sort": {"total_sales":{ "order": "desc" }},
            "from": 0,
            "size": 4
          }
        }
      }
    }
  }
}
  1. 计算每天Men’s Clothing的销售额
GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "sales_per_day": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "products.base_price"
          }
        },
        "customfilter":{
          "filter": {
            "term": {
              "category.keyword": "Men's Clothing"
            }
          },
          "aggs": {
            "sales": {
              "sum": {
                "field": "products.base_price"
              }
            }
          }
        }
      }
    }
  }
}
  1. 使用bucket_script 计算日men closing销售占比
GET kibana_sample_data_ecommerce/_search
{
  "size": 0,
  "aggs": {
    "sales_per_day": {
      "date_histogram": {
        "field": "order_date",
        "calendar_interval": "day"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "products.base_price"
          }
        },
        "customfilter":{
          "filter": {
            "term": {
              "category.keyword": "Men's Clothing"
            }
          },
          "aggs": {
            "sales": {
              "sum": {
                "field": "products.base_price"
              }
            }
          }
        },
        "man's_clothing_percentage":{
          "bucket_script": {
            "buckets_path": {
              "total":"total_sales",
              "mens":"customfilter>sales"
            },
            "script": "params.mens/params.total"
          }
        }
      }
    }
  }
}

customfilter>sales 是指取数据的路径

你可能感兴趣的:(elasticsearch,elasticsearch,大数据)