Documentation
¶
Overview ¶
Package elasticsearch provides a Go client for Elasticsearch.
Create the client with the NewDefaultClient function:
elasticsearch.NewDefaultClient()
The ELASTICSEARCH_URL environment variable is used instead of the default URL, when set. Use a comma to separate multiple URLs.
To configure the client, pass a Config object to the NewClient function:
cfg := elasticsearch.Config{
Addresses: []string{
"http://localhost:9200",
"http://localhost:9201",
},
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second,
DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
TLSClientConfig: &tls.Config{
MaxVersion: tls.VersionTLS11,
InsecureSkipVerify: true,
},
},
}
elasticsearch.NewClient(cfg)
See the elasticsearch_integration_test.go file and the _examples folder for more information.
Call the Elasticsearch APIs by invoking the corresponding methods on the client:
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
log.Println(res)
See the github.com/elastic/go-elasticsearch/esapi package for more information and examples.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
*esapi.API // Embeds the API methods
Transport estransport.Interface
}
Client represents the Elasticsearch client.
func NewClient ¶
NewClient creates a new client with configuration from cfg.
It will use http://localhost:9200 as the default address.
It will use the ELASTICSEARCH_URL environment variable, if set, to configure the addresses; use a comma to separate multiple URLs.
It's an error to set both cfg.Addresses and the ELASTICSEARCH_URL environment variable.
Example ¶
package main
import (
"crypto/tls"
"log"
"net"
"net/http"
"time"
"github.com/elastic/go-elasticsearch"
"github.com/elastic/go-elasticsearch/estransport"
)
func main() {
cfg := elasticsearch.Config{
Addresses: []string{
"http://localhost:9200",
},
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second,
DialContext: (&net.Dialer{Timeout: time.Second}).DialContext,
TLSClientConfig: &tls.Config{
MaxVersion: tls.VersionTLS11,
InsecureSkipVerify: true,
},
},
}
es, _ := elasticsearch.NewClient(cfg)
log.Print(es.Transport.(*estransport.Client).URLs())
}
Output:
func NewDefaultClient ¶
NewDefaultClient creates a new client with default options.
It will use http://localhost:9200 as the default address.
It will use the ELASTICSEARCH_URL environment variable, if set, to configure the addresses; use a comma to separate multiple URLs.
Example ¶
package main
import (
"log"
"github.com/elastic/go-elasticsearch"
"github.com/elastic/go-elasticsearch/estransport"
)
func main() {
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s\n", err)
}
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting the response: %s\n", err)
}
defer res.Body.Close()
log.Print(es.Transport.(*estransport.Client).URLs())
}
Output:
type Config ¶
type Config struct {
Addresses []string // A list of Elasticsearch nodes to use.
Transport http.RoundTripper // The HTTP transport object.
}
Config represents the client configuration.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package esapi provides the Go API for Elasticsearch.
|
Package esapi provides the Go API for Elasticsearch. |
|
Package estransport provides the transport layer for the Elasticsearch client.
|
Package estransport provides the transport layer for the Elasticsearch client. |