千家信息网

elasticsearch文档操作的方法有哪些

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章主要介绍"elasticsearch文档操作的方法有哪些",在日常操作中,相信很多人在elasticsearch文档操作的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法
千家信息网最后更新 2025年12月02日elasticsearch文档操作的方法有哪些

这篇文章主要介绍"elasticsearch文档操作的方法有哪些",在日常操作中,相信很多人在elasticsearch文档操作的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"elasticsearch文档操作的方法有哪些"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

文档

查找name=hnatao的数据

rst, _ := client.Search().Index("user").Query(elastic.NewMatchQuery("name", "hnatao")).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[    {        "_score": 1.3862942,        "_index": "user",        "_type": "_doc",        "_id": "1",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "hnatao", "age": 21, "score": 80 }    }]

查找 20 岁的 hnatao 的数据

q := elastic.NewBoolQuery().Must(    elastic.NewMatchQuery("name", "hnatao"),    elastic.NewMatchQuery("age", "20"),)rst, _ := client.Search().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[]

查找 20 岁,21 岁的所有用户信息

q := elastic.NewRangeQuery("age").Gte("20").Lte("21")rst, _ := client.Search().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "1",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "hnatao", "age": 21, "score": 80 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "5",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "guofucheng", "age": 20, "score": 0 }    }]

查找大于 21 岁的所有用户信息

q := elastic.NewRangeQuery("age").Gte("21")rst, _ := client.Search().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "1",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "hnatao", "age": 21, "score": 80 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "2",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "lqt", "age": 22, "score": 90 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "3",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "liudehua", "age": 23, "score": 85 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "4",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "zhangxueyou", "age": 24, "score": 86 }    }]

查找有得分记录的用户

q := elastic.NewExistsQuery("score")rst, _ := client.Search().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "1",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "hnatao", "age": 21, "score": 80 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "2",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "lqt", "age": 22, "score": 90 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "3",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "liudehua", "age": 23, "score": 85 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "4",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "zhangxueyou", "age": 24, "score": 86 }    },    {        "_score": 1,        "_index": "user",        "_type": "_doc",        "_id": "5",        "_seq_no": null,        "_primary_term": null,        "_source": { "name": "guofucheng", "age": 20, "score": 0 }    }]

查找没有得分记录的用户

q := elastic.NewBoolQuery().MustNot(elastic.NewExistsQuery("score"))rst, _ := client.Search().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[]

20 岁用户总人数

q := elastic.NewTermQuery("age", "20")rst, _ := client.Count().Index("user").Query(q).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

数字:1

用户的平均人数

q := elastic.NewAvgAggregation().Field("age")rst, _ := client.Search().Index("user").Aggregation("avg_age", q).Size(0).Do(ctx)fmt.Println(string(rst.Aggregations["avg_age"]))

返回

{ "value": 22.0 }

查找年龄最小的用户

rst, _ := client.Search().Index("user").Sort("age", true).Size(1).Do(ctx)buf, _ := json.Marshal(rst.Hits.Hits)fmt.Println(string(buf))

返回

[    {        "_index": "user",        "_type": "_doc",        "_id": "5",        "_seq_no": null,        "_primary_term": null,        "sort": [20],        "_source": { "name": "guofucheng", "age": 20, "score": 0 }    }]

统计年龄的各个维度

agg := elastic.NewStatsAggregation().Field("age")rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx)buf, _ := rst.Aggregations["stats_age"].MarshalJSON()fmt.Println(string(buf))

返回

{    "count": 5,    "min": 20.0,    "max": 24.0,    "avg": 22.0,    "sum": 110.0}

统计年龄占比百分位

agg := elastic.NewPercentilesAggregation().Field("age")rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx)buf, _ := rst.Aggregations["stats_age"].MarshalJSON()fmt.Println(string(buf))

返回

{    "values": {        "1.0": 20.0,        "5.0": 20.0,        "25.0": 20.75,        "50.0": 22.0,        "75.0": 23.25,        "95.0": 24.0,        "99.0": 24.0    }}

查询每个年龄的平均分数,并按年龄从小到大排序

agg := elastic.NewTermsAggregation().Field("age").    SubAggregation("avg_score", elastic.NewAvgAggregation().Field("score")).OrderByKeyAsc()rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx)buf, _ := rst.Aggregations["stats_age"].MarshalJSON()fmt.Println(string(buf))

返回

{    "doc_count_error_upper_bound": 0,    "sum_other_doc_count": 0,    "buckets": [        { "key": 20, "doc_count": 1, "avg_score": { "value": 0.0 } },        { "key": 21, "doc_count": 2, "avg_score": { "value": 85.0 } },        { "key": 22, "doc_count": 2, "avg_score": { "value": 85.5 } }    ]}

查询每个年龄的平均分数,并按平均分数从大到小排序

agg := elastic.NewTermsAggregation().Field("age").    SubAggregation("avg_score", elastic.NewAvgAggregation().Field("score")).OrderByAggregation("avg_score", false)rst, _ := client.Search().Index("user").Aggregation("stats_age", agg).Do(ctx)buf, _ := rst.Aggregations["stats_age"].MarshalJSON()fmt.Println(string(buf))

返回

{    "doc_count_error_upper_bound": 0,    "sum_other_doc_count": 0,    "buckets": [        { "key": 22, "doc_count": 2, "avg_score": { "value": 85.5 } },        { "key": 21, "doc_count": 2, "avg_score": { "value": 85.0 } },        { "key": 20, "doc_count": 1, "avg_score": { "value": 0.0 } }    ]}

到此,关于"elasticsearch文档操作的方法有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

用户 年龄 文档 方法 学习 分数 人数 信息 得分 数据 更多 帮助 排序 查询 统计 实用 最小 从小到大 接下来 从小 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 数据库就是存放表的吗 智能移动终端软件开发是什么 锐思数据库 st 国外知名的物流软件开发公司 服务器如何修改端口 app抢票软件开发 计算机网络技术工作总结 云服务器存储别的系统的数据 如何在指定文件中创建数据库 数据库安全控制包含哪些内容 软件开发与项目管理课程 网络安全600 做软件开发的人咋一句话都没有 网络安全指挥大屏 如何进入图书馆万德数据库 滨州供应链erp软件开发公司 服务器在韩国如何连接 数据库的表的字段默认值怎么定义 plsql数据库编码设置 连接数据库增删改查 黑龙江省网络安全宣传周开幕 盐城软件开发价格大全 数据库如何使用print语句 宁波一站式网络技术哪家好 国内外网络安全现状 诚海录像服务器主板 ibmx3850服务器升级 一个服务器可以运行多个app吗 网络安全为人民繁体字 网络安全实用技术第三版答案
0