Documentation
¶
Overview ¶
Package elasticsearch is a thin, option-configured wrapper around the official github.com/elastic/go-elasticsearch/v8 (low-level esapi).
It covers document CRUD + search — Index/Get/Search/Delete — and provides ergonomic construction (functional options + sane defaults), a fail-fast Ping at construction, lightweight metrics + an event hook, and an escape hatch to the underlying *elasticsearch.Client. Bulk/Aggregation/Cat/Indices/Cluster APIs are reached via Client() (not wrapped — keeps the surface thin). There is no Close: elasticsearch.Client is stateless (an HTTP connection pool owned by http.Transport; release a custom Transport set via WithTransport yourself).
Like the minio/etcd/mongo wrappers, New and all ops take a context.Context: it bounds the construction Ping and each HTTP call. When the caller's context carries no deadline, New applies a 10s fallback to the Ping so a context.Background() caller cannot block startup on a half-open endpoint.
go-elasticsearch v8.19 exposes Index/Search/Get/Delete/Ping as FIELDS of named func types (not methods). The wrapper holds these func fields directly (copied from the client; tests assign their own) — so no adapter or interface layer is needed.
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) Client() *elasticsearch.Client
- func (c *Client) Delete(ctx context.Context, index, id string, opts ...func(*esapi.DeleteRequest)) (*esapi.Response, error)
- func (c *Client) Get(ctx context.Context, index, id string, opts ...func(*esapi.GetRequest)) (*esapi.Response, error)
- func (c *Client) Index(ctx context.Context, index string, body io.Reader, ...) (*esapi.Response, error)
- func (c *Client) Metrics() Metrics
- func (c *Client) Options() Options
- func (c *Client) Search(ctx context.Context, opts ...func(*esapi.SearchRequest)) (*esapi.Response, error)
- func (c *Client) SetOnEvent(fn func(Event))
- type Event
- type Metrics
- type Option
- type Options
Examples ¶
Constants ¶
const ( KindIndex = "index" KindSearch = "search" KindGet = "get" KindDelete = "delete" )
Event kinds.
const ( OutcomeSuccess = "success" OutcomeError = "error" )
Event outcomes.
Variables ¶
var ErrNoAddresses = errors.New("elasticsearch: at least one address required (WithAddresses)")
ErrNoAddresses is returned by New when no address was configured.
var ErrPingFailed = errors.New("elasticsearch: ping returned non-success status")
ErrPingFailed is returned by New when the construction Ping does not yield a 2xx status (the cluster is reachable but unhealthy/unauthorized, or returned an unexpected status). Callers may errors.Is against it to distinguish a connectivity/health failure from a config/transport error.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps an elasticsearch client. Safe for concurrent use.
func New ¶
New constructs the client and verifies connectivity with a Ping. Returns an owning Client.
The context bounds the construction-time Ping. If it carries no deadline, a 10s fallback is applied so a caller passing context.Background() against a half-open endpoint cannot block startup indefinitely.
Example ¶
ExampleNew shows connect + Index/Search. It is a compile-checked illustration (an Example without an // Output: comment is compiled but not executed); wire your own address to run it against a live cluster.
v8.19 options are methods on the named func types: esapi.Index(nil).WithXxx(v) builds an option func(*IndexRequest) without invoking the (nil) receiver.
package main
import (
"context"
"log"
"strings"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/v8fg/kit4go/elasticsearch"
)
func main() {
ctx := context.Background()
c, err := elasticsearch.New(ctx, elasticsearch.WithAddresses("http://localhost:9200"))
if err != nil {
log.Fatal(err)
}
if _, err := c.Index(ctx, "creatives", strings.NewReader(`{"name":"banner-1"}`),
esapi.Index(nil).WithDocumentID("1"),
); err != nil {
log.Fatal(err)
}
res, err := c.Search(ctx,
esapi.Search(nil).WithIndex("creatives"),
esapi.Search(nil).WithBody(strings.NewReader(`{"query":{"match_all":{}}}`)),
)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
}
Output:
func Wrap ¶
func Wrap(raw *elasticsearch.Client, opts ...Option) *Client
Wrap adopts an existing *elasticsearch.Client.
func (*Client) Client ¶
func (c *Client) Client() *elasticsearch.Client
Client returns the underlying *elasticsearch.Client for anything the wrapper does not expose (Bulk, Indices, Cat, Cluster, Aggregations). Returns nil when built from a mock.
func (*Client) Delete ¶
func (c *Client) Delete(ctx context.Context, index, id string, opts ...func(*esapi.DeleteRequest)) (*esapi.Response, error)
Delete removes a document by id.
The caller MUST close resp.Body (failure leaks the connection).
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, index, id string, opts ...func(*esapi.GetRequest)) (*esapi.Response, error)
Get fetches a document by id.
The caller MUST close resp.Body (failure leaks the connection).
func (*Client) Index ¶
func (c *Client) Index(ctx context.Context, index string, body io.Reader, opts ...func(*esapi.IndexRequest)) (*esapi.Response, error)
Index creates/replaces a document. body is the JSON document. Forward options (WithDocumentID, WithRefresh, ...) untouched; ctx is applied via WithContext (caller options win — appended last, so an explicit WithContext overrides).
The caller MUST close resp.Body (failure leaks the connection).
func (*Client) Options ¶
Options returns the resolved options the client was built with.
The struct may carry credentials: do not log it verbatim.
func (*Client) Search ¶
func (c *Client) Search(ctx context.Context, opts ...func(*esapi.SearchRequest)) (*esapi.Response, error)
Search runs a query. Pass WithBody(body) + WithIndex("idx") etc. in opts.
The caller MUST close resp.Body (failure leaks the connection).
func (*Client) SetOnEvent ¶
SetOnEvent installs a hook fired after each operation (nil disables it). The hook runs on the calling goroutine; keep it cheap and must not panic (a panic propagates to the caller — the wrapper does not recover). When nil, the cost is a single atomic-pointer load per operation (effectively zero overhead).
type Event ¶
type Event struct {
Kind string // KindIndex, KindSearch, KindGet, KindDelete
Outcome string // OutcomeSuccess or OutcomeError
}
Event is fired after each operation when an OnEvent hook is installed.
type Metrics ¶
type Metrics struct {
Indexes uint64 // Index calls
Searches uint64 // Search calls
Gets uint64 // Get calls
Deletes uint64 // Delete calls
Errors uint64 // any operation returning a transport error (err != nil)
}
Metrics is a point-in-time snapshot of Client counters (all atomic loads).
type Option ¶
type Option func(*Options)
Option configures Options.
func WithAddresses ¶
WithAddresses sets the cluster URLs (e.g. "http://localhost:9200"). Required unless CloudID is set. Copies the slice.
func WithCACert ¶
WithCACert sets a PEM-encoded CA cert for TLS verification.
func WithCloudID ¶
WithCloudID connects to Elastic Cloud (mutually exclusive with Addresses).
func WithCredentials ¶
WithCredentials sets basic-auth username/password.
func WithTransport ¶
func WithTransport(t http.RoundTripper) Option
WithTransport sets a custom http.RoundTripper.
type Options ¶
type Options struct {
Addresses []string
Username string
Password string
CloudID string // Elastic Cloud (mutually exclusive with Addresses)
CACert []byte // PEM-encoded CA cert for TLS verification
Transport http.RoundTripper
}
Options configures the elasticsearch client. Only Addresses (or CloudID) is required.