当前位置: 首页 > news >正文

Elasticsearch各种高级文档操作3

本文来记录几种Elasticsearch的文档操作

文章目录

  • 初始化文档数据
  • 聚合查询文档
    • 概述
    • 对某个字段取最大值 max 示例
    • 对某个字段取最小值 min 示例
    • 对某个字段求和 sum 示例
    • 对某个字段取平均值 avg 示例
    • 对某个字段的值进行去重之后再取总数 示例
  • State 聚合查询文档
    • 概述
    • 操作实例
  • 桶聚合查询文档
    • 概述
    • terms 聚合,分组统计的示例
    • 在 terms 分组下再进行聚合的示例
  • 本文小结


初始化文档数据

在进行各种文档操作之前,我们先进行初始化文档数据的工作

在这里插入图片描述


聚合查询文档

概述

聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值、平均值等等。


对某个字段取最大值 max 示例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

服务器响应结果

{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"max_age": {"value": 25.0}}
}

对某个字段取最小值 min 示例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

服务器响应结果

{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"min_age": {"value": 20.0}}
}

对某个字段求和 sum 示例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

服务器响应结果

{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"sum_age": {"value": 88.0}}
}

对某个字段取平均值 avg 示例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

服务器响应结果

{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"avg_age": {"value": 22.0}}
}

对某个字段的值进行去重之后再取总数 示例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述


State 聚合查询文档

概述

stats 聚合,对某个字段一次性返回 count,max,min,avg 和 sum 五个指标。


操作实例

在 apifox 中,向 ES 服务器发 GET请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

服务器响应结果

在这里插入图片描述


桶聚合查询文档

概述

桶聚和相当于 sql 中的 group by 语句。


terms 聚合,分组统计的示例

在 apifox 中,向 ES 服务器发 POST 请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

查询成功后,服务器响应结果

{"took": 4,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"age_groupby": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": 20,"doc_count": 1},{"key": 21,"doc_count": 1},{"key": 22,"doc_count": 1},{"key": 25,"doc_count": 1}]}}
}

在 terms 分组下再进行聚合的示例

在 apifox 中,向 ES 服务器发 POST 请求 :http://localhost:9200/person/_search,请求体内容为:

在这里插入图片描述

查询成功后,服务器响应结果

{"took": 7,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"age_groupby": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": 20,"doc_count": 1,"sum_age": {"value": 20.0}},{"key": 21,"doc_count": 1,"sum_age": {"value": 21.0}},{"key": 22,"doc_count": 1,"sum_age": {"value": 22.0}},{"key": 25,"doc_count": 1,"sum_age": {"value": 25.0}}]}}
}

本文小结

本文记录了Elasticsearch几种常见的文档操作

http://www.lryc.cn/news/284778.html

相关文章:

  • 【算法题】66. 加一
  • 查看服务器资源使用情况
  • 锐浪报表 Grid++Report 明细表格标题重复打印
  • 编程笔记 html5cssjs 048 CSS链接
  • Spring DI
  • CorelDRAW Graphics Suite2024专业图形设计软件Mac/Windows版
  • 如何本地部署虚拟数字克隆人 SadTalker
  • 电容充电时间的计算
  • MicroPython核心(1):源码获取、编译构建
  • pyspark之Structured Streaming file文件案例1
  • 虚幻UE 特效-Niagara特效实战-雨天
  • k8s 集群搭建的一些坑
  • SpringMVC传递数据给前台
  • 国标GB28181安防视频监控EasyCVR级联后上级平台视频加载慢的原因排查
  • React16源码: React中的HostComponent HostText的源码实现
  • Unity3D代码混淆方案详解
  • 安科瑞应急疏散照明系统在歌舞娱乐等场所的应用
  • Go语言协程使用
  • JAVA如何创建对象
  • 《WebKit 技术内幕》之五(2): HTML解释器和DOM 模型
  • Spring Boot多环境配置
  • 常用的目标跟踪有哪些
  • python222网站实战(SpringBoot+SpringSecurity+MybatisPlus+thymeleaf+layui)-帖子详情页实现
  • 11、Kafka ------ Kafka 核心API 及 生产者API 讲解
  • MySQL 8.3 发布, 它带来哪些新变化?
  • 【数据结构】详谈队列的顺序存储及C语言实现
  • 为什么 HTTPS 协议能保障数据传输的安全性?
  • 使用 Node 创建 Web 服务器
  • leetcode 151反转字符串如何原地去除多余空格
  • 面试问题记录【深圳,共三面,A 轮公司】