elasticsearch

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package elasticsearch implements the Elasticsearch storage.

Index

Constants

View Source
const Name = "elasticsearch"

Name of the storage.

Variables

This section is empty.

Functions

func Get

func Get() storage.IStorage

Get returns a setup MongoDB, or set it up.

func Set

func Set(s storage.IStorage)

Set sets the storage, primarily used for testing.

func ToElasticSearchString

func ToElasticSearchString(s customsort.Sort) (string, error)

ToElasticSearchString converts the `Sort` to ElasticSearch sort format.

Types

type Config

type Config = elasticsearch.Config

Config is the ElasticSearch configuration.

type CountResponse

type CountResponse struct {
	Took     int  `json:"took"`
	TimedOut bool `json:"timed_out"`
	Shards   struct {
		Total      int `json:"total"`
		Successful int `json:"successful"`
		Skipped    int `json:"skipped"`
		Failed     int `json:"failed"`
	} `json:"_shards"`
	Hits struct {
		Total struct {
			Value    int    `json:"value"`
			Relation string `json:"relation"`
		} `json:"total"`
		MaxScore any   `json:"max_score"`
		Hits     []any `json:"hits"`
	} `json:"hits"`
}

CountResponse is the response from the Count operation.

type DynamicIndexFunc

type DynamicIndexFunc func() string

DynamicIndexFunc is a function which defines the name of the index, and evaluated at the index time.

type ElasticSearch

type ElasticSearch struct {
	*storage.Storage

	// Client is the ElasticSearch client.
	Client *elasticsearch.Client `json:"-" validate:"required"`

	// Config is the ElasticSearch configuration.
	Config Config `json:"-"`

	// Target allows to set a static target. If it is empty, the target will be
	// dynamic - the one set at the operation (count, create, delete, etc) time.
	// Depending on the storage, target is a collection, a table, a bucket, etc.
	// For ElasticSearch, for example it doesn't have a concept of a database -
	// the target then is the index. Due to different cases of ElasticSearch
	// usage, the target can be static or dynamic - defined at the index time,
	// for example: log-{YYYY}-{MM}. For Redis, it isn't used at all.
	Target DynamicIndexFunc `json:"-"`
}

ElasticSearch storage definition.

func New

func New(ctx context.Context, cfg Config) (*ElasticSearch, error)

New returns a new `ElasticSearch` storage.

func NewWithDynamicIndex

func NewWithDynamicIndex(
	ctx context.Context,
	dynamicIndexFunc DynamicIndexFunc,
	cfg Config,
) (*ElasticSearch, error)

NewWithDynamicIndex returns a new `ElasticSearch` storage. It allows to define a function which defines the name of the index, and evaluated at the index time.

func NewWithIndex

func NewWithIndex(
	ctx context.Context,
	indexName string,
	cfg Config,
) (*ElasticSearch, error)

NewWithIndex returns a new `ElasticSearch` storage specifying the index name.

func (*ElasticSearch) Count

func (es *ElasticSearch) Count(ctx context.Context, target string, prm *count.Count, options ...storage.Func[*count.Count]) (int64, error)

Count returns the number of items in the storage.

func (*ElasticSearch) Create added in v0.0.15

func (es *ElasticSearch) Create(ctx context.Context, id, target string, v any, prm *create.Create, options ...storage.Func[*create.Create]) (string, error)

Create data.

NOTE: Not all storages returns the ID, neither all storages requires `id` to be set. You are better off setting the ID yourself.

func (*ElasticSearch) CreateIndex

func (es *ElasticSearch) CreateIndex(ctx context.Context, name, mapping string) error

CreateIndex creates a new index in Elasticsearch.

func (*ElasticSearch) Delete

func (es *ElasticSearch) Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...storage.Func[*delete.Delete]) error

Delete removes data.

func (*ElasticSearch) DeleteIndex

func (es *ElasticSearch) DeleteIndex(ctx context.Context, name string) error

DeleteIndex deletes a new index in Elasticsearch.

func (*ElasticSearch) GetClient

func (es *ElasticSearch) GetClient() any

GetClient returns the client.

func (*ElasticSearch) List

func (es *ElasticSearch) List(ctx context.Context, target string, v any, prm *list.List, options ...storage.Func[*list.List]) error

List data.

NOTE: It uses param.List.Search to query the data.

func (*ElasticSearch) Retrieve added in v0.0.15

func (es *ElasticSearch) Retrieve(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, options ...storage.Func[*retrieve.Retrieve]) error

Retrieve data.

func (*ElasticSearch) Update

func (es *ElasticSearch) Update(ctx context.Context, id, target string, v any, prm *update.Update, options ...storage.Func[*update.Update]) error

Update data.

type ResponseError

type ResponseError struct {
	Reason string `json:"reason"`
	Status int    `json:"status"`
}

ResponseError is the error from the Elasticsearch response.

type ResponseErrorFromES

type ResponseErrorFromES struct {
	Error  ResponseErrorFromESReason `json:"error"`
	Status int                       `json:"status"`
}

ResponseErrorFromES is the error from the Elasticsearch response.

type ResponseErrorFromESReason

type ResponseErrorFromESReason struct {
	Reason string `json:"reason"`
}

ResponseErrorFromESReason is the reason from the Elasticsearch response.

type ResponseSourceFromES

type ResponseSourceFromES struct {
	Data interface{} `json:"_source"`
}

ResponseSourceFromES is the data from the Elasticsearch response.

Jump to

Keyboard shortcuts

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