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

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"//聚合对象
          } 
        }
      }
    }
  }
}

posted on 2017-12-14 23:33 王守昌 阅读() 评论() 编辑 收藏

版权声明:本文为wangshouchang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/wangshouchang/p/8040587.html