In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use update to update documents in elasticsearch". In daily operation, I believe many people have doubts about how to use update to update documents in elasticsearch. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use update to update documents in elasticsearch". Next, please follow the editor to study!
Document add data
If the data does not exist, it is automatically created
Rst, _: = client.Index (). Index ("user"). BodyJson (& User {Name: "hnatao", Age: 20}) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"_ index": "user", "_ type": "_ doc", "_ id": "iL1nWHQBIsMSghaJZ0p9", "_ version": 1, "result": "created", "_ shards": {"total": 4, "successful": 1, "failed": 0}, "_ primary_term": 1} add data for the specified id
Specify _ 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))
Return
{"_ index": "user", "_ type": "_ doc", "_ id": "1", "_ version": 1, "result": "created", "_ shards": {"total": 4, "successful": 1, "failed": 0}, "_ seq_no": 1, "_ primary_term": 1} update data
_ id = "1" already exists, modify the age, this operation will cause other fields to be empty, because the operation belongs to an override operation
Rst, _: = client.Index (). Index ("user"). Id ("1"). BodyJson (& User {Age: 23}) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"_ index": "user", "_ type": "_ doc", "_ id": "1", "_ version": 2, "result": "updated", "_ shards": {"total": 4, "successful": 1, "failed": 0}, "_ seq_no": 2, "_ primary_term": 1} update the document with _ update
Update fields using 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))
Return
{"_ index": "user", "_ type": "_ doc", "_ id": "1", "_ version": 7, "result": "updated", "_ shards": {"total": 4, "successful": 1, "failed": 0}, "_ seq_no": 7 "_ primary_term": 1} use _ update_by_query to update document 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))
Return
{"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": []} add documents bulkReq1: = elastic.NewBulkIndexRequest () .Id ("2") in batch via _ bulk. Doc (& User {Name: "Zhang San", Age: 21}) bulkReq2: = elastic.NewBulkIndexRequest () .Id ("3") .Doc (& User {Name: "Li Si", Age: 22}) rst _: = client.Bulk () .Index ("user") .add (bulkReq1, bulkReq2) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"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 Update the document bulkReq1: = elastic.NewBulkUpdateRequest () .Index ("user") .Id ("2") .Doc (interface {} {"age": 31}) bulkReq2: = elastic.NewBulkUpdateRequest () .Index ("user") .Id ("3") .Doc (map [string] interface {} {"age": 31}) rst via _ bulk _: = client.Bulk () .Add (bulkReq1, bulkReq2) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"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}}]} batch delete documents 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))
Return
{"took": 130,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}}]} sort by _ id ascending order Take the first two data rst, _: = client.Search (). Index ("user"). Sort ("_ id", false) .size (2) .From (0) .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"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": "Zhang San" "age": 21}}]}, "_ shards": {"total": 1, "succeful": 1, "failed": 0}} sort by field value
Age descending order, _ id ascending order, first 5 items of data
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))
Return
{"hits": {"total": {"value": 8, "relation": "eq"}, "hits": [{"_ index": "user", "_ type": "_ doc", "_ id": "5", "_ seq_no": null "_ primary_term": null, "sort": [24, "5"], "_ source": {"name": "Jacky Cheung", "age": 24}}, {"_ index": "user", "_ type": "_ doc" "_ id": "4", "_ seq_no": null, "_ primary_term": null, "sort": [23, "4"], "_ source": {"name": "Andy Lau", "age": 23}} {"_ index": "user", "_ type": "_ doc", "_ id": "3", "_ seq_no": null, "_ primary_term": null, "sort": [22, "3"] "_ source": {"name": "Li Si", "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": "Zhang San", "age": 21}}]} "_ shards": {"total": 1, "successful": 1, "failed": 0}} query results show only some fields rst, _: = client.Search (). Index ("user"). FilterPath ("hits.hits._id", "hits.hits._source.name") .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"hits": {"hits": [{"_ id": "1", "_ seq_no": null, "_ primary_term": null, "_ source": {"name": "lqt"} {"_ id": "2", "_ seq_no": null, "_ primary_term": null, "_ source": {"name": "Zhang San"}}, {"_ id": "3" "_ seq_no": null, "_ primary_term": null, "_ source": {"name": "Li Si"}}, {"_ id": "4", "_ seq_no": null, "_ primary_term": null "_ source": {"name": "Andy Lau"}}, {"_ id": "5", "_ seq_no": null, "_ primary_term": null "_ source": {"name": "Jacky Cheung"} query data rst according to _ id, _: = client.Get (). Index ("user"). Id ("1") .Do (ctx) buf, _: = json.Marshal (rst) fmt.Println (string (buf))
Return
{"_ index": "user", "_ type": "_ doc", "_ id": "1", "_ uid": "", "_ routing": "," _ parent ":", "_ version": 5, "_ seq_no": 5, "_ primary_term": 1, "_ source": {"name": "," age ": 23} "found": true} so far The study on "how to use update to update documents in elasticsearch" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.