# go-elasticsearch
The official Go client for [Elasticsearch](https://www.elastic.co/products/elasticsearch).
[![GoDoc](https://godoc.org/github.com/elastic/go-elasticsearch?status.svg)](https://pkg.go.dev/github.com/elastic/go-elasticsearch/v8)
[![Go Report Card](https://goreportcard.com/badge/github.com/elastic/go-elasticsearch)](https://goreportcard.com/report/github.com/elastic/go-elasticsearch)
[![codecov.io](https://codecov.io/github/elastic/go-elasticsearch/coverage.svg?branch=master)](https://codecov.io/gh/elastic/go-elasticsearch?branch=master)
[![Build](https://github.com/elastic/go-elasticsearch/workflows/Build/badge.svg)](https://github.com/elastic/go-elasticsearch/actions?query=branch%3Amaster)
[![Unit](https://github.com/elastic/go-elasticsearch/workflows/Unit/badge.svg)](https://github.com/elastic/go-elasticsearch/actions?query=branch%3Amaster)
[![Integration](https://github.com/elastic/go-elasticsearch/workflows/Integration/badge.svg)](https://github.com/elastic/go-elasticsearch/actions?query=branch%3Amaster)
[![API](https://github.com/elastic/go-elasticsearch/workflows/API/badge.svg)](https://github.com/elastic/go-elasticsearch/actions?query=branch%3Amaster)
## Compatibility
The client major versions correspond to the compatible Elasticsearch major versions: to connect to Elasticsearch `7.x`, use a [`7.x`](https://github.com/elastic/go-elasticsearch/tree/7.x) version of the client, to connect to Elasticsearch `6.x`, use a [`6.x`](https://github.com/elastic/go-elasticsearch/tree/6.x) version of the client.
When using Go modules, include the version in the import path, and specify either an explicit version or a branch:
require github.com/elastic/go-elasticsearch/v7 7.x
require github.com/elastic/go-elasticsearch/v7 7.0.0
It's possible to use multiple versions of the client in a single project:
// go.mod
github.com/elastic/go-elasticsearch/v6 6.x
github.com/elastic/go-elasticsearch/v7 7.x
// main.go
import (
elasticsearch6 "github.com/elastic/go-elasticsearch/v6"
elasticsearch7 "github.com/elastic/go-elasticsearch/v7"
)
// ...
es6, _ := elasticsearch6.NewDefaultClient()
es7, _ := elasticsearch7.NewDefaultClient()
The `master` branch of the client is compatible with the current `master` branch of Elasticsearch.
<!-- ----------------------------------------------------------------------------------------------- -->
## Installation
Add the package to your `go.mod` file:
require github.com/elastic/go-elasticsearch/v8 master
Or, clone the repository:
git clone --branch master https://github.com/elastic/go-elasticsearch.git $GOPATH/src/github.com/elastic/go-elasticsearch
A complete example:
```bash
mkdir my-elasticsearch-app && cd my-elasticsearch-app
cat > go.mod <<-END
module my-elasticsearch-app
require github.com/elastic/go-elasticsearch/v8 master
END
cat > main.go <<-END
package main
import (
"log"
"github.com/elastic/go-elasticsearch/v8"
)
func main() {
es, _ := elasticsearch.NewDefaultClient()
log.Println(elasticsearch.Version)
log.Println(es.Info())
}
END
go run main.go
```
<!-- ----------------------------------------------------------------------------------------------- -->
## Usage
The `elasticsearch` package ties together two separate packages for calling the Elasticsearch APIs and transferring data over HTTP: `esapi` and `estransport`, respectively.
Use the `elasticsearch.NewDefaultClient()` function to create the client with the default settings.
```golang
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
log.Println(res)
// [200 OK] {
// "name" : "node-1",
// "cluster_name" : "go-elasticsearch"
// ...
```
> NOTE: It is _critical_ to both close the response body _and_ to consume it, in order to re-use persistent TCP connections in the default HTTP transport. If you're not interested in the response body, call `io.Copy(ioutil.Discard, res.Body)`.
When you export the `ELASTICSEARCH_URL` environment variable,
it will be used to set the cluster endpoint(s). Separate multiple adresses by a comma.
To set the cluster endpoint(s) programatically, pass a configuration object
to the `elasticsearch.NewClient()` function.
```golang
cfg := elasticsearch.Config{
Addresses: []string{
"http://localhost:9200",
"http://localhost:9201",
},
// ...
}
es, err := elasticsearch.NewClient(cfg)
```
To set the username and password, include them in the endpoint URL,
or use the corresponding configuration options.
```golang
cfg := elasticsearch.Config{
// ...
Username: "foo",
Password: "bar",
}
```
To set a custom certificate authority used to sign the certificates of cluster nodes,
use the `CACert` configuration option.
```golang
cert, _ := ioutil.ReadFile(*cacert)
cfg := elasticsearch.Config{
// ...
CACert: cert,
}
```
To configure other HTTP settings, pass an [`http.Transport`](https://golang.org/pkg/net/http/#Transport)
object in the configuration object.
```golang
cfg := elasticsearch.Config{
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS11,
// ...
},
// ...
},
}
```
See the [`_examples/configuration.go`](_examples/configuration.go) and
[`_examples/customization.go`](_examples/customization.go) files for
more examples of configuration and customization of the client.
See the [`_examples/security`](_examples/security) for an example of a security configuration.
The following example demonstrates a more complex usage. It fetches the Elasticsearch version from the cluster, indexes a couple of documents concurrently, and prints the search results, using a lightweight wrapper around the response body.
```golang
// $ go run _examples/main.go
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"strconv"
"strings"
"sync"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
func main() {
log.SetFlags(0)
var (
r map[string]interface{}
wg sync.WaitGroup
)
// Initialize a client with the default settings.
//
// An `ELASTICSEARCH_URL` environment variable will be used when exported.
//
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
// 1. Get cluster info
//
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
// Check response status
if res.IsError() {
log.Fatalf("Error: %s", res.String())
}
// Deserialize the response into a map.
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
// Print client and server version numbers.
log.Printf("Client: %s", elasticsearch.Version)
log.Printf("Server: %s", r["version"].(map[string]interface{})["number"])
log.Println(strings.Repeat("~", 37))
// 2. Index documents concurrently
//
for i, title := range []string{"Test One", "Test Two"} {
wg.Add(1)
go func(i int, title string) {
defer wg.Done()
// Build the request body.
var b strings.Builder
b.WriteString(`{"title" : "`)
b.WriteString(title)
b.WriteString(`"}`)
// Set up the request object.
req := esapi.IndexRequest{
Index: "test",
DocumentID: strconv.Itoa(i + 1),
Body: strings.NewReader(b.String()),
Refresh: "true",
}
// Perform the request with the client.
res, err := req.Do(context.Background(), es)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
没有合适的资源?快使用搜索试试~ 我知道了~
Elasticsearch的官方Go客户-Golang开发
共1339个文件
go:851个
asciidoc:353个
yml:26个
需积分: 33 3 下载量 36 浏览量
2021-05-26
14:13:29
上传
评论
收藏 2.8MB ZIP 举报
温馨提示
go-elasticsearch Elasticsearch的官方Go客户。 兼容性客户端主要版本与兼容的Elasticsearch主要版本相对应:要连接到Elasticsearch 7.x,请使用7.x版本go-elasticsearch Elasticsearch的官方Go客户端。 兼容性客户端主要版本与兼容的Elasticsearch主要版本相对应:要连接到Elasticsearch 7.x,请使用客户端的7.x版本,要连接到Elasticsearch 6.x,请使用客户端的6.x版本。 使用Go模块时,请在导入路径中包含版本,并指定显式版本或分支:require github.com/elastic/go-elasticsearch/v7 7.x需要github.com/elastic/go-elas
资源详情
资源评论
资源推荐
收起资源包目录
Elasticsearch的官方Go客户-Golang开发 (1339个子文件)
6cd083045bf06e80b83889a939a18451.asciidoc 2KB
installation.asciidoc 2KB
overview.asciidoc 2KB
22334f4b24bb8977d3e1bf2ffdc29d3f.asciidoc 1KB
4a2080ae55d931eb0643cc3eb91ec524.asciidoc 1KB
6aa2941855d13f365f70aa8767ecb137.asciidoc 1KB
eada8af6588584ac88f1e5b15f4a5c2a.asciidoc 1KB
d2f52c106685bd8eab47e11d644d7a70.asciidoc 1KB
f454e3f8ad5f5bd82a4a25af7dee9ca1.asciidoc 1KB
2fe28d9a91b3081a9ec4601af8fb7b1c.asciidoc 1003B
51b40610ae05730b4c6afd25647d7ae0.asciidoc 947B
9a02bd47c000a3d9a8911233c37c890f.asciidoc 921B
873fbbc6ab81409058591385fd602736.asciidoc 909B
6464124d1677f4552ddddd95a340ca3a.asciidoc 881B
4ba86373e13e106de044f190343be328.asciidoc 839B
06afce2955f9094d96d27067ebca32e8.asciidoc 836B
711443504b69d0d296e717c716a223e2.asciidoc 836B
edcfadbfb14d97a2f5e6e21ef7039818.asciidoc 835B
8cd00a3aba7c3c158277bc032aac2830.asciidoc 819B
8c8b5224befab7804461c7e7b6086d9a.asciidoc 792B
09d0e8454a413cfce28212a841b2f2bd.asciidoc 782B
3cd50a789b8e1f0ebbbc53a8d7ecf656.asciidoc 769B
5c6fbeac20dc23b613847f35d431ecab.asciidoc 765B
1e871f060dbe1a5c316ed205278804a8.asciidoc 754B
f29a28fffa7ec604a33a838f48f7ea79.asciidoc 748B
641f75862c70e25e79d249d9e0a79f03.asciidoc 717B
6c3f7c8601e8cc13d36eef98a5e2cb34.asciidoc 702B
5d9d7b84e2fec7ecd832145cbb951cf1.asciidoc 681B
0e44c19403fbd14f97b84c8afffbe4c1.asciidoc 676B
fe3a927d868cbc530e08e05964d5174a.asciidoc 668B
0d664883151008b1051ef2c9ab2d0373.asciidoc 617B
6bbc613bd4f9aec1bbdbabf5db021d28.asciidoc 613B
ea795b6f41dc1ba19121ea4f6d6f663c.asciidoc 603B
1e49eba5b9042c1900a608fe5105ba43.asciidoc 603B
ac875a357f4d4643cdc625b59e927622.asciidoc 597B
0264e994a7e68561e2ca6be0f0d90ee9.asciidoc 583B
de139866a220124360e5e27d1a736ea4.asciidoc 582B
645796e8047967ca4a7635a22a876f4c.asciidoc 578B
fbb38243221c8fb311660616e3add9ce.asciidoc 565B
0f2e5e006b663a88ee99b130ab1b4844.asciidoc 565B
9a4d5e41c52c20635d1fd9c6e13f6c7a.asciidoc 559B
ba0b4081c98f3387f76b77847c52ee9a.asciidoc 559B
666c420fe61fa122386da3c356a64943.asciidoc 558B
1b8655e6ba99fe39933c6eafe78728b7.asciidoc 555B
5e0a6e903dcbef11da316fe89cf05792.asciidoc 555B
37530f35f315b9f35e3e6a13cf2a1ccd.asciidoc 552B
f495f9c99916a05e3b28166d31955fad.asciidoc 547B
4342ccf6cc24fd80bd3cd1f9a4c2ef8e.asciidoc 544B
8eac28d2e9b6482b413d61817456a14f.asciidoc 541B
97babc8d19ef0866774576716eb6d19e.asciidoc 536B
e20fafe937851bf0fbcb6b5188418a53.asciidoc 531B
33f148e3d8676de6cc52f58749898a13.asciidoc 529B
cd3986003382259348b84859f8ac2466.asciidoc 520B
b0eaf67e5cce24ef8889bf20951ccec1.asciidoc 520B
5553cf7a02c22f616cd994747f2dd5a5.asciidoc 518B
d2d30b766e42c74a38f73252a5edc36d.asciidoc 516B
47bb632c6091ad0cd94bc660bdd309a5.asciidoc 515B
ecc57597f6b791d1151ad79d9f4ce67b.asciidoc 511B
929d8c007ba61644537af1441f038ea6.asciidoc 511B
653c0d0ef146c997ef6bc6450d4f5f94.asciidoc 510B
cfbaea6f0df045c5d940bbb6a9c69cd8.asciidoc 509B
52fd112e970882c4d7cc4b0cca8e2c6f.asciidoc 507B
d88e3a006208680c0a1c6974e3acecbe.asciidoc 506B
4c95d54b32df4dc49e9762b6c1ae2c05.asciidoc 504B
3a6238835c7d9f51e6d91f92885fadeb.asciidoc 502B
25429a303a53d06fd0151f3618013ee4.asciidoc 502B
251ea12c1248385ab409906ac64d9ee9.asciidoc 493B
626f8c4b3e2cd3d9beaa63a7f5799d7a.asciidoc 491B
b94cee0f74f57742b3948f9b784dfdd4.asciidoc 489B
e6b972611c0ec8ab4c240f33f323d85b.asciidoc 488B
edae616e1244babf6032aecc6aaaf836.asciidoc 488B
9bfdda207b701028a3439e495e800c02.asciidoc 487B
4832108e01f5efac5e6e40ac405998ca.asciidoc 486B
3e573bfabe00f8bfb8bb69aa5820768e.asciidoc 485B
c4b278ba293abd0d02a0b5ad1a99f84a.asciidoc 485B
316cd43feb3b86396483903af1a048b1.asciidoc 484B
acb10091ad335ddd15d71021aaf23c62.asciidoc 477B
2533e4b36ae837eaecda08407ecb6383.asciidoc 477B
aee4734ee63dbbbd12a21ee886f7a829.asciidoc 474B
e22ef762d6020157e3514d3cda16599c.asciidoc 463B
1da77e114459e0b77d78a3dcc8fae429.asciidoc 458B
7f697eb436dfa3c30dfe610d8c32d132.asciidoc 458B
f9fec6ec9a7da3d35ac674712bd5b813.asciidoc 457B
af44cc7fb0c435d4497c77baf904bf5e.asciidoc 457B
a22f1a6661a074a07041a710e4da6d50.asciidoc 456B
2615aaced23fbe827cbc00c364e4bd4f.asciidoc 456B
6ce0585ab13a480796add86de6b2037c.asciidoc 454B
f5815d573cee0447910c9668003887b8.asciidoc 454B
25576b6773322f0929d4c635a940dba0.asciidoc 453B
095d60b2cfc5004c97efc49f27287262.asciidoc 452B
f43d551aaaad73d979adf1b86533e6a3.asciidoc 451B
5275842787967b6db876025f4a1c6942.asciidoc 450B
36b2778f23d0955255f52c075c4d213d.asciidoc 448B
179f0a3e84ff4bbac18787a018eabf89.asciidoc 447B
9beb260834f8cfb240f6308950dbb9c2.asciidoc 446B
afef5cac988592b97ae289ab39c2f437.asciidoc 446B
7b908b1189f076942de8cd497ff1fa59.asciidoc 445B
ae9ccfaa146731ab9176df90670db1c2.asciidoc 444B
d8c857bbceed72c990038f1ef9d4f9e9.asciidoc 443B
ad0dcbc7fc619e952c8825b8f307b7b2.asciidoc 442B
共 1339 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
清木一阳
- 粉丝: 23
- 资源: 4656
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0