esdriver

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package esdriver implements an Elasticsearch driver for the Grove ORM.

Unlike the SQL-based drivers (pgdriver, mysqldriver), this driver uses Elasticsearch-native JSON operations (Search, Index, Update, Delete, Bulk, Aggregate) instead of SQL query builders. It implements grove.GroveDriver and the adapter interface (queryBuilder) so that it integrates with the top-level grove.DB handle.

Usage:

esdb := esdriver.New()
err := esdb.Open(ctx, "http://localhost:9200")
db, err := grove.Open(esdb)

// Typed access via Unwrap:
es := esdriver.Unwrap(db)
es.NewSearch(&users).Match("name", "alice").Scan(ctx)

Index

Constants

This section is empty.

Variables

View Source
var ErrLastInsertIDNotSupported = errors.New("esdriver: LastInsertId is not supported; use DocumentID() instead")

ErrLastInsertIDNotSupported is returned by EsResult.LastInsertId because Elasticsearch does not use auto-incrementing integer IDs. Use the DocumentID method to get the generated document ID instead.

View Source
var ErrNotSupported = errors.New("esdriver: operation not supported")

ErrNotSupported is returned for operations that are not applicable to Elasticsearch.

Functions

This section is empty.

Types

type AggregateQuery

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

AggregateQuery builds and executes Elasticsearch aggregation queries. Use ElasticDB.NewAggregate() to create one.

func (*AggregateQuery) Avg

func (q *AggregateQuery) Avg(name, field string) *AggregateQuery

Avg adds an avg metric aggregation.

func (*AggregateQuery) BuildBody

func (q *AggregateQuery) BuildBody() M

BuildBody returns the full request body as a map for testing/inspection.

func (*AggregateQuery) Cardinality

func (q *AggregateQuery) Cardinality(name, field string) *AggregateQuery

Cardinality adds a cardinality (approximate distinct count) aggregation.

func (*AggregateQuery) DateHistogram

func (q *AggregateQuery) DateHistogram(name, field, interval string) *AggregateQuery

DateHistogram adds a date_histogram aggregation.

func (*AggregateQuery) GetAggs

func (q *AggregateQuery) GetAggs() M

GetAggs returns the current aggregation definitions. Useful for testing.

func (*AggregateQuery) GetIndex

func (q *AggregateQuery) GetIndex() string

GetIndex returns the index name. Useful for testing.

func (*AggregateQuery) GetQuery

func (q *AggregateQuery) GetQuery() M

GetQuery returns the current query filter. Useful for testing.

func (*AggregateQuery) Max

func (q *AggregateQuery) Max(name, field string) *AggregateQuery

Max adds a max metric aggregation.

func (*AggregateQuery) Min

func (q *AggregateQuery) Min(name, field string) *AggregateQuery

Min adds a min metric aggregation.

func (*AggregateQuery) Query

func (q *AggregateQuery) Query(query M) *AggregateQuery

Query sets a filter query to restrict the documents being aggregated.

func (*AggregateQuery) RawAggs

func (q *AggregateQuery) RawAggs(aggs M) *AggregateQuery

RawAggs sets the entire aggregations body from a raw map.

func (*AggregateQuery) Scan

func (q *AggregateQuery) Scan(ctx context.Context, dest any) error

Scan executes the aggregation and decodes results into dest. dest should be a pointer to the structure receiving aggregation results.

func (*AggregateQuery) ScanRaw

func (q *AggregateQuery) ScanRaw(ctx context.Context) (*SearchResult, error)

ScanRaw executes the aggregation and returns the raw SearchResult.

func (*AggregateQuery) Size

func (q *AggregateQuery) Size(n int) *AggregateQuery

Size sets the number of hits to return alongside aggregations. Default is 0 (aggregations only).

func (*AggregateQuery) SubAgg

func (q *AggregateQuery) SubAgg(parent string, child M) *AggregateQuery

SubAgg adds a sub-aggregation to an existing parent aggregation.

func (*AggregateQuery) Sum

func (q *AggregateQuery) Sum(name, field string) *AggregateQuery

Sum adds a sum metric aggregation.

func (*AggregateQuery) Terms

func (q *AggregateQuery) Terms(name, field string) *AggregateQuery

Terms adds a terms aggregation.

type AggregateResult

type AggregateResult struct {
	Raw json.RawMessage `json:"-"`
}

AggregateResult holds parsed aggregation results.

type BoolQuery

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

BoolQuery builds an Elasticsearch bool query with must, should, must_not, and filter clauses.

func (*BoolQuery) Build

func (b *BoolQuery) Build() M

Build returns the bool query as a map.

func (*BoolQuery) Filter

func (b *BoolQuery) Filter(clause M) *BoolQuery

Filter adds a clause to the filter array (AND semantics, no scoring).

func (*BoolQuery) MinimumShouldMatch

func (b *BoolQuery) MinimumShouldMatch(n int) *BoolQuery

MinimumShouldMatch sets the minimum number of should clauses that must match.

func (*BoolQuery) Must

func (b *BoolQuery) Must(clause M) *BoolQuery

Must adds a clause to the must array (AND semantics, contributes to score).

func (*BoolQuery) MustNot

func (b *BoolQuery) MustNot(clause M) *BoolQuery

MustNot adds a clause to the must_not array (NOT semantics).

func (*BoolQuery) Should

func (b *BoolQuery) Should(clause M) *BoolQuery

Should adds a clause to the should array (OR semantics, boosts score).

type BulkAction

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

type BulkItem

type BulkItem struct {
	Index  *BulkItemResult `json:"index,omitempty"`
	Create *BulkItemResult `json:"create,omitempty"`
	Update *BulkItemResult `json:"update,omitempty"`
	Delete *BulkItemResult `json:"delete,omitempty"`
}

BulkItem holds the result for a single bulk action.

type BulkItemResult

type BulkItemResult struct {
	Index   string `json:"_index"`
	ID      string `json:"_id"`
	Version int64  `json:"_version"`
	Status  int    `json:"status"`
	Error   *M     `json:"error,omitempty"`
}

BulkItemResult holds details of a single bulk action outcome.

type BulkQuery

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

BulkQuery builds and executes mixed Elasticsearch bulk operations (index, create, update, delete in a single request). Use ElasticDB.NewBulk() to create one.

func (*BulkQuery) Create

func (q *BulkQuery) Create(index, docID string, doc any) *BulkQuery

Create adds a create action (insert only, fail if exists).

func (*BulkQuery) Delete

func (q *BulkQuery) Delete(index, docID string) *BulkQuery

Delete adds a delete action.

func (*BulkQuery) Exec

func (q *BulkQuery) Exec(ctx context.Context) (*BulkResult, error)

Exec executes the bulk operation.

func (*BulkQuery) GetActions

func (q *BulkQuery) GetActions() []BulkAction

GetActions returns the current actions. Useful for testing.

func (*BulkQuery) Index

func (q *BulkQuery) Index(index, docID string, doc any) *BulkQuery

Index adds an index action (insert or overwrite).

func (*BulkQuery) Refresh

func (q *BulkQuery) Refresh(r string) *BulkQuery

Refresh overrides the default refresh policy for this bulk operation.

func (*BulkQuery) Update

func (q *BulkQuery) Update(index, docID string, doc M) *BulkQuery

Update adds an update action (partial document update).

type BulkResult

type BulkResult struct {
	Took   int64      `json:"took"`
	Errors bool       `json:"errors"`
	Items  []BulkItem `json:"items"`
}

BulkResult holds the response from the ES _bulk API.

type DeleteQuery

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

DeleteQuery builds and executes Elasticsearch delete operations. Use ElasticDB.NewDelete() to create one.

func (*DeleteQuery) DocumentID

func (q *DeleteQuery) DocumentID(id string) *DeleteQuery

DocumentID sets the document _id for a single-document delete.

func (*DeleteQuery) Exec

func (q *DeleteQuery) Exec(ctx context.Context) (*EsResult, error)

Exec executes the delete operation.

func (*DeleteQuery) Filter

func (q *DeleteQuery) Filter(f M) *DeleteQuery

Filter sets the query filter for delete-by-query operations.

func (*DeleteQuery) GetFilter

func (q *DeleteQuery) GetFilter() M

GetFilter returns the current filter. Useful for testing.

func (*DeleteQuery) GetIndex

func (q *DeleteQuery) GetIndex() string

GetIndex returns the index name. Useful for testing.

func (*DeleteQuery) Index

func (q *DeleteQuery) Index(name string) *DeleteQuery

Index overrides the index name derived from the model.

func (*DeleteQuery) IsMany

func (q *DeleteQuery) IsMany() bool

IsMany returns whether the query targets multiple documents. Useful for testing.

func (*DeleteQuery) Many

func (q *DeleteQuery) Many() *DeleteQuery

Many configures the query to delete all matching documents (delete-by-query).

func (*DeleteQuery) Refresh

func (q *DeleteQuery) Refresh(r string) *DeleteQuery

Refresh overrides the default refresh policy for this operation.

type ElasticDB

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

ElasticDB implements grove.GroveDriver for Elasticsearch using the official Go client github.com/elastic/go-elasticsearch/v8. It also implements the grove adapter interface (queryBuilder) for integration with grove.DB.

func New

func New() *ElasticDB

New creates a new unconnected Elasticsearch driver. Call Open to establish a connection to the Elasticsearch cluster.

func Unwrap

func Unwrap(db *grove.DB) *ElasticDB

Unwrap extracts the underlying *ElasticDB from a *grove.DB handle. This allows access to Elasticsearch-specific query builders and features.

esdb := esdriver.Unwrap(db) // returns *esdriver.ElasticDB
esdb.NewSearch(&users).Match("name", "alice").Scan(ctx)

Panics if the driver is not an *ElasticDB.

func (*ElasticDB) Client

func (db *ElasticDB) Client() *elasticsearch.Client

Client returns the underlying elasticsearch.Client.

func (*ElasticDB) Close

func (db *ElasticDB) Close() error

Close is a no-op for Elasticsearch since the HTTP client does not require explicit cleanup.

func (*ElasticDB) CreateIndex

func (db *ElasticDB) CreateIndex(ctx context.Context, name string, mapping M) error

CreateIndex creates an Elasticsearch index with the given mapping.

func (*ElasticDB) DefaultIndex

func (db *ElasticDB) DefaultIndex() string

DefaultIndex returns the default index name, if set.

func (*ElasticDB) DeleteIndex

func (db *ElasticDB) DeleteIndex(ctx context.Context, name string) error

DeleteIndex deletes an Elasticsearch index.

func (*ElasticDB) GroveDelete

func (db *ElasticDB) GroveDelete(model any) any

GroveDelete is the adapter method for grove.DB.NewDelete().

func (*ElasticDB) GroveInsert

func (db *ElasticDB) GroveInsert(model any) any

GroveInsert is the adapter method for grove.DB.NewInsert().

func (*ElasticDB) GroveSelect

func (db *ElasticDB) GroveSelect(model ...any) any

GroveSelect is the adapter method for grove.DB.NewSelect().

func (*ElasticDB) GroveUpdate

func (db *ElasticDB) GroveUpdate(model any) any

GroveUpdate is the adapter method for grove.DB.NewUpdate().

func (*ElasticDB) IndexExists

func (db *ElasticDB) IndexExists(ctx context.Context, name string) (bool, error)

IndexExists checks whether an Elasticsearch index exists.

func (*ElasticDB) Name

func (db *ElasticDB) Name() string

Name returns the driver identifier.

func (*ElasticDB) NewAggregate

func (db *ElasticDB) NewAggregate(index string) *AggregateQuery

NewAggregate creates a new aggregation query for the given index.

func (*ElasticDB) NewBulk

func (db *ElasticDB) NewBulk() *BulkQuery

NewBulk creates a new BulkQuery.

func (*ElasticDB) NewDelete

func (db *ElasticDB) NewDelete(model any) *DeleteQuery

NewDelete creates a new DeleteQuery.

func (*ElasticDB) NewInsert

func (db *ElasticDB) NewInsert(model any) *InsertQuery

NewInsert creates a new InsertQuery. model can be a struct pointer or a pointer to a slice (for bulk insert).

func (*ElasticDB) NewSearch

func (db *ElasticDB) NewSearch(model ...any) *SearchQuery

NewSearch creates a new SearchQuery. model can be:

  • *[]User (slice pointer for multi-row)
  • *User (struct pointer for single row)
  • (*User)(nil) (nil pointer for index reference without binding)

func (*ElasticDB) NewUpdate

func (db *ElasticDB) NewUpdate(model any) *UpdateQuery

NewUpdate creates a new UpdateQuery.

func (*ElasticDB) Open

func (db *ElasticDB) Open(ctx context.Context, addresses string, opts ...EsOption) error

Open connects to Elasticsearch using the given address(es). Addresses can be comma-separated (e.g. "http://node1:9200,http://node2:9200") or a single URL.

esdb := esdriver.New()
err := esdb.Open(ctx, "http://localhost:9200")

func (*ElasticDB) Ping

func (db *ElasticDB) Ping(ctx context.Context) error

Ping verifies that the Elasticsearch cluster is reachable.

func (*ElasticDB) PutMapping

func (db *ElasticDB) PutMapping(ctx context.Context, index string, mapping M) error

PutMapping updates the field mapping for an index.

func (*ElasticDB) Refresh

func (db *ElasticDB) Refresh(ctx context.Context, indices ...string) error

Refresh forces a refresh on the given indices, making recent writes searchable.

func (*ElasticDB) SetDefaultIndex

func (db *ElasticDB) SetDefaultIndex(name string)

SetDefaultIndex sets the default index used when no index is specified on a query builder.

func (*ElasticDB) SetHooks

func (db *ElasticDB) SetHooks(engine *hook.Engine)

SetHooks attaches a hook engine for lifecycle hooks.

type EsCursor

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

EsCursor provides deep pagination over Elasticsearch search results using the scroll API.

func (*EsCursor) Close

func (c *EsCursor) Close() error

Close clears the scroll context on the Elasticsearch server.

func (*EsCursor) Decode

func (c *EsCursor) Decode(dest any) error

Decode decodes the current hit's _source into dest.

func (*EsCursor) Err

func (c *EsCursor) Err() error

Err returns the first error encountered during iteration.

func (*EsCursor) Hit

func (c *EsCursor) Hit() *Hit

Hit returns the current hit. Must be called after a successful Next().

func (*EsCursor) Next

func (c *EsCursor) Next() bool

Next advances the cursor to the next hit. Returns false when there are no more results or an error occurred.

type EsOption

type EsOption func(*esOptions)

EsOption configures the Elasticsearch driver during Open.

func WithAPIKey

func WithAPIKey(key string) EsOption

WithAPIKey sets the API key for authentication.

func WithAddresses

func WithAddresses(addrs ...string) EsOption

WithAddresses overrides the addresses parsed from the connection string.

func WithBasicAuth

func WithBasicAuth(username, password string) EsOption

WithBasicAuth sets the username and password for HTTP basic authentication.

func WithCACert

func WithCACert(cert []byte) EsOption

WithCACert sets the CA certificate for verifying the server's TLS certificate.

func WithCloudID

func WithCloudID(id string) EsOption

WithCloudID sets the Elastic Cloud ID for connecting to Elastic Cloud.

func WithMaxRetries

func WithMaxRetries(n int) EsOption

WithMaxRetries sets the maximum number of retries for failed requests.

func WithOpenSearch

func WithOpenSearch() EsOption

WithOpenSearch targets an OpenSearch cluster (or any ES-API-compatible server that isn't genuine Elasticsearch). The go-elasticsearch v8 client runs a product check that rejects servers not returning the "X-Elastic-Product: Elasticsearch" response header — which OpenSearch never sends. This wraps the transport to inject that header so the check passes; it composes over any transport set via WithTransport. Leave off for real Elasticsearch.

func WithRefresh

func WithRefresh(policy string) EsOption

WithRefresh sets the default refresh policy for write operations. Valid values: "true" (immediate), "false" (default), "wait_for" (wait until visible).

func WithTransport

func WithTransport(t http.RoundTripper) EsOption

WithTransport sets a custom HTTP transport for the Elasticsearch client.

type EsResult

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

EsResult wraps the outcome of an Elasticsearch write operation and implements the driver.Result interface (RowsAffected, LastInsertId).

func (*EsResult) Action

func (r *EsResult) Action() string

Action returns the result action: "created", "updated", "deleted", or "noop".

func (*EsResult) DocumentID

func (r *EsResult) DocumentID() string

DocumentID returns the _id of the affected document.

func (*EsResult) LastInsertId

func (r *EsResult) LastInsertId() (int64, error)

LastInsertId always returns 0 and an error because Elasticsearch does not provide auto-incrementing integer IDs. Use DocumentID() instead.

func (*EsResult) RowsAffected

func (r *EsResult) RowsAffected() (int64, error)

RowsAffected returns the number of documents affected by the operation.

func (*EsResult) Version

func (r *EsResult) Version() int64

Version returns the document version after the operation.

type Hit

type Hit struct {
	Index     string          `json:"_index"`
	ID        string          `json:"_id"`
	Score     *float64        `json:"_score"`
	Source    json.RawMessage `json:"_source"`
	Sort      []any           `json:"sort,omitempty"`
	Highlight M               `json:"highlight,omitempty"`
}

Hit represents a single search result document.

type HitsResult

type HitsResult struct {
	Total    TotalHits `json:"total"`
	MaxScore *float64  `json:"max_score"`
	Hits     []Hit     `json:"hits"`
}

HitsResult contains the search hits and metadata.

type InsertQuery

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

InsertQuery builds and executes Elasticsearch index (insert) operations. Use ElasticDB.NewInsert() to create one.

func (*InsertQuery) BuildDoc

func (q *InsertQuery) BuildDoc() (M, error)

BuildDoc converts the model to a document map for insertion. Exported for testing purposes.

func (*InsertQuery) BuildDocs

func (q *InsertQuery) BuildDocs() ([]M, error)

BuildDocs converts a slice model to document maps for insertion. Exported for testing purposes.

func (*InsertQuery) DocumentID

func (q *InsertQuery) DocumentID(id string) *InsertQuery

DocumentID sets an explicit document _id.

func (*InsertQuery) Exec

func (q *InsertQuery) Exec(ctx context.Context) (*EsResult, error)

Exec executes the insert operation. For single documents, uses the Index API. For slices, uses the Bulk API.

func (*InsertQuery) GetIndex

func (q *InsertQuery) GetIndex() string

GetIndex returns the index name. Useful for testing.

func (*InsertQuery) Index

func (q *InsertQuery) Index(name string) *InsertQuery

Index overrides the index name derived from the model.

func (*InsertQuery) Pipeline

func (q *InsertQuery) Pipeline(p string) *InsertQuery

Pipeline sets the ingest pipeline name.

func (*InsertQuery) Refresh

func (q *InsertQuery) Refresh(r string) *InsertQuery

Refresh overrides the default refresh policy for this operation.

func (*InsertQuery) Routing

func (q *InsertQuery) Routing(r string) *InsertQuery

Routing sets the ES routing value.

type M

type M = map[string]any

M is an unordered map for building Elasticsearch JSON request bodies. Analogous to mongodriver.M (bson.M).

type RangeOpts

type RangeOpts struct {
	GT     any    // Greater than
	GTE    any    // Greater than or equal
	LT     any    // Less than
	LTE    any    // Less than or equal
	Format string // Date format (e.g. "yyyy-MM-dd")
}

RangeOpts configures a range query clause.

type Script

type Script struct {
	Source string
	Lang   string
	Params M
}

Script represents an Elasticsearch script for scripted updates.

type SearchQuery

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

SearchQuery builds and executes Elasticsearch search operations. Use ElasticDB.NewSearch() to create one.

func (*SearchQuery) Aggs

func (q *SearchQuery) Aggs(aggs M) *SearchQuery

Aggs sets aggregations on the search query.

func (*SearchQuery) Bool

func (q *SearchQuery) Bool(fn func(b *BoolQuery)) *SearchQuery

Bool provides access to a bool query builder. The callback receives a BoolQuery that can accumulate must, should, must_not, and filter clauses.

func (*SearchQuery) BuildBody

func (q *SearchQuery) BuildBody() (M, error)

BuildBody returns the full request body as a map for testing/inspection.

func (*SearchQuery) Count

func (q *SearchQuery) Count(ctx context.Context) (int64, error)

Count executes a count query and returns the number of matching documents.

func (*SearchQuery) ExcludeSource

func (q *SearchQuery) ExcludeSource(fields ...string) *SearchQuery

ExcludeSource sets _source exclusion (which fields to exclude).

func (*SearchQuery) Exists

func (q *SearchQuery) Exists(field string) *SearchQuery

Exists checks for the existence of a field.

func (*SearchQuery) From

func (q *SearchQuery) From(n int) *SearchQuery

From sets the offset for pagination.

func (*SearchQuery) GetFrom

func (q *SearchQuery) GetFrom() int

GetFrom returns the current from offset.

func (*SearchQuery) GetIndex

func (q *SearchQuery) GetIndex() string

GetIndex returns the current index name.

func (*SearchQuery) GetQuery

func (q *SearchQuery) GetQuery() M

GetQuery returns the current query object.

func (*SearchQuery) GetSize

func (q *SearchQuery) GetSize() int

GetSize returns the current size.

func (*SearchQuery) GetSort

func (q *SearchQuery) GetSort() []M

GetSort returns the current sort clauses.

func (*SearchQuery) Highlight

func (q *SearchQuery) Highlight(h M) *SearchQuery

Highlight sets the highlight configuration.

func (*SearchQuery) Index

func (q *SearchQuery) Index(name string) *SearchQuery

Index overrides the index name derived from the model.

func (*SearchQuery) Match

func (q *SearchQuery) Match(field string, value any) *SearchQuery

Match sets a match query on a field.

func (*SearchQuery) MatchPhrase

func (q *SearchQuery) MatchPhrase(field, phrase string) *SearchQuery

MatchPhrase sets a match_phrase query on a field.

func (*SearchQuery) Range

func (q *SearchQuery) Range(field string, opts RangeOpts) *SearchQuery

Range sets a range query on a field.

func (*SearchQuery) RawQuery

func (q *SearchQuery) RawQuery(query M) *SearchQuery

RawQuery sets the entire query body from a raw map (escape hatch).

func (*SearchQuery) Scan

func (q *SearchQuery) Scan(ctx context.Context) error

Scan executes the search query and decodes results into the model. For slice pointers, it decodes all matching documents. For struct pointers, it decodes the first matching document.

func (*SearchQuery) ScanHits

func (q *SearchQuery) ScanHits(ctx context.Context) (*SearchResult, error)

ScanHits executes the search query and returns the raw SearchResult with scores, highlights, and other metadata.

func (*SearchQuery) Scroll

func (q *SearchQuery) Scroll(ctx context.Context, keepAlive string) (*EsCursor, error)

Scroll creates a scroll cursor for deep pagination.

func (*SearchQuery) SearchAfter

func (q *SearchQuery) SearchAfter(values ...any) *SearchQuery

SearchAfter sets the search_after values for keyset pagination.

func (*SearchQuery) Size

func (q *SearchQuery) Size(n int) *SearchQuery

Size sets the maximum number of results to return.

func (*SearchQuery) Sort

func (q *SearchQuery) Sort(field, order string) *SearchQuery

Sort adds a sort clause. order should be "asc" or "desc".

func (*SearchQuery) SortBy

func (q *SearchQuery) SortBy(sorts ...M) *SearchQuery

SortBy sets multiple sort clauses at once, replacing any existing ones.

func (*SearchQuery) Source

func (q *SearchQuery) Source(fields ...string) *SearchQuery

Source sets _source inclusion (which fields to include in results).

func (*SearchQuery) Term

func (q *SearchQuery) Term(field string, value any) *SearchQuery

Term sets an exact term query on a field.

func (*SearchQuery) Terms

func (q *SearchQuery) Terms(field string, values ...any) *SearchQuery

Terms sets a terms query (IN equivalent) on a field.

func (*SearchQuery) TrackTotalHits

func (q *SearchQuery) TrackTotalHits() *SearchQuery

TrackTotalHits enables exact total hit counting.

type SearchResult

type SearchResult struct {
	Took     int64           `json:"took"`
	TimedOut bool            `json:"timed_out"`
	Hits     HitsResult      `json:"hits"`
	Aggs     json.RawMessage `json:"aggregations,omitempty"`
	ScrollID string          `json:"_scroll_id,omitempty"`
}

SearchResult holds the parsed response from the ES _search API.

type TotalHits

type TotalHits struct {
	Value    int64  `json:"value"`
	Relation string `json:"relation"` // "eq" or "gte"
}

TotalHits holds the total count and its relation (exact or lower bound).

type UpdateQuery

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

UpdateQuery builds and executes Elasticsearch update operations. Use ElasticDB.NewUpdate() to create one.

func (*UpdateQuery) DocumentID

func (q *UpdateQuery) DocumentID(id string) *UpdateQuery

DocumentID sets the document _id for a single-document update.

func (*UpdateQuery) Exec

func (q *UpdateQuery) Exec(ctx context.Context) (*EsResult, error)

Exec executes the update operation.

func (*UpdateQuery) Filter

func (q *UpdateQuery) Filter(f M) *UpdateQuery

Filter sets the query filter for update-by-query operations.

func (*UpdateQuery) GetDoc

func (q *UpdateQuery) GetDoc() M

GetDoc returns the current partial document. Useful for testing.

func (*UpdateQuery) GetFilter

func (q *UpdateQuery) GetFilter() M

GetFilter returns the current filter. Useful for testing.

func (*UpdateQuery) GetIndex

func (q *UpdateQuery) GetIndex() string

GetIndex returns the index name. Useful for testing.

func (*UpdateQuery) Index

func (q *UpdateQuery) Index(name string) *UpdateQuery

Index overrides the index name derived from the model.

func (*UpdateQuery) IsMany

func (q *UpdateQuery) IsMany() bool

IsMany returns whether the query targets multiple documents. Useful for testing.

func (*UpdateQuery) IsUpsert

func (q *UpdateQuery) IsUpsert() bool

IsUpsert returns whether upsert is enabled. Useful for testing.

func (*UpdateQuery) Many

func (q *UpdateQuery) Many() *UpdateQuery

Many configures the query to update all matching documents (update-by-query).

func (*UpdateQuery) Refresh

func (q *UpdateQuery) Refresh(r string) *UpdateQuery

Refresh overrides the default refresh policy for this operation.

func (*UpdateQuery) Set

func (q *UpdateQuery) Set(field string, value any) *UpdateQuery

Set adds a field to the partial document update.

func (*UpdateQuery) SetDoc

func (q *UpdateQuery) SetDoc(doc M) *UpdateQuery

SetDoc sets the entire partial document for update.

func (*UpdateQuery) SetScript

func (q *UpdateQuery) SetScript(s Script) *UpdateQuery

SetScript sets a script for scripted updates.

func (*UpdateQuery) Upsert

func (q *UpdateQuery) Upsert() *UpdateQuery

Upsert enables upsert behavior: if no document matches, insert a new one.

Jump to

Keyboard shortcuts

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