ElasticSearch 学习记录之ES几种常见的聚合操作

时间:2021-08-04 12:11:14

ElasticSearch 系列文章

1 ES 入门之一 安装ElasticSearcha

2 ES 记录之如何创建一个索引映射

3 ElasticSearch 学习记录之Text keyword 两种基本类型区别

4 ES 入门记录之 match和term查询的区别

5 ElasticSearch 学习记录之ES几种常见的聚合操作

6 ElasticSearch 学习记录之父子结构的查询

7 ElasticSearch 学习记录之ES查询添加排序字段和使用missing或existing字段查询

8 ElasticSearch 学习记录之ES高亮搜索

9 ElasticSearch 学习记录之ES短语匹配基本用法

10 ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理

11 ElasticSearch 学习记录之集群分片内部原理

12 ElasticSearch 学习记录之ES如何操作Lucene段

13 ElasticSearch 学习记录之如任何设计可扩容的索引结构

14 ElasticSearch之 控制相关度原理讲解

ES几种常见的聚合操作

普通聚合

POST /product/_search
{
"size": 0,
"aggs": {
"agg_city": {
"terms": {
"field": "departureCitys" //字段
}
}
}
}

父子结构的索引的聚合

POST product/_search
{
"size": 0,
"aggs": {
"months": {
"children": {
"type": "price"//子类型
},
"aggs": {
"month": {
"terms": {
"field": "months"
}
}
}
}
}
}

双层嵌套结构索引的聚合查询

POST /product/_search
{
"size": 0,
"aggs": {
"theme": {
"nested": {
"path": "themes"//嵌套对象
},
"aggs": {
"agg_category": {
"terms": {
"field": "themes.category"//聚合对象
},
"aggs": {
"agg_name": {
"nested": {
"path": "themes.items"//嵌套对象的嵌套对象
},
"aggs": {
"agg_name": {
"terms": {
"field": "themes.items.name",//聚合的对象的路径
"size": 10
}
}
}
}
}
}
}
}
}
}

嵌套结构索引的聚合查询

POST /product/_search
{
"size": 0,
"aggs": {
"theme": {
"nested": {
"path": "themes"//嵌套对象
},
"aggs": {
"agg_category": {
"terms": {
"field": "themes.category"//聚合对象
}
}
}
}
}
}