Documentation
¶
Overview ¶
Package elasticsearch implements the Elasticsearch storage.
Index ¶
- Constants
- func Get() storage.IStorage
- func Query(ctx context.Context, s storage.IStorage, index string, query string, v any) error
- func Set(s storage.IStorage)
- func ToElasticSearchString(s customsort.SortSlice) (string, error)
- type Config
- type CountResponse
- type DynamicIndexFunc
- type ElasticSearch
- func (es *ElasticSearch) Count(ctx context.Context, target string, prm *count.Count, ...) (int64, error)
- func (es *ElasticSearch) Create(ctx context.Context, id, target string, v any, prm *create.Create, ...) (string, error)
- func (es *ElasticSearch) CreateIndex(ctx context.Context, name, mapping string) error
- func (es *ElasticSearch) Delete(ctx context.Context, id, target string, prm *delete.Delete, ...) error
- func (es *ElasticSearch) DeleteIndex(ctx context.Context, name string) error
- func (es *ElasticSearch) GetClient() any
- func (es *ElasticSearch) List(ctx context.Context, target string, v any, prm *list.List, ...) error
- func (es *ElasticSearch) Retrieve(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, ...) error
- func (es *ElasticSearch) Update(ctx context.Context, id, target string, v any, prm *update.Update, ...) error
- type ListAny
- type ResponseError
- type ResponseErrorFromES
- type ResponseErrorFromESReason
- type ResponseSourceFromES
Constants ¶
const (
// Name of the storage.
Name = "elasticsearch"
)
Variables ¶
This section is empty.
Functions ¶
func ToElasticSearchString ¶
func ToElasticSearchString(s customsort.SortSlice) (string, error)
ToElasticSearchString converts the `SortSlice` to ElasticSearch sort format.
Types ¶
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,structonly"`
// 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 ¶
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 ¶
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.
type ListAny ¶
type ListAny struct {
// TrackTotalHits indicates whether Elasticsearch should track the true
// total number of hits.
TrackTotalHits bool `default:"false" json:"track_total_hits" query:"track_total_hits"`
}
ListAny is a struct for the `list.List` `Any` field.
type ResponseError ¶
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 {
Type string `json:"type,omitempty"`
Reason string `json:"reason"`
CausedBy *ResponseErrorFromESReason `json:"caused_by,omitempty"`
RootCause []ResponseErrorFromESReason `json:"root_cause,omitempty"`
}
ResponseErrorFromESReason is the reason from the Elasticsearch response.
Elasticsearch's error envelope can carry chained causation via `caused_by` (recursive) and `root_cause` (array of leaf causes). Before v2.1.1 only the top-level `reason` was surfaced, which for `search_phase_execution_exception` is a meaningless "all shards failed" string while the actionable detail lives in `caused_by.reason` (e.g. "Result window is too large..."). Both chains are now captured so callers — directly via the typed fields or indirectly via the enriched `ResponseError.Reason` string — can see the real cause.
type ResponseSourceFromES ¶
type ResponseSourceFromES struct {
Data any `json:"_source"`
}
ResponseSourceFromES is the data from the Elasticsearch response.