learning_notes

学习笔记

View project on GitHub

聚合分析

设置要聚合的列的filedata

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

聚合

GET /shop/product/_search
{
  "size": 0, //不显示对那些数据操作
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

对匹配的内容进行聚合分析

GET /shop/product/_search
{
  "size": 0, 
  "query": {
    "match": {
      "name": "ya gao"
    }
  }, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

先分组再聚合运算,顺便排序

GET /shop/product/_search
{
  "size":0,
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order": {
          "arv_price":"desc"
        }
      },
      "aggs": {
        "arv_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}
GET /shop/product/_search
{
  "aggs": {
    "group_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 30
          },
          {
            "from": 30,
            "to":60
          },
          {
            "from":60,
            "to":200
          }
        ]
      },
      "aggs": {
        "group_by_tags": {
          "terms": {
            "field": "tags"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}