Elasticsearch各种高级文档操作3

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

文章目录

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

初始化文档数据

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


聚合查询文档

概述

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


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

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

服务器响应结果

xml 复制代码
{
    "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,请求体内容为:

服务器响应结果

xml 复制代码
{
    "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,请求体内容为:

服务器响应结果

xml 复制代码
{
    "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,请求体内容为:

服务器响应结果

xml 复制代码
{
    "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,请求体内容为:

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

xml 复制代码
{
    "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,请求体内容为:

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

xml 复制代码
{
    "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几种常见的文档操作

相关推荐
玉石观沧海44 分钟前
高压变频器故障代码解析F67 F68
运维·经验分享·笔记·分布式·深度学习
小马爱打代码2 小时前
分布式锁:原理算法和使用建议
分布式·算法
problc3 小时前
PostgreSQL + Redis + Elasticsearch 实时同步方案实践:从触发器到高性能搜索
redis·elasticsearch·postgresql
Elastic 中国社区官方博客4 小时前
如何减少 Elasticsearch 集群中的分片数量
大数据·数据库·elasticsearch·搜索引擎·全文检索
一叶飘零_sweeeet4 小时前
从 “黑盒“ 到 “透明“:SkyWalking 实战指南 —— 让微服务问题无所遁形
分布式·微服务·skywalking·分布式链路追踪
顧棟4 小时前
【ES实战】ES6.8到9.1.4的常用客户端变化
elasticsearch
知识浅谈4 小时前
Elasticsearch 核心知识点全景解读
大数据·elasticsearch·搜索引擎
ArabySide5 小时前
【ASP.NET Core】分布式场景下ASP.NET Core中JWT应用教程
分布式·后端·asp.net core
Komorebi_99995 小时前
Git 常用命令完整指南
大数据·git·elasticsearch
还是鼠鼠6 小时前
《黑马商城》Elasticsearch基础-详细介绍【简单易懂注释版】
java·spring boot·spring·elasticsearch·搜索引擎·spring cloud·全文检索