Elasticsearch - SQL特性

基于聚合查询(二)求出每个颜色的销售数量、平均价格、最大价格、最小价格、价格总和
查询起来比较麻烦:

GET /tvs/_search
{
  "size": 0,
  "aggs": {
    "group_color": {
      "terms": {
        "field": "color"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        },
        "max_price": {
          "max": {
            "field": "price"
          }
        },
        "min_price": {
          "min": {
            "field": "price"
          }
        },
        "sum_price": {
          "sum": {
            "field": "price"
          }
        }
      }
    }
  }
}

想要通过sql的方式去查询出结果:
操作如下:

GET /_sql?format=txt
{
  "query": """
  SELECT color, avg(price), sum(price), min(price), max(price) FROM "tvs" group by color
  """
}

返回结果:

posted @ 2024-05-07 16:56  py卡卡  阅读(1)  评论(0编辑  收藏  举报