Documentation
¶
Index ¶
- Constants
- Variables
- func AsAPIError(err error) *models.APIError
- func Count(ctx context.Context, index string, queries ...query.Option) (int64, error)
- func CreateDoc[T ~string, O any](ctx context.Context, index string, id T, doc O, ...) error
- func CreateIndexIfNotExists(ctx context.Context, prefix string) (bool, error)
- func DecodePagination[T ~string](pagination *T) ([]types.FieldValueVariant, error)
- func DeleteDoc[T ~string](ctx context.Context, index string, id T, options ...Option[*delete.Delete]) error
- func DeleteDocs(ctx context.Context, index string, queries ...query.Option) error
- func EncodePagination[T ~string](sortValues []types.FieldValue) (T, error)
- func ExtractAggregation[T any](aggs map[string]types.Aggregate, name string) (T, bool, error)
- func ExtractBuckets[T BucketTypes](container any) ([]T, error)
- func ExtractBucketsAsMap[T BucketTypes](container any) (map[string]T, error)
- func GenerateIndexName(prefix string) string
- func GetDoc[T ~string, O any](ctx context.Context, index string, id T) (O, error)
- func GetDocs[T ~string, O any](ctx context.Context, index string, ids ...T) ([]O, error)
- func NewCountRequest(ctx context.Context, api *elasticsearch.TypedClient, ...) *count.Count
- func NewDeleteByQueryRequest(ctx context.Context, api *elasticsearch.TypedClient, index string, ...) *deletebyquery.DeleteByQuery
- func NewGetRequest(ctx context.Context, api *elasticsearch.TypedClient, index string, id string, ...) *get.Get
- func NewMGetRequest(ctx context.Context, api *elasticsearch.TypedClient, ...) *mget.Mget
- func NewSearchRequest(ctx context.Context, options ...Option[*search.Search]) *search.Search
- func NewUpdateDocRequest(ctx context.Context, api *elasticsearch.TypedClient, index, id string, doc any, ...) *update.Update
- func SearchAll[O any](ctx context.Context, index string, query query.Option, paginationSize int, ...) ([]O, error)
- func Shutdown(ctx context.Context) error
- func UpdateDoc[T ~string](ctx context.Context, index string, id T, updates any, ...) error
- func UpdateIndexAlias(ctx context.Context, alias string, index string) error
- type API
- type Aggs
- type BucketTypes
- type Logger
- type Option
- func WithAggregation[T supportsAggregations[T]](key string, value types.AggregationsVariant) Option[T]
- func WithAggregations[T supportsAggregations[T]](aggregations map[string]types.Aggregations) Option[T]
- func WithCollapse[T supportsCollapse[T]](field string) Option[T]
- func WithDocAsUpsert[T supportsDocAsUpsert[T]](value bool) Option[T]
- func WithDocIDs[T supportsIDs[T]](ids ...string) Option[T]
- func WithDocSorting[T supportsSort[T]]() Option[T]
- func WithFrom[T supportsFrom[T]](from int) Option[T]
- func WithHeader[T supportsHeader[T]](key, value string) Option[T]
- func WithIndex[T supportsIndex[T]](index string) Option[T]
- func WithPrimaryTerm[T supportsPrimaryTerm[T]](term int64) Option[T]
- func WithQuery[T supportsQuery[T]](options ...query.Option) Option[T]
- func WithRefresh[T supportsRefresh[T]](value refresh.Refresh) Option[T]
- func WithRetriever[T supportsRetriever[T]](options ...retriever.Option) Option[T]
- func WithRetryOnConflict[T supportsRetryOnConflict[T]](retries int) Option[T]
- func WithSearchAfter[T supportsSearchAfter[T]](sortresults ...types.FieldValueVariant) Option[T]
- func WithSeqNo[T supportsSeqNo[T]](seqno int64) Option[T]
- func WithSeqNoPrimaryTerm[T supportsSeqNoPrimaryTerm[T]](value bool) Option[T]
- func WithSize[T supportsSize[T]](size int) Option[T]
- func WithSort[T supportsSort[T]](sorts ...types.SortCombinationsVariant) Option[T]
- func WithTrackHits[T supportsTrackHits[T]](value TrackHits) Option[T]
- type SearchResponse
- type TrackHits
Constants ¶
const ( // ReqIDHeader is the value that will be used to assign a unique ID to an Elasticsearch API request (that can be // used to associate the API request with a web server request). ReqIDHeader = "X-Opaque-Id" )
Variables ¶
var ( ErrInvalidAggType = errors.New("invalid aggregation type") ErrAggNotFound = errors.New("aggregation not found") )
var Connect = sync.OnceValue(func() error { if err := loadConfigOnce(); err != nil { return fmt.Errorf("load config environment: %w", err) } var options []elasticsearch.Option options = append(options, elasticsearch.WithLogger(&Logger{EnableResponseBody: false, EnableRequestBody: false}), elasticsearch.WithRetry(5, 429, 502, 503, 504), elasticsearch.WithTransportOptions( elastictransport.WithRetryBackoff(func(attempt int) time.Duration { return time.Duration(1<<uint(attempt)) * 100 * time.Millisecond }), ), ) switch { case cfg.CloudID != "": options = append(options, elasticsearch.WithCloudID(cfg.CloudID), elasticsearch.WithAPIKey(cfg.APIKey)) case len(cfg.URLs) > 0: options = append(options, elasticsearch.WithAddresses(cfg.URLs...)) if cfg.CAFile != "" { cert, err := os.ReadFile(cfg.CAFile) if err != nil { return fmt.Errorf("read CA cert: %w", err) } options = append(options, elasticsearch.WithCACert(cert)) switch { case cfg.APIKey != "": options = append(options, elasticsearch.WithAPIKey(cfg.APIKey)) case cfg.Username != "" && cfg.Password != "": options = append(options, elasticsearch.WithBasicAuth(cfg.Username, cfg.Password), ) } } default: return errors.New("invalid config") } esclient, err := elasticsearch.NewTyped(options...) if err != nil { return fmt.Errorf("create typed client: %w", err) } api = API{TypedClient: esclient} initialized.Store(true) slog.Debug("Elasticsearch connection created.") return nil })
Connect sets up a connection to Elasticsearch.
var ErrNotFound = errors.New("not found")
Functions ¶
func AsAPIError ¶ added in v0.237.0
func CreateDoc ¶
func CreateDoc[T ~string, O any]( ctx context.Context, index string, id T, doc O, options ...Option[*create.Create], ) error
CreateDoc will create the given document, with given id, in the given index.
func CreateIndexIfNotExists ¶ added in v0.157.0
func DecodePagination ¶
func DecodePagination[T ~string](pagination *T) ([]types.FieldValueVariant, error)
DecodePagination will take a Pagination object, HTML-unescape the string then unmarshal it back into sort values.
func DeleteDoc ¶
func DeleteDoc[T ~string](ctx context.Context, index string, id T, options ...Option[*delete.Delete]) error
DeleteDoc deletes the document with the given id from the given index.
func DeleteDocs ¶
DeleteDocs performs a delete by query request on the given index to delete documents matching the given queries.
func EncodePagination ¶
func EncodePagination[T ~string](sortValues []types.FieldValue) (T, error)
EncodePagination will take sort values returned from a query, marshal them to JSON, then HTML-escape the string into a Pagination object, which is safe for use in API query parameters.
func ExtractAggregation ¶ added in v0.121.0
ExtractAggregation recursively searches for an aggregation by name. It searches top-level Aggregations and then digs into bucket sub-aggregations.
func ExtractBuckets ¶ added in v0.121.0
func ExtractBuckets[T BucketTypes](container any) ([]T, error)
ExtractBuckets extracts the bucket object from the aggregation as a slice of the given type. If the aggregation cannot be extracted to the type, a non-nil error is returned that will contain details.
func ExtractBucketsAsMap ¶ added in v0.207.0
func ExtractBucketsAsMap[T BucketTypes](container any) (map[string]T, error)
ExtractBucketsAsMap extracts the bucket object from the aggregation as a map[string]T of given type. If the aggregation cannot be extracted to the type, a non-nil error is returned that will contain details.
func GenerateIndexName ¶ added in v0.157.0
func GetDoc ¶
GetDoc retrieves the doc with the given id from the given index. A non-nil error is returned on a failure.
func GetDocs ¶
GetDocs performs an `_mget` request to fetch the documents from the given index with the given ids. A non-nil error is returned on a failure.
func NewCountRequest ¶
func NewCountRequest( ctx context.Context, api *elasticsearch.TypedClient, options ...Option[*count.Count], ) *count.Count
NewCountRequest creates a new count request with the given options.
func NewDeleteByQueryRequest ¶
func NewDeleteByQueryRequest( ctx context.Context, api *elasticsearch.TypedClient, index string, options ...Option[*deletebyquery.DeleteByQuery], ) *deletebyquery.DeleteByQuery
NewDeleteByQueryRequest creates a new delete by query request that will operate on the given index with the given options.
func NewGetRequest ¶
func NewGetRequest( ctx context.Context, api *elasticsearch.TypedClient, index string, id string, options ...Option[*get.Get], ) *get.Get
NewGetRequest creates a new get object with the given options.
func NewMGetRequest ¶
func NewMGetRequest(ctx context.Context, api *elasticsearch.TypedClient, options ...Option[*mget.Mget]) *mget.Mget
NewMGetRequest creates a new mget object with the given options.
func NewSearchRequest ¶
NewSearchRequest creates a new search request with the given options.
func NewUpdateDocRequest ¶
func NewUpdateDocRequest( ctx context.Context, api *elasticsearch.TypedClient, index, id string, doc any, options ...Option[*update.Update], ) *update.Update
NewUpdateDocRequest creates a new doc update request with the given options.
func SearchAll ¶
func SearchAll[O any]( ctx context.Context, index string, query query.Option, paginationSize int, options ...Option[*search.Search], ) ([]O, error)
SearchAll performs a paginated search request to retrieve *all* documents matching the given query. Unlike Search, it does not stop when the request hits count is reached.
func Shutdown ¶ added in v0.190.3
Shutdown gracefully closes the Elasticsearch connection (if it has been initialized/created).
func UpdateDoc ¶
func UpdateDoc[T ~string]( ctx context.Context, index string, id T, updates any, options ...Option[*update.Update], ) error
UpdateDoc performs a partial doc update on the document with the given id in the given index. A non-nil error is returned on a failure.
func UpdateIndexAlias ¶ added in v0.157.0
UpdateIndexAlias performs a swap of an alias to the given index. It adds the given index to the alias, sets it as the write destination, then removes any existing aliased indicies so the index remains as the only aliased one.
Types ¶
type API ¶
type API struct {
*elasticsearch.TypedClient
}
API is an object that provides access to the Elasticsearch API.
type Aggs ¶ added in v0.121.0
type Aggs map[string]types.Aggregations
type BucketTypes ¶ added in v0.121.0
type BucketTypes interface {
types.StringTermsBucket | types.StringRareTermsBucket | types.FiltersBucket
}
BucketTypes is a type constraint for all aggregation bucket types.
type Logger ¶
Logger is a custom elastictransport.Logger.
func (*Logger) LogRoundTrip ¶
func (l *Logger) LogRoundTrip( req *http.Request, res *http.Response, err error, _ time.Time, dur time.Duration, ) error
LogRoundTrip should not modify the request or response, except for consuming and closing the body. Implementations have to check for nil values in request and response.
func (*Logger) RequestBodyEnabled ¶
RequestBodyEnabled makes the client pass a copy of request body to the logger.
func (*Logger) ResponseBodyEnabled ¶
ResponseBodyEnabled makes the client pass a copy of response body to the logger.
type Option ¶
type Option[T any] func(T) T
Option is a functional option for a request of type T.
func WithAggregation ¶ added in v0.237.0
func WithAggregation[T supportsAggregations[T]](key string, value types.AggregationsVariant) Option[T]
WithAggregation option adds an aggregation to perform with the request.
func WithAggregations ¶
func WithAggregations[T supportsAggregations[T]](aggregations map[string]types.Aggregations) Option[T]
WithAggregations option sets the aggregations to perform with the request.
func WithCollapse ¶ added in v0.237.0
WithCollapse option sets that duplicate results for the given field should be collapsed down to the first result.
func WithDocAsUpsert ¶ added in v0.120.0
WithDocAsUpsert option specifies that the doc should be added if it does not already exist. Normally, the request would not add a document if none already exist.
func WithDocIDs ¶ added in v0.237.0
WithDocIDs option sets the doc IDs to retrieve.
func WithDocSorting ¶ added in v0.120.0
func WithDocSorting[T supportsSort[T]]() Option[T]
WithDocSorting option is a convenience option to sort the results by doc id. This option is useful when fetching all results or deep pagination where sort order is irrelevant.
func WithHeader ¶ added in v0.120.0
WithHeader option sets a HTTP header on the request.
func WithPrimaryTerm ¶ added in v0.237.0
WithPrimaryTerm option to specifies the primary term of the doc on which to operate.
func WithRefresh ¶
WithRefresh option sets the refresh value on the request.
func WithRetriever ¶ added in v0.177.0
WithRetriever option sets a retriever as part of the request.
func WithRetryOnConflict ¶
WithRetryOnConflict option sets the number of times the request will be retried if there is a conflict.
func WithSearchAfter ¶
func WithSearchAfter[T supportsSearchAfter[T]](sortresults ...types.FieldValueVariant) Option[T]
WithSearchAfter option sets the point after which results should be fetched.
func WithSeqNo ¶ added in v0.237.0
WithSeqNo option specifies the sequence no of the doc on which to operate.
func WithSeqNoPrimaryTerm ¶ added in v0.231.0
WithSeqNoPrimaryTerm option specifies that the results should return the sequence no and primary term of each hit.
func WithSort ¶ added in v0.120.0
func WithSort[T supportsSort[T]](sorts ...types.SortCombinationsVariant) Option[T]
WithSort option sets how the results will be sorted.
func WithTrackHits ¶ added in v0.237.0
WithTrackHits option sets whether total hits should be tracked.
type SearchResponse ¶ added in v0.121.0
type SearchResponse[O any] struct { *search.Response Results []O Pagination []types.FieldValue }
SearchResponse represents the results of a search request. In addition to exposing the raw API response, the object contains marshaled results and pagination values.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ilm contains wrappers that help with creating ILM policies.
|
Package ilm contains wrappers that help with creating ILM policies. |
|
Package query contains methods for building Elasticsearch queries.
|
Package query contains methods for building Elasticsearch queries. |
|
Package templates contains wrappers for helping with creating index and component templates.
|
Package templates contains wrappers for helping with creating index and component templates. |