ElasticSearch学习(十七) --查询统计

转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0
转载自:https://blog.csdn.net/qq_23536449/article/details/92612846

增加两条文档

PUT my-index/persion/5
{
  "name":"程裕强",
  "age":28,
  "salary":10000
}
PUT my-index/persion/6
{
  "name":"hadron",
  "age":19,
  "salary":5000
}

max

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "max_age": {
      "max": {"field": "age"}
    }
  }
}

返回结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "max_age": {
      "value": 28
    }
  }
}

min

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "min_age": {
      "min": {"field": "age"}
    }
  }
}

返回结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "min_age": {
      "value": 19
    }
  }
}

avg

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "avg_salary": {
      "avg": {"field": "salary"}
    }
  }
}

返回

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "avg_salary": {
      "value": 7166.666666666667
    }
  }
}

sum

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "sum_salary": {
      "sum": {"field": "salary"}
    }
  }
}

返回

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "sum_salary": {
      "value": 43000
    }
  }
}

stats

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "stats_salary": {
      "stats": {"field": "salary"}
    }
  }
}

返回结果

{
  "took": 21,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "statssalary": {
      "count": 6,
      "min": 5000,
      "max": 10000,
      "avg": 7166.666666666667,
      "sum": 43000
    }
  }
}

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "stats_salary": {
      "extended_stats": {"field": "salary"}
    }
  }
}

返回

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "stats_salary": {
      "count": 6,
      "min": 5000,
      "max": 10000,
      "avg": 7166.666666666667,
      "sum": 43000,
      "sum_of_squares": 325000000,
      "variance": 2805555.5555555522,
      "std_deviation": 1674.9792701868141,
      "std_deviation_bounds": {
        "upper": 10516.625207040295,
        "lower": 3816.7081262930387
      }
    }
  }
}

基数统计(类似sql中 count(distinct))

GET my-index/_search
{
  "size":0,
  "aggs": {
    "class_salary": {
      "cardinality": {
        "field": "salary"
      }
    }
  }
}

返回结果

{
  "took": 34,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "class_salary": {
      "value": 4
    }
  }
}

可以理解为这些员工有哪些工资(工资的类型)

文档数量统计

GET my-index/_search
{
  "size": 0,
  "aggs": {
    "doc_count": {
      "value_count": {
        "field": "salary"
      }
    }
  }
}

返回结果

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "doc_count": {
      "value": 6
    }
  }
}

统计包含某一字段的文档数量

百分位统计

GET my-index/_search
{
  "size": 0,
  "aggs": {
      "persion_salary": {
        "percentiles": {
          "field":"salary"
        }
      }
  }
}

返回结果

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "persion_salary": {
      "values": {
        "1.0": 5000,
        "5.0": 5000,#超过95%的人工资高于5000
        "25.0": 6000,
        "50.0": 7000,
        "75.0": 8000,
        "95.0": 10000,
        "99.0": 10000
      }
    }
  }
}

下一篇:ElasticSearch学习(十八) --聚合查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值