(04)Elasticsearch简单的分组,聚合,排序,下钻

1.按照name进行分组
	GET /product/electronic/_search
	{
	  "aggs": {			//数据的分组,聚合操作需要在aggs中进行
	    "group_by_name": { // group_by_ (这里接你要按什么分组的字段名) 一般是这样写,这里只是给取个别名
	      "terms": { "field": "name" }
	    }
	  }
	}
	如果是使用的默认的配置会报一下错误 
	Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory
	
	解决方法 将文本field的fielddata属性设置为true
	PUT /product/_mapping/electronic
	{
	  "properties": {
	    "name":{
	      "type": "text"
	      , "fielddata": false
	    }
	  }
	}
	  "aggregations": {
    "group_by_name": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "huawei",  //按name 分组,华为字段出现过2次
          "doc_count": 2   
        },
        {
          "key": "20",
          "doc_count": 1
        },
        {
          "key": "8",
          "doc_count": 1
        },
        {
          "key": "iphone",
          "doc_count": 1
        },
        {
          "key": "leshi",
          "doc_count": 1
        },
        {
          "key": "mate",
          "doc_count": 1
        },
        {
          "key": "pro",
          "doc_count": 1
        }
      ]
    }
  }
2.求平均值
//求平均价格
GET product/electronic/_search
{
  "aggs": {     
    "avg_price": {  
      "avg": {   //求平均值
        "field": "price"
      }
    }
  }
}
结果
  "aggregations": {
    "avg_price": {
      "value": 4000
    }
  }
//对name分组后再进行对价格求平均值
GET /product/electronic/_search
{
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}
结果 对name分组后的平均值
"buckets": [
   {
      "key": "huawei",
      "doc_count": 2,
      "avg_price": {
        "value": 3500 
  }
},]
对name分组后再进行对价格求平均值,然后再按平均价格进行降序
GET /product/electronic/_search
{
  "aggs": {
    "group_by_name": {
      "terms": {
        "field": "name",
        "order": {
          "avg_price": "desc"
        }
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

3.按照指定的价格范围区间进行分组

GET /product/electronic/_search
{
  "aggs": {
    "group_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 2500,
            "to": 3500
          },
          {
            "from": 3500,
            "to": 5000
          },
          {
            "from": 5000,
            "to": 6000
          }
        ]
      }
    }
  }
}
结果
  "aggregations": {
    "group_by_price": {
      "buckets": [
        {
          "key": "2500.0-3500.0",
          "from": 2500,
          "to": 3500,
          "doc_count": 2
        },
        {
          "key": "3500.0-5000.0",
          "from": 3500,
          "to": 5000,
          "doc_count": 1
        },
        {
          "key": "5000.0-6000.0",
          "from": 5000,
          "to": 6000,
          "doc_count": 0
        }
      ]
    }
  }
  //按照名子分组后再按价格分组
  GET /product/electronic/_search
{
  "aggs":{
      "group_by_name":{
        "terms": {
          "field": "name"
        }
        , "aggs": {
          "group_by_price": {
            "terms": {
              "field": "price"
            }
          }
        }
      }
  }
}
上一篇:es 聚合查询记录


下一篇:Elasticsearch聚合 之 Date Histogram聚合