elasticsearch

package
v0.0.0-...-a6a2b61 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, elasticsearchURL string, options ...connectOption) (*elasticsearch.Client, error)

Connect to Elasticsearch.

func ErrorReason

func ErrorReason(err error) string

ErrorReason returns the reason of an error that Elasticsearch reported, if err is of kind Error and has ErrorDetails with a Reason. Any other value of err will return an empty string.

func IsConflict

func IsConflict(err interface{}) bool

IsConflict returns true if the given error indicates that the Elasticsearch operation resulted in a version conflict. This can occur in operations like `update` or `index` with `op_type=create`. The err parameter can be of type *elastic.Error, elastic.Error, *http.Response or int (indicating the HTTP status code).

func IsContextErr

func IsContextErr(err error) bool

IsContextErr returns true if the error is from a context that was canceled or deadline exceeded

func IsForbidden

func IsForbidden(err interface{}) bool

IsForbidden returns true if the given error indicates that Elasticsearch returned HTTP status 403. This happens e.g. due to a missing license. The err parameter can be of type *elastic.Error, elastic.Error, *http.Response or int (indicating the HTTP status code).

func IsNotFound

func IsNotFound(err interface{}) bool

IsNotFound returns true if the given error indicates that Elasticsearch returned HTTP status 404. The err parameter can be of type *elastic.Error, elastic.Error, *http.Response or int (indicating the HTTP status code).

func IsStatusCode

func IsStatusCode(err interface{}, code int) bool

IsStatusCode returns true if the given error indicates that the Elasticsearch operation returned the specified HTTP status code. The err parameter can be of type *http.Response, *Error, Error, or int (indicating the HTTP status code).

func IsTimeout

func IsTimeout(err interface{}) bool

IsTimeout returns true if the given error indicates that Elasticsearch returned HTTP status 408. The err parameter can be of type *elastic.Error, elastic.Error, *http.Response or int (indicating the HTTP status code).

func IsUnauthorized

func IsUnauthorized(err interface{}) bool

IsUnauthorized returns true if the given error indicates that Elasticsearch returned HTTP status 401. This happens e.g. when the cluster is configured to require HTTP Basic Auth. The err parameter can be of type *elastic.Error, elastic.Error, *http.Response or int (indicating the HTTP status code).

func ParseError

func ParseError(res *esapi.Response, err error) error

ParseError will parse the response and return a decoded Error returned by Elasticsearch.

1. If err is not nil, return err. 2. If res is nil, return nil. 3. If res is not an error, return nil. 4. If res cannot be decoded into an ES error, return the decoding error. 4. Return an *Error instance.

func ParseHTTPResponse

func ParseHTTPResponse(res *http.Response, err error) error

ParseHTTPResponse is like ParseError but expects a raw http.Response.

func Ping

func Ping(ctx context.Context, es *elasticsearch.Client) error

Ping the Elasticsearch server.

func WithDebug

func WithDebug(debug bool) connectOption

WithDebug sets the debug mode for the elasticsearch connection.

func WithPassword

func WithPassword(password string) connectOption

WithPassword sets the password for the elasticsearch connection.

func WithPostStart

func WithPostStart(funcs ...postStartFunc) startConfigFunc

WithPostStart adds a post-startup operation to the container. This can be used to install extensions, create tables, seed data etc.

func WithTimeout

func WithTimeout(timeout time.Duration) startConfigFunc

func WithUsername

func WithUsername(username string) connectOption

WithUsername sets the username for the elasticsearch connection.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

func Start

func Start(tb testing.TB, options ...startConfigFunc) *Container

Start an Elasticsearch cluster/node.

func (*Container) Client

func (c *Container) Client() *elasticsearch.Client

func (*Container) Close

func (c *Container) Close() error

type ContainerCache

type ContainerCache struct {
	// contains filtered or unexported fields
}

ContainerCache is a thread-safe cache for elasticsearch containers.

func NewContainerCache

func NewContainerCache() *ContainerCache

NewContainerCache returns a new ContainerCache.

func (*ContainerCache) Close

func (p *ContainerCache) Close() error

Close stops all containers in the cache.

func (*ContainerCache) GetOrCreate

func (p *ContainerCache) GetOrCreate(id string, createFunc func() *Container) *Container

GetOrCreate starts a new container if none is running, otherwise returns the pooled container.

type Error

type Error struct {
	Status  int           `json:"status"`
	Details *ErrorDetails `json:"error,omitempty"`
}

Error encapsulates error details as returned from Elasticsearch.

func (*Error) Error

func (e *Error) Error() string

Error returns a string representation of the error.

type ErrorDetails

type ErrorDetails struct {
	Type         string                   `json:"type"`
	Reason       string                   `json:"reason"`
	ResourceType string                   `json:"resource.type,omitempty"`
	ResourceId   string                   `json:"resource.id,omitempty"`
	Index        string                   `json:"index,omitempty"`
	Phase        string                   `json:"phase,omitempty"`
	Grouped      bool                     `json:"grouped,omitempty"`
	CausedBy     map[string]interface{}   `json:"caused_by,omitempty"`
	RootCause    []*ErrorDetails          `json:"root_cause,omitempty"`
	FailedShards []map[string]interface{} `json:"failed_shards,omitempty"`

	ScriptStack []string             `json:"script_stack,omitempty"` // from ScriptException
	Script      string               `json:"script,omitempty"`       // from ScriptException
	Lang        string               `json:"lang,omitempty"`         // from ScriptException
	Position    *ScriptErrorPosition `json:"position,omitempty"`     // from ScriptException (7.7+)
}

ErrorDetails encapsulate error details from Elasticsearch. It is used in e.g. elastic.Error and elastic.BulkResponseItem.

type ScriptErrorPosition

type ScriptErrorPosition struct {
	Offset int `json:"offset"`
	Start  int `json:"start"`
	End    int `json:"end"`
}

ScriptErrorPosition specifies the position of the error in a script. It is used in ErrorDetails for scripting errors.

Jump to

Keyboard shortcuts

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