千家信息网

elasticsearch中怎么使用update更新文档

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"elasticsearch中怎么使用update更新文档",在日常操作中,相信很多人在elasticsearch中怎么使用update更新文档问题上存在疑惑,小编查阅了各式资料,整理
千家信息网最后更新 2026年11月02日elasticsearch中怎么使用update更新文档

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

文档

添加数据

如果数据不存在,会自动创建

rst, _ := client.Index().Index("user").BodyJson(&User{Name: "hnatao", Age: 20}).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "_index": "user",    "_type": "_doc",    "_id": "iL1nWHQBIsMSghaJZ0p9",    "_version": 1,    "result": "created",    "_shards": { "total": 4, "successful": 1, "failed": 0 },    "_primary_term": 1}

添加指定 id 的数据

指定 _id = "1"

rst, _ := client.Index().Index("user").Id("1").BodyJson(&User{Name: "lqt", Age: 22}).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "_index": "user",    "_type": "_doc",    "_id": "1",    "_version": 1,    "result": "created",    "_shards": { "total": 4, "successful": 1, "failed": 0 },    "_seq_no": 1,    "_primary_term": 1}

更新数据

_id = "1" 已经存在,修改年龄,该操作会导致其他字段为空,因为该操作属于覆盖操作

rst, _ := client.Index().Index("user").Id("1").BodyJson(&User{Age: 23}).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "_index": "user",    "_type": "_doc",    "_id": "1",    "_version": 2,    "result": "updated",    "_shards": { "total": 4, "successful": 1, "failed": 0 },    "_seq_no": 2,    "_primary_term": 1}

使用_update更新文档

使用map[string]interface{}更新字段

rst, _ := client.Update().Index("user").Id("1").Doc(map[string]interface{}{"age":25}).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "_index": "user",    "_type": "_doc",    "_id": "1",    "_version": 7,    "result": "updated",    "_shards": { "total": 4, "successful": 1, "failed": 0 },    "_seq_no": 7,    "_primary_term": 1}

使用_update_by_query更新文档

q := elastic.NewMatchQuery("_id", "1")sc := elastic.NewScript("ctx._source.age=21")rst, _ := client.UpdateByQuery("user").Query(q).Script(sc).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "took": 5,    "timed_out": false,    "total": 1,    "updated": 1,    "deleted": 0,    "batches": 1,    "version_conflicts": 0,    "noops": 0,    "retries": { "bulk": 0, "search": 0 },    "throttled": "",    "throttled_millis": 0,    "requests_per_second": -1,    "throttled_until": "",    "throttled_until_millis": 0,    "failures": []}

通过 _bulk 批量添加文档

bulkReq1 := elastic.NewBulkIndexRequest().Id("2").Doc(&User{Name: "张三", Age: 21})bulkReq2 := elastic.NewBulkIndexRequest().Id("3").Doc(&User{Name: "李四", Age: 22})rst, _ := client.Bulk().Index("user").Add(bulkReq1, bulkReq2).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "took": 3,    "items": [        {            "index": {                "_index": "user",                "_type": "_doc",                "_id": "2",                "_version": 1,                "result": "created",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 19,                "_primary_term": 1,                "status": 201            }        },        {            "index": {                "_index": "user",                "_type": "_doc",                "_id": "3",                "_version": 1,                "result": "created",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 20,                "_primary_term": 1,                "status": 201            }        }    ]}

通过 _bulk 批量更新文档

bulkReq1 := elastic.NewBulkUpdateRequest().Index("user").Id("2").Doc(map[string]interface{}{"age": 31})bulkReq2 := elastic.NewBulkUpdateRequest().Index("user").Id("3").Doc(map[string]interface{}{"age": 31})rst, _ := client.Bulk().Add(bulkReq1, bulkReq2).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "took": 7,    "items": [        {            "update": {                "_index": "user",                "_type": "_doc",                "_id": "2",                "_version": 2,                "result": "updated",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 21,                "_primary_term": 1,                "status": 200            }        },        {            "update": {                "_index": "user",                "_type": "_doc",                "_id": "3",                "_version": 2,                "result": "updated",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 22,                "_primary_term": 1,                "status": 200            }        }    ]}

通过 _bulk 批量删除文档

bulkReq1 := elastic.NewBulkDeleteRequest().Index("user").Id("2")bulkReq2 := elastic.NewBulkDeleteRequest().Index("user").Id("3")rst, _ := client.Bulk().Add(bulkReq1, bulkReq2).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "took": 130,    "items": [        {            "delete": {                "_index": "user",                "_type": "_doc",                "_id": "2",                "_version": 3,                "result": "deleted",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 23,                "_primary_term": 1,                "status": 200            }        },        {            "delete": {                "_index": "user",                "_type": "_doc",                "_id": "3",                "_version": 3,                "result": "deleted",                "_shards": { "total": 4, "successful": 1, "failed": 0 },                "_seq_no": 24,                "_primary_term": 1,                "status": 200            }        }    ]}

按照 _id 升序排序,取前 2 个数据

rst, _ := client.Search().Index("user").Sort("_id", false).Size(2).From(0).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "took": 439,    "hits": {        "total": { "value": 8, "relation": "eq" },        "hits": [            {                "_index": "user",                "_type": "_doc",                "_id": "5",                "_seq_no": null,                "_primary_term": null,                "sort": ["1"],                "_source": { "name": "lqt", "age": 21 }            },            {                "_index": "user",                "_type": "_doc",                "_id": "4",                "_seq_no": null,                "_primary_term": null,                "sort": ["2"],                "_source": { "name": "张三", "age": 21 }            }        ]    },    "_shards": { "total": 1, "succeful": 1, "failed": 0 }}

按照字段值排序

年龄降序,_id升序,前 5 条数据

rst, _ := client.Search().Index("user").SortBy(elastic.NewFieldSort("age").Desc(), elastic.NewFieldSort("_id").Asc()).Size(5).From(0).Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "hits": {        "total": { "value": 8, "relation": "eq" },        "hits": [            {                "_index": "user",                "_type": "_doc",                "_id": "5",                "_seq_no": null,                "_primary_term": null,                "sort": [24, "5"],                "_source": { "name": "张学友", "age": 24 }            },            {                "_index": "user",                "_type": "_doc",                "_id": "4",                "_seq_no": null,                "_primary_term": null,                "sort": [23, "4"],                "_source": { "name": "刘德华", "age": 23 }            },            {                "_index": "user",                "_type": "_doc",                "_id": "3",                "_seq_no": null,                "_primary_term": null,                "sort": [22, "3"],                "_source": { "name": "李四", "age": 22 }            },            {                "_index": "user",                "_type": "_doc",                "_id": "1",                "_seq_no": null,                "_primary_term": null,                "sort": [21, "1"],                "_source": { "name": "lqt", "age": 21 }            },            {                "_index": "user",                "_type": "_doc",                "_id": "2",                "_seq_no": null,                "_primary_term": null,                "sort": [21, "2"],                "_source": { "name": "张三", "age": 21 }            }        ]    },    "_shards": { "total": 1, "successful": 1, "failed": 0 }}

查询结果只展示部分字段

rst, _ := client.Search().Index("user").FilterPath("hits.hits._id", "hits.hits._source.name").Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "hits": {        "hits": [            {                "_id": "1",                "_seq_no": null,                "_primary_term": null,                "_source": { "name": "lqt" }            },            {                "_id": "2",                "_seq_no": null,                "_primary_term": null,                "_source": { "name": "张三" }            },            {                "_id": "3",                "_seq_no": null,                "_primary_term": null,                "_source": { "name": "李四" }            },            {                "_id": "4",                "_seq_no": null,                "_primary_term": null,                "_source": { "name": "刘德华" }            },            {                "_id": "5",                "_seq_no": null,                "_primary_term": null,                "_source": { "name": "张学友" }            }        ]    }}

根据_id查询数据

rst, _ := client.Get().Index("user").Id("1").Do(ctx)buf, _ := json.Marshal(rst)fmt.Println(string(buf))

返回

{    "_index": "user",    "_type": "_doc",    "_id": "1",    "_uid": "",    "_routing": "",    "_parent": "",    "_version": 5,    "_seq_no": 5,    "_primary_term": 1,    "_source": { "name": "", "age": 23 },    "found": true}

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

文档 更新 数据 字段 张三 学习 李四 升序 年龄 张学友 更多 刘德华 帮助 排序 查询 实用 接下来 文章 方法 理论 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 小米移动互联网科技园 上海喔噻互联网科技有限公司 互联网的科技 智能科技公司是互联网公司吗 互联网 科技创业 浩特互联网科技 深圳天众互联网科技公司 互联网金融 科技创新 互联网科技公司有哪些 多迪互联网科技有限公司 广州优美互联网科技 字节跳动和互联网科技 互联网科技有哪些公司 成都互联网科技公司工作好不好 互联网科技和法律行业 广州千川互联网科技有限公司 皮皮虾互联网科技有限公司 科技互联网行业投资研究 互联网科技机房 数字科技和互联网金融哪个好 善班互联网科技有限公司 互联网科技公司招聘 昭通云南众福互联网科技有限公司 电子科技大学互联网创业大赛 洛阳共同富裕互联网科技有限公司 互联网科技的全部知识 爱医互联网络科技 启辰互联网科技 萌小萌互联网科技河南有限公司 科技发展第三阶段互联网时代
0