其他分享
首页 > 其他分享> > ES聚合查询在基本语法

ES聚合查询在基本语法

作者:互联网

以下代码均在Kibana的dev tools下执行

1、创造数据

POST /test_aggs/infos/1?pretty
{

	"title":"test class 01",

	"price": 20,

	"desc": ["good","very","handsome"]

}
 
POST /test_aggs/infos/2?pretty
{

	"title":"test class 02",

	"price": 30,

	"desc": ["good2","very","handsome"]

}

 
POST /test_aggs/infos/3?pretty
{

	"title":"test class 03",

	"price": 50,

	"desc": ["good3","very2","handsome"]

}


2、构建聚合函数语句
ES聚合的基本语法 

## 示例1:
POST /test_aggs/infos/_search?pretty
{

	"size":0,

	"query":{"match_all":{}},

	"aggs":{

		"aggs_terms":{

			"min":{

				 "field":"price"

			}

		}

	}

}

 

 

## 示例2:根据title的分词进行分组,计算分数的平均值

PUT /test_aggs/_mapping/infos
{
  "properties": {
    "title": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

 
POST /test_aggs/infos/_search?pretty
{

	"size":0,

	"query":{"match_all":{}},

	"aggs":{

		"aggs_terms":{

			"terms":{

				 "field":"title"

			},

			"aggs":{

			 		"aggs_avg":{

			 			"avg":{

			 				"field":"price"

			 			}

			 		}

			 }

		}

	}

}

结果:
​
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "aggs_terms": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "class",
          "doc_count": 3,
          "aggs_avg": {
            "value": 33.333333333333336
          }
        },
        {
          "key": "test",
          "doc_count": 3,
          "aggs_avg": {
            "value": 33.333333333333336
          }
        },
        {
          "key": "01",
          "doc_count": 1,
          "aggs_avg": {
            "value": 20
          }
        },
        {
          "key": "02",
          "doc_count": 1,
          "aggs_avg": {
            "value": 30
          }
        },
        {
          "key": "03",
          "doc_count": 1,
          "aggs_avg": {
            "value": 50
          }
        }
      ]
    }
  }
}

​
## 示例3:
POST /test_aggs/infos/_search?pretty
{

	"size":0,

	"query":{"match_all":{}},

	"aggs":{

		"aggs_terms":{

			"stats":{

				 "field":"price"

			}

		}

	}

}
#示例4:冗余索引的分组
1、构造更多测试数据

PUT /website/users/3
{
  "name": "黄药师",
  "email": "huangyaoshi@sina.com",
  "birthday": "1970-10-24"
}

PUT /website/blogs/3
{
  "title": "我是黄药师",
  "content": "我是黄药师啊,各位同学们!!!",
  "userInfo": {
    "userId": 1,
    "userName": "黄药师"
  }
}

PUT /website/users/2
{
  "name": "花无缺",
  "email": "huawuque@sina.com",
  "birthday": "1980-02-02"
}

PUT /website/blogs/4
{
  "title": "花无缺的身世揭秘",
  "content": "大家好,我是花无缺,所以我的身世是。。。",
  "userInfo": {
    "userId": 2,
    "userName": "花无缺"
  }
}

2、对每个用户发表的博客进行分组

比如说,小鱼儿发表的那些博客,花无缺发表了哪些博客,黄药师发表了哪些博客

GET /website/blogs/_search 
{
  "size": 0, 
  "aggs": {
    "group_by_username": {
      "terms": {
        "field": "userInfo.username.keyword"
      },
      "aggs": {
        "top_blogs": {
          "top_hits": {
            "_source": {
              "include": "title"
            }, 
            "size": 5
          }
        }
      }
    }
  }
}

 

标签:count,聚合,title,语法,aggs,test,infos,avg,ES
来源: https://blog.51cto.com/u_13270164/3036145