You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `elasticsearch` package ties together two separate packages for calling the Elasticsearch APIs and transferring data over HTTP: `esapi` and `elastictransport`, respectively.
88
-
89
-
Use the `elasticsearch.NewDefaultClient()` function to create the client with the default settings.
90
-
91
-
```golang
92
-
es, err:= elasticsearch.NewDefaultClient()
93
-
if err != nil {
94
-
log.Fatalf("Error creating the client: %s", err)
95
-
}
96
-
97
-
res, err:= es.Info()
98
-
if err != nil {
99
-
log.Fatalf("Error getting response: %s", err)
100
-
}
101
-
102
-
defer res.Body.Close()
103
-
log.Println(res)
104
-
105
-
// [200 OK] {
106
-
// "name" : "node-1",
107
-
// "cluster_name" : "go-elasticsearch"
108
-
// ...
109
-
```
110
-
111
-
> 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)`.
112
-
113
-
When you export the `ELASTICSEARCH_URL` environment variable,
114
-
it will be used to set the cluster endpoint(s). Separate multiple addresses by a comma.
115
-
116
-
To set the cluster endpoint(s) programmatically, pass a configuration object
117
-
to the `elasticsearch.NewClient()` function.
118
-
119
-
```golang
120
-
cfg:= elasticsearch.Config{
121
-
Addresses: []string{
122
-
"https://localhost:9200",
123
-
"https://localhost:9201",
124
-
},
125
-
// ...
126
-
}
127
-
es, err:= elasticsearch.NewClient(cfg)
128
-
```
129
-
130
-
To set the username and password, include them in the endpoint URL,
131
-
or use the corresponding configuration options.
132
-
133
-
```golang
134
-
cfg:= elasticsearch.Config{
135
-
// ...
136
-
Username: "foo",
137
-
Password: "bar",
138
-
}
139
-
```
140
-
141
-
To set a custom certificate authority used to sign the certificates of cluster nodes,
142
-
use the `CACert` configuration option.
143
-
144
-
```golang
145
-
cert, _:= ioutil.ReadFile(*cacert)
146
-
147
-
cfg:= elasticsearch.Config{
148
-
// ...
149
-
CACert: cert,
150
-
}
151
-
```
152
-
153
-
To set a fingerprint to validate the HTTPS connection use the `CertificateFingerprint` configuration option.
154
-
155
-
```golang
156
-
cfg:= elasticsearch.Config{
157
-
// ...
158
-
CertificateFingerprint: fingerPrint,
159
-
}
160
-
```
161
-
162
-
To configure other HTTP settings, pass an [`http.Transport`](https://golang.org/pkg/net/http/#Transport)
163
-
object in the configuration object.
164
-
165
-
```golang
166
-
cfg:= elasticsearch.Config{
167
-
Transport: &http.Transport{
168
-
MaxIdleConnsPerHost: 10,
169
-
ResponseHeaderTimeout: time.Second,
170
-
TLSClientConfig: &tls.Config{
171
-
MinVersion: tls.VersionTLS12,
172
-
// ...
173
-
},
174
-
// ...
175
-
},
176
-
}
177
-
```
178
-
179
-
See the [`_examples/configuration.go`](_examples/configuration.go) and
180
-
[`_examples/customization.go`](_examples/customization.go) files for
181
-
more examples of configuration and customization of the client.
182
-
See the [`_examples/security`](_examples/security) for an example of a security configuration.
183
-
184
-
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.
185
-
186
-
```golang
187
-
// $ go run _examples/main.go
188
-
189
-
package main
190
-
191
-
import (
192
-
"bytes"
193
-
"context"
194
-
"encoding/json"
195
-
"log"
196
-
"strconv"
197
-
"strings"
198
-
"sync"
199
-
200
-
"github.com/elastic/go-elasticsearch/v8"
201
-
"github.com/elastic/go-elasticsearch/v8/esapi"
202
-
)
203
-
204
-
funcmain() {
205
-
log.SetFlags(0)
206
-
207
-
var (
208
-
r map[string]interface{}
209
-
wg sync.WaitGroup
210
-
)
211
-
212
-
// Initialize a client with the default settings.
213
-
//
214
-
// An `ELASTICSEARCH_URL` environment variable will be used when exported.
As you see in the example above, the `esapi` package allows to call the Elasticsearch APIs in two distinct ways: either by creating a struct, such as `IndexRequest`, and calling its `Do()` method by passing it a context and the client, or by calling the `Search()` function on the client directly, using the option functions such as `WithIndex()`. See more information and examples in the
The `elastictransport` package handles the transfer of data to and from Elasticsearch, including retrying failed requests, keeping a connection pool, discovering cluster nodes and logging.
49
+
## Connecting
366
50
367
-
Read more about the client internals and usage in the following blog posts:
51
+
Refer to the [Connecting section](https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html#_connecting)
0 commit comments