es6

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2021 License: MIT, Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

package es6 provides a Go client for Elasticsearch.

Create the client with the NewDefaultClient function:

es6.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 := es6.Config{
  Addresses: []string{
    "http://localhost:9200",
    "http://localhost:9201",
  },
  Username: "foo",
  Password: "bar",
  Transport: &http.Transport{
    MaxIdleConnsPerHost:   10,
    ResponseHeaderTimeout: time.Second,
    DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
    TLSClientConfig: &tls.Config{
      MinVersion:         tls.VersionTLS11,
    },
  },
}

es6.NewClient(cfg)

When using the Elastic Service (https://elastic.co/cloud), you can use CloudID instead of Addresses. When either Addresses or CloudID is set, the ELASTICSEARCH_URL environment variable is ignored.

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 about using the API.

See the github.com/elastic/go-elasticsearch/estransport package for more information about configuring the transport.

Index

Examples

Constants

View Source
const Version = version.Client

Version returns the package version as a string.

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

func NewClient(cfg Config) (*Client, error)

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.

If either cfg.Addresses or cfg.CloudID is set, the ELASTICSEARCH_URL environment variable is ignored.

It's an error to set both cfg.Addresses and cfg.CloudID.

Example
package main

import (
	"crypto/tls"
	"log"
	"net"
	"net/http"
	"time"

	"jinycoo.com/jinygo/database/es6"
	"jinycoo.com/jinygo/database/es6/estransport"
)

func main() {
	cfg := es6.Config{
		Addresses: []string{
			"http://localhost:9200",
		},
		Username: "foo",
		Password: "bar",
		Transport: &http.Transport{
			MaxIdleConnsPerHost:   10,
			ResponseHeaderTimeout: time.Second,
			DialContext:           (&net.Dialer{Timeout: time.Second}).DialContext,
			TLSClientConfig: &tls.Config{
				MinVersion: tls.VersionTLS11,
			},
		},
	}

	es, _ := es6.NewClient(cfg)
	log.Print(es.Transport.(*estransport.Client).URLs())
}
Output:

Example (Logger)
package main

import (
	"os"

	"jinycoo.com/jinygo/database/es6"
	"jinycoo.com/jinygo/database/es6/estransport"
)

func main() {
	// import "jinycoo.com/jinygo/database/es6/estransport"

	// Use one of the bundled loggers:
	//
	// * estransport.TextLogger
	// * estransport.ColorLogger
	// * estransport.CurlLogger
	// * estransport.JSONLogger

	cfg := es6.Config{
		Logger: &estransport.ColorLogger{Output: os.Stdout},
	}

	es6.NewClient(cfg)
}
Output:

func NewDefaultClient

func NewDefaultClient() (*Client, error)

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"

	"jinycoo.com/jinygo/database/es6"
	"jinycoo.com/jinygo/database/es6/estransport"
)

func main() {
	es, err := es6.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:

func (*Client) DiscoverNodes

func (c *Client) DiscoverNodes() error

DiscoverNodes reloads the client connections by fetching information from the cluster.

func (*Client) Metrics

func (c *Client) Metrics() (estransport.Metrics, error)

Metrics returns the client metrics.

func (*Client) Perform

func (c *Client) Perform(req *http.Request) (*http.Response, error)

Perform delegates to Transport to execute a request and return a response.

type Config

type Config struct {
	Addresses []string // A list of Elasticsearch nodes to use.
	Username  string   // Username for HTTP Basic Authentication.
	Password  string   // Password for HTTP Basic Authentication.

	CloudID string // Endpoint for the Elastic Service (https://elastic.co/cloud).
	APIKey  string // Base64-encoded token for authorization; if set, overrides username and password.

	Header http.Header // Global HTTP request header.

	// PEM-encoded certificate authorities.
	// When set, an empty certificate pool will be created, and the certificates will be appended to it.
	// The option is only valid when the transport is not specified, or when it's http.Transport.
	CACert []byte

	RetryOnStatus        []int // List of status codes for retry. Default: 502, 503, 504.
	DisableRetry         bool  // Default: false.
	EnableRetryOnTimeout bool  // Default: false.
	MaxRetries           int   // Default: 3.

	DiscoverNodesOnStart  bool          // Discover nodes when initializing the client. Default: false.
	DiscoverNodesInterval time.Duration // Discover nodes periodically. Default: disabled.

	EnableMetrics     bool // Enable the metrics collection.
	EnableDebugLogger bool // Enable the debug logging.

	RetryBackoff func(attempt int) time.Duration // Optional backoff duration. Default: nil.

	Transport http.RoundTripper    // The HTTP transport object.
	Logger    estransport.Logger   // The logger object.
	Selector  estransport.Selector // The selector object.

	// Optional constructor function for a custom ConnectionPool. Default: nil.
	ConnectionPoolFunc func([]*estransport.Connection, estransport.Selector) estransport.ConnectionPool
}

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.
Package esutil provides helper utilities to the Go client for Elasticsearch.
Package esutil provides helper utilities to the Go client for Elasticsearch.
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL