{"id":43075,"date":"2024-12-01T09:51:53","date_gmt":"2024-12-01T01:51:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43075\/"},"modified":"2024-12-01T09:51:53","modified_gmt":"2024-12-01T01:51:53","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-golang-%e4%b8%ad%e4%bd%bf%e7%94%a8-olivere-%e5%b0%86%e6%95%b0%e6%8d%ae%e6%8f%92%e5%85%a5-elasticsearch","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43075\/","title":{"rendered":"\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch<\/span><\/p>\n<h1>\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 10:24:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u9700\u8981\u4e00\u4e2a\u6b63\u786e\u7684\u793a\u4f8b\uff0c\u5728\u5176\u4e2d\u6211\u53ef\u4ee5\u4f7f\u7528 golang \u4e2d\u7684 olivere \u5c06\u6570\u636e\u63d2\u5165\u5230 elasticsearch \u4e2d\u3002\u6211\u5df2\u7ecf\u6536\u96c6\u4e86\u6570\u636e\uff0c\u5e76\u6253\u5370\u4e86\u5c06\u63d2\u5165\u7684\u6bcf\u4e2a\u6570\u636e\u7684\u7ed3\u679c\u3002\u95ee\u9898\u662f\u6ca1\u6709\u6570\u636e\u63d2\u5165\u5230elasticsearch\u4e2d\u3002 <\/p>\n<p>\u8fd9\u662f\u6536\u96c6\u5c06\u63d2\u5165\u5230elasticsearch\u4e2d\u7684\u6570\u636e\u7684\u4ee3\u7801\u3002 <\/p>\n<pre>indexing := company\nindextype := employee \ndata := lists{Id: id__,\n            Article_id:      id_row,\n            Category_id:     category_id,\n            Datee:           date,\n            Media_id:        media,\n            Mention_times:   mention,\n            Data_input_date: data_input_date,\n            User:           user,\n            name:         pc_name}\n\n\/\/Save to Elastic using olivere \nresponse := elastic.NewBulkIndexRequest().Index(indexing).Type(indextype).Id(id__).Doc(data)  \nif response != nil {\n        fmt.Println(\"Print the result\", OlivereResponse)\n    } \n\/* log the result \n Print the result for inserting to elastic using olivere {\"index\": \n{\"_index\":\"company\",\"_id\":\"2660e1\",\"_type\":\"employee\"}}\n{\"id\":\"26688\",\"article_id\":20,\"category_id\":\"farming\",\"datee\":\"2018-11-23\",\"media_id\":8, Mention_times: \"20\", \"data_input_date\":\"2018-12-01 15:42:22\",\"usere\":\"robot-kwd41\",\"pc_name\":\"server\"}\n*\/\n\n\n\n if OlivereResponse == nil {\n       fmt.Println(\"Response returns null when inserting to elasticsearch using \n          olivere\")\n    }<\/pre>\n<p>\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5982\u4f55\u4f7f\u7528 golang \u4e2d\u7684 olivere \u5305\u5c06\u6570\u636e\u63d2\u5165\u5230 elasticsearch \u4e2d\uff1f <\/p>\n<p>\u8c22\u8c22 }<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8981\u5c06\u6570\u636e\u63d2\u5165 elastic\uff0c\u6211\u4f7f\u7528\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>func (e *Elastic) SaveDocument(index string, id string, data interface{}) error {\n    _, err := e.client.Index().Index(index).Type(\"entity\").Id(id).\n        BodyJson(data).Refresh(\"true\").Do(e.ctx)\n\n    if err != nil {\n        e.fileLogger.Log(data)\n        fmt.Printf(\"Error save document to elastic search: %s. Save log to file\\n\", err.Error())\n    }\n\n    return err\n}<\/pre>\n<p>\u8fd9\u91cc\u662f\uff1a<\/p>\n<ul>\n<li>e.client \u662f f *elastic.client \u7684\u5b9e\u4f8b\uff1b<\/li>\n<li>e.ctx \u662f\u4e00\u4e2a context.context<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch \u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch \u6765\u6e90\uff1astackoverflow 2024-04-29 10:24:33 0\u6d4f\u89c8 \u6536\u85cf \u5b66\u4e60Golang\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Olivere \u5c06\u6570\u636e\u63d2\u5165 Elasticsearch\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60Golang\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u9700\u8981\u4e00\u4e2a\u6b63\u786e\u7684\u793a\u4f8b\uff0c\u5728\u5176\u4e2d\u6211\u53ef\u4ee5\u4f7f\u7528 golang \u4e2d\u7684 olivere \u5c06\u6570\u636e\u63d2\u5165\u5230 elasticsearch \u4e2d\u3002\u6211\u5df2\u7ecf\u6536\u96c6\u4e86\u6570\u636e\uff0c\u5e76\u6253\u5370\u4e86\u5c06\u63d2\u5165\u7684\u6bcf\u4e2a\u6570\u636e\u7684\u7ed3\u679c\u3002\u95ee\u9898\u662f\u6ca1\u6709\u6570\u636e\u63d2\u5165\u5230elasticsearch\u4e2d\u3002 \u8fd9\u662f\u6536\u96c6\u5c06\u63d2\u5165\u5230elasticsearch\u4e2d\u7684\u6570\u636e\u7684\u4ee3\u7801\u3002 indexing := company indextype := employee data := lists{Id: id__, Article_id: id_row, Category_id: category_id, Datee: date, Media_id: media, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43075","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43075"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43075\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}