golang

Elasticsearch批量insert和批量upsert

月盾
golang版本的elasticsearch批量插入和批量更新方法如下: package main import ( "github.com/elastic/go-elasticsearch/v8" "github.com/elastic/go-elasticsearch/v8/esutil" ) func main() { list:=make(User, 0) bulkES(list) } func bulkES(list []User) error { indexer, err := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{ Index: "search-user", Client: ES, }) if err != nil { return err } for _, v := range list { data, err := json.Marshal(v) if err != nil { return err } err = indexer.Add( context.Background(), esutil.BulkIndexerItem{ Action: "index", Body: bytes.NewReader(data), }, ) if err != nil { return err } } indexer.

go http响应乱码

月盾
golang请求接口返回的数据乱码,原因之一是请求头设置了"Accept-Encoding": "gzip, deflate, br",那么如果服务器支持的话响应的数据就会经过gzip,deflate,br等方式的压缩,解决方式是对数据解压,或者可以不设置接收方式,即"Accept-Encoding": "" import "github.com/andybalholm/brotli" import "compress/flate" import "compress/gzip" // 检测返回的body是否经过压缩,并返回解压的内容 func contentDecoding(res *http.Response) (bodyReader io.Reader, err error) { switch res.Header.Get("Content-Encoding") { case "gzip": bodyReader, err = gzip.NewReader(res.Body) case "deflate": bodyReader = flate.NewReader(res.Body) case "br": bodyReader = brotli.NewReader(res.Body) default: bodyReader = res.Body } return }