{"id":578,"date":"2021-10-29T22:43:52","date_gmt":"2021-10-29T14:43:52","guid":{"rendered":"https:\/\/twocups.cn\/?p=578"},"modified":"2021-10-29T22:43:52","modified_gmt":"2021-10-29T14:43:52","slug":"46","status":"publish","type":"post","link":"https:\/\/twocups.cn\/index.php\/2021\/10\/29\/46\/","title":{"rendered":"\u3010Elastic Stack\u7cfb\u5217\u3011\u7b2c\u4e94\u7ae0\uff1a\u5de5\u4f5c\u4e2d\u5b9e\u8df5\uff08\u4e00\uff09\uff1aGolang\u64cd\u4f5cElasticsearch\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"\n<p><img width=\"80\" src=\"https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a.png\" alt=\"\">\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a href=\"https:\/\/twocups.cn\/index.php\/2021\/10\/29\/46\/\">https:\/\/twocups.cn\/index.php\/2021\/10\/29\/46\/<\/a><\/p>\n\n\n\n<h1>\u7ed3\u8bba\u5148\u884c<\/h1>\n\n\n\n<p>\u76f4\u63a5\u7528 Golang \u6a21\u62df HTTP \u8bf7\u6c42\u8bbf\u95ee Elasticsearch \u6700\u65b9\u4fbf\u4e86\uff01\uff08\u5b98\u65b9\u7684 SDK \u5c31\u662f\u7c7b\u4f3c\u7684\u505a\u6cd5\uff09<\/p>\n\n\n\n<h1>\u524d\u8a00<\/h1>\n\n\n\n<p>\u56e0\u5de5\u4f5c\u9700\u8981\uff0c\u8981\u5b9a\u671f\u4ece Elasticsearch\uff08\u4ee5\u4e0b\u7b80\u79f0es\uff09\u4e2d\u53d6\u6570\u636e\u3002\u6211\u9009\u7684\u7f16\u7a0b\u8bed\u8a00\u662f Golang\uff0c\u4e0b\u9762\u8bb2\u4e00\u4e0b\u6211\u81ea\u5df1\u5728\u7528 Golang \u67e5\u8be2 es \u6570\u636e\u7684\u65f6\u5019\u9047\u5230\u7684\u5751\u3002<\/p>\n\n\n\n<h1>\u6ce8\u610f\u4e8b\u9879<\/h1>\n\n\n\n<h2>1. Golang\u64cd\u4f5cElasticsearch\u5f15\u7528\u7684SDK<\/h2>\n\n\n\n<p>\u4e3b\u6d41\u7684SDK\u6709\u4e24\u79cd<\/p>\n\n\n\n<ul><li>github.com\/olivere\/elastic \uff08\u7b2c\u4e09\u65b9\uff09<\/li><li>github.com\/elastic\/go-elasticsearch \uff08\u5b98\u65b9\uff09<\/li><\/ul>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u5b98\u65b9\u7684 SDK \u6700\u597d\u7684\uff0c\u56e0\u4e3a\u652f\u6301\u6027\u662f\u6700\u5f3a\u7684\uff0c\u6240\u4ee5\u6211\u4e00\u822c\u90fd\u7528\u5b98\u65b9\u7ed9\u7684 SDK\u3002\u4f46\u7b2c\u4e09\u65b9SDK\u8fd8\u662f\u5f88\u706b\uff0c\u6240\u4ee5\u8fd9\u6b21\u6211\u6765\u8bd5\u8bd5\u770b\u3002\uff08\u4ece\u4f53\u9a8c\u7ed3\u679c\u4e0a\u6765\u8bf4\uff0c\u8fd8\u662f\u5b98\u65b9\u7684\u597d\u7528\uff09<\/p>\n\n\n\n<h2>2. SDK\u7248\u672c\u548cElasticsearch\u7248\u672c\u8981\u5bf9\u9f50<\/h2>\n\n\n\n<p>\u5982\u679c\u4e0d\u5bf9\u9f50\uff0c\u4f7f\u7528 SDK \u5411 es \u8bf7\u6c42\u7684\u65f6\u5019\u5c31\u4f1a\u62a5\u9519\u3002\u4e00\u822c SDK \u548c es \u7684\u7248\u672c\u53f7\u662f\u76f8\u540c\u7684\uff0c\u4f8b\u59825.x\u30016.x\u30017.x\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0d\u77e5\u9053\u76ee\u6807 es \u7684\u7248\u672c\u662f\u591a\u5c11\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728 es \u91cc\u9762\u67e5\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl http:\/\/es_ip:port<br>\u200b<br>{<br> &nbsp;\"name\" : \"node-1\",<br> &nbsp;\"cluster_name\" : \"elasticsearch\",<br> &nbsp;\"cluster_uuid\" : \"\",<br> &nbsp;\"version\" : {<br> &nbsp; &nbsp;\"number\" : \"7.13.1\",<br> &nbsp; &nbsp;\"build_flavor\" : \"default\",<br> &nbsp; &nbsp;\"build_type\" : \"tar\",<br> &nbsp; &nbsp;\"build_hash\" : \"\",<br> &nbsp; &nbsp;\"build_date\" : \"2021-05-28T17:40:59.346932922Z\",<br> &nbsp; &nbsp;\"build_snapshot\" : false,<br> &nbsp; &nbsp;\"lucene_version\" : \"8.8.2\",<br> &nbsp; &nbsp;\"minimum_wire_compatibility_version\" : \"6.8.0\",<br> &nbsp; &nbsp;\"minimum_index_compatibility_version\" : \"6.0.0-beta1\"<br>  },<br> &nbsp;\"tagline\" : \"You Know, for Search\"<br>}<\/code><\/pre>\n\n\n\n<p>\u6211\u8fd9\u4e2a\u6d4b\u8bd5\u73af\u5883\u91cc\u9762\u7684 es \u7248\u672c\u5c31\u662f7.13.1\uff0c\u7136\u540e\u4e0b\u8f7d\u5bf9\u5e94\u7684 SDK \u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<h2>3. \u521b\u5efaElasticsearch\u5ba2\u6237\u7aef\u5931\u8d25<\/h2>\n\n\n\n<p>\u6709\u4eba\u5728\u521b\u5efa es \u5ba2\u6237\u7aef\u7684\u65f6\u5019\u4f1a\u9047\u5230\u8fd9\u4e2a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>context deadline exceeded<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a github.com\/olivere\/elastic \u8fd9\u4e2a\u5305\u4f1a\u81ea\u52a8\u8f6c\u6362\u8fde\u63a5\u5730\u5740\uff0c\u6240\u4ee5\u9700\u8981\u8bbe\u7f6e elastic.SetSniff \u4e3a false\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efaclient\u8fde\u63a5ES<br>client, err := elastic.NewClient(<br> &nbsp; &nbsp;elastic.SetURL(esUrl),<br>   &nbsp;elastic.SetBasicAuth(esId, esKey),<br>   &nbsp;\/\/ \u8fd9\u91cc\u8bbe\u7f6e\u6210\u4e0d\u9700\u8981\u5730\u5740\u81ea\u52a8\u8f6c\u6362\uff0c\u5426\u5219\u4f1a\u62a5\u9519context deadline exceeded<br>   &nbsp;elastic.SetSniff(false),<br>)<\/code><\/pre>\n\n\n\n<h2>4. Elasticsearch\u67e5\u8be2\u7ed3\u679c\u7684\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/h2>\n\n\n\n<p>\u4f7f\u7528 Golang \u7ed9 es \u505a\u67e5\u8be2\u662f\u8fd9\u6837\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>response, err := client.Get().<br>    Index(index).Type(type).<br>    Id(id).<br>    Do(context.Background())<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u8fd9\u4e2a response \u7684\u6570\u636e\u683c\u5f0f\u662f *GetService\uff0c\u5e76\u4e14\u67e5\u8be2\u5230\u7684\u6570\u636e\u662f\u5728 response.Source \u4e2d\u3002\u800c\u8fd9\u4e2a response.Source \u7684\u6570\u636e\u7ed3\u6784\u662f *json.RawMessage\uff0c\u8fd9\u662f\u4e00\u79cd\u539f\u59cb\u7684 json \u7ed3\u6784\uff0c\u4e0d\u80fd\u76f4\u63a5\u8bfb\u3002\u4f46\u6bd5\u7adf\u7b97\u662f json\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u628a\u8fd9\u4e2a json \u91cc\u9762\u7684\u9700\u8981\u7684\u6570\u636e\u6620\u5c04\u51fa\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Data struct {<br>    Name string `json:\"name\"`<br>    Age string `json:\"age\"`<br>}<br>\u200b<br>func GetData(client *elastic.Client, index string) {<br>   &nbsp;res, err := client.Get().Index(index).Type(\"_doc\").Id(\"userTotal-2021-10-14\").Do(context.Background())<br>    if err != nil {<br>        fmt.Println(\"\u67e5\u8be2\u6570\u636e\u9519\u8bef\")<br>        return<br>    }<br>\u200b<br>    if res.Found {<br>        var msg ProxyData<br>        \/\/ \u8fd9\u91cc\u522b\u5fd8\u4e86*\uff0c\u56e0\u4e3ares.Source\u662f\u5730\u5740<br> &nbsp; &nbsp;    if err := json.Unmarshal(*res.Source, &amp;msg); err != nil {<br>            fmt.Println(\"\u8f6c\u6362\u6210json\u5931\u8d25\")<br> &nbsp; &nbsp; &nbsp; &nbsp;    return<br>        }<br>        fmt.Println(msg)<br>    }<br>}<\/code><\/pre>\n\n\n\n<h1>\u76f4\u63a5\u7528HTTP\u8bf7\u6c42\u8bbf\u95eeElasticsearch<\/h1>\n\n\n\n<p>\u5c31\u6211\u76ee\u524d\u4e09\u79cd Golang \u64cd\u4f5c es \u7684\u65b9\u5f0f\uff08\u5b98\u65b9 SDK\u3001\u7b2c\u4e09\u65b9 SDK\u3001\u76f4\u63a5HTTP\u8bf7\u6c42\uff09\u4f7f\u7528\u4e0b\u6765\uff0c\u6700\u65b9\u4fbf\u7684\u5176\u5b9e\u8fd8\u662f\u76f4\u63a5\u7528 HTTP \u8bf7\u6c42\u76f4\u63a5\u8bbf\u95ee\u3002\u56e0\u4e3a\u4f7f\u7528 HTTP \u76f4\u63a5\u8bbf\u95ee\u53ef\u4ee5\u76f4\u63a5\u7528 es \u7684\u67e5\u8be2\u8bed\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u53bb\u5b66\u4e60 SDK \u7684\u903b\u8f91\u3002\u7b2c\u4e8c\u65b9\u4fbf\u7684\u5c31\u662f\u5b98\u65b9\u7684 SDK\uff0c\u73b0\u5728\u6700\u65b0\u7684\u5b98\u7f51\u4e0a\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528 es \u7684\u8bed\u6cd5\uff0c\u800c\u4e0d\u7528\u7814\u7a76 SDK \u7684\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u3002\u6700\u540e\u624d\u662f\u7b2c\u4e09\u65b9\u5305\u3002<\/p>\n\n\n\n<p>\u6211\u6709\u4e4b\u524d\u81ea\u5df1\u5c01\u88c5\u597d\u7684 HTTP \u8bf7\u6c42\u7684\u4ee3\u7801\uff0c\u800c\u4e14\u8fd9\u79cd\u5bf9 es \u7684\u64cd\u4f5c\u662f\u7edd\u5927\u90e8\u5206\u60c5\u51b5\u662f\u4e0d\u9700\u8981 tcp \u53bb\u4fdd\u6301\u8fde\u63a5\u7684\u3002\u6240\u4ee5\u5bf9\u4e8e\u6211\u6765\u8bf4\uff0cGolang \u76f4\u63a5\u901a\u8fc7 HTTP \u8bf7\u6c42 es \u662f\u6700\u597d\u7528\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/twocups.cn\/index.php\/202 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":474,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[99],"tags":[105,127],"_links":{"self":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":0,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media\/474"}],"wp:attachment":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}