当前位置: 首页 > 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.dtcms.com/a/7579.html

相关文章:

  • python/c++ Leetcode题解——2744. 最大字符串配对数目
  • AI视频智能识别技术在智慧农业大棚升级改造管理场景中的应用方案
  • go获取文件md5值不正确的问题记录
  • 基于springboot+vue的图书个性化推荐系统(前后端分离)
  • 爬虫入门学习(二)——response对象
  • 【51单片机Keil+Proteus8.9】控制步进电机+LCD1602显示状态
  • Kafka框架详解
  • Hive数据定义(1)
  • C#学习教程
  • 【论文阅读】Relation-Aware Graph Transformer for SQL-to-Text Generation
  • java数据结构与算法刷题-----LeetCode59. 螺旋矩阵 II
  • 【02】mapbox js api加载arcgis切片服务
  • Python数据分析案例33——新闻文本主题多分类(Transformer, 组合模型) 模型保存
  • 如何避免知识付费小程序平台的陷阱?搭建平台的最佳实践
  • Webpack5入门到原理12:处理 Html 资源
  • linux上面hadoop配置集群
  • 现阶段Python和Java哪个更吃香?
  • Ubuntu使用QtCreator + CMake 开发C/C++程序
  • 克魔助手工具详解、数据包抓取分析、使用教程
  • 【备战蓝桥杯】图的遍历问题
  • C++后端笔记
  • MetaGPT-打卡-day2,MetaGPT框架组件学习
  • CSS 浮动 定位
  • 算法训练营第五十二天|300.最长递增子序列 674. 最长连续递增序列 718. 最长重复子数组
  • 软件工程研究生后期总结
  • 【Vue技巧】vue3中不支持.sync语法糖的解决方案
  • Flink启动Yarn Session报错:Couldn‘t deploy Yarn session cluster
  • 免费chartGPT网站汇总
  • Android aar包集成与报错
  • [python]裁剪文件夹中所有pdf文档并按名称保存到指定的文件夹