接续上篇,本篇介绍elasticsearch聚合查询,使用python库elasticsearch-dsl进行聚合查询操作。

7.3、聚合查询

高阶概念

  • Buckets(桶/集合):满足特定条件的文档的集合
  • Metrics(指标):对桶内的文档进行统计计算(例如最小值,求和,最大值等)

    • 新建一张测试表

       PUT cars
      {
      "mappings": {
      "transactions":{
      "properties": {
      "price":{
      "type": "integer"
      },
      "color":{
      "type": "text",
      "fielddata": true
      },
      "make":{
      "type": "text",
      "fielddata": true
      },
      "sold":{
      "type": "date",
      "format": "yyyy-MM-dd"
      }
      }
      }
      }
      }

      插入数据

       POST /cars/transactions/_bulk
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
    • 查询哪个颜色的汽车销量最好(按颜色分类)
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "popular_colors": {
      "terms": {
      "field": "color"
      }
      }
      }
      }
       s = Search(index='cars')
      a = A("terms", field="color")
      s.aggs.bucket("popular_color", a)
      response = s.execute()

      或者

       s.aggs.bucket("popular_color", "terms", field="color")
    • 查询每种颜色车的平均价格
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "avg_price": {
      "avg": {
      "field": "price"
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      a1 = A("terms", field="color")
      a2 = A("avg", field="price")
      s.aggs.bucket("colors", a1).metric("avg_price", a2)
      response = s.execute()

      或者

       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color").metric("avg_price", "avg", field="price")
      response = s.execute()
    • 先按颜色分,再按品牌分,再求每种品牌的均价
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "make": {
      "terms": {
      "field": "make"
      },
      "aggs": {
      "avg_price": {
      "avg": {
      "field": "price"
      }
      }
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color")
      s.aggs["colors"].bucket("make", "terms", field="make")
      s.aggs["colors"].aggs["make"].metric("avg_price", "avg", field="price")
      response = s.execute()
    • 先按颜色分,再按品牌分,再求每种品牌的最高和最低价
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "make": {
      "terms": {
      "field": "make"
      },
      "aggs": {
      "min_price": {
      "min": {
      "field": "price"
      }
      },
      "max_price": {
      "max": {
      "field": "price"
      }
      }
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color")
      s.aggs["colors"].bucket("make", "terms", field="make")
      s.aggs["colors"].aggs["make"].metric("min_price", "min", field="price")
      s.aggs["colors"].aggs["make"].metric("max_price", "max", field="price")
      response = s.execute()
    • 未完待续...

最新文章

  1. 调用微信退款接口或发红包接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
  2. hive内部表、外部表、分区表、视图
  3. 2.一起来学hibernate之配置文件1与持久化对象
  4. Android中解决图像解码导致的OOM问题
  5. IE下AjaxForm上传文件直接提示下载的兼容性Bug
  6. java中的static变量
  7. bzoj3667: Rabin-Miller算法
  8. Apache POI解析excel文件
  9. [转] Ant 编译 Android 项目为 Apk 实战, 常见问题解决
  10. 安装Devstack的DNS问题
  11. 初步掌握Yarn的架构及原理(转)
  12. IP Editor IP控件(对比一下封装IP控件)
  13. LeetCode Solutions : Swap Nodes in Pairs
  14. 转:如何让LoadRunner实现多个场景运行?
  15. 用Redis实现优先级队列
  16. Spring MVC处理(下周完善)
  17. kubernetes入门(04)kubernetes的核心概念(1)
  18. 观察者模式之ES6实现(一)
  19. CentOS7 配置静态 ip
  20. JAVA I/O(一)基本字节和字符IO流

热门文章

  1. C知识要点-个人总结
  2. [HZOI 2015]复仇的序幕曲
  3. js 去掉字符串前后空格5种方法
  4. JavaSE之Java基础(1)
  5. div多选控制
  6. 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
  7. SpringBoot常用应用程序属性
  8. Java -GUI开发九九乘法表
  9. check_mk 之 Check Parameters
  10. jshint-eclipse: JavaScript Code Quality Plugin for Eclipse