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 ¶
- Variables
- type AggregateQuery
- func (q *AggregateQuery) Avg(name, field string) *AggregateQuery
- func (q *AggregateQuery) BuildBody() M
- func (q *AggregateQuery) Cardinality(name, field string) *AggregateQuery
- func (q *AggregateQuery) DateHistogram(name, field, interval string) *AggregateQuery
- func (q *AggregateQuery) GetAggs() M
- func (q *AggregateQuery) GetIndex() string
- func (q *AggregateQuery) GetQuery() M
- func (q *AggregateQuery) Max(name, field string) *AggregateQuery
- func (q *AggregateQuery) Min(name, field string) *AggregateQuery
- func (q *AggregateQuery) Query(query M) *AggregateQuery
- func (q *AggregateQuery) RawAggs(aggs M) *AggregateQuery
- func (q *AggregateQuery) Scan(ctx context.Context, dest any) error
- func (q *AggregateQuery) ScanRaw(ctx context.Context) (*SearchResult, error)
- func (q *AggregateQuery) Size(n int) *AggregateQuery
- func (q *AggregateQuery) SubAgg(parent string, child M) *AggregateQuery
- func (q *AggregateQuery) Sum(name, field string) *AggregateQuery
- func (q *AggregateQuery) Terms(name, field string) *AggregateQuery
- type AggregateResult
- type BoolQuery
- type BulkAction
- type BulkItem
- type BulkItemResult
- type BulkQuery
- func (q *BulkQuery) Create(index, docID string, doc any) *BulkQuery
- func (q *BulkQuery) Delete(index, docID string) *BulkQuery
- func (q *BulkQuery) Exec(ctx context.Context) (*BulkResult, error)
- func (q *BulkQuery) GetActions() []BulkAction
- func (q *BulkQuery) Index(index, docID string, doc any) *BulkQuery
- func (q *BulkQuery) Refresh(r string) *BulkQuery
- func (q *BulkQuery) Update(index, docID string, doc M) *BulkQuery
- type BulkResult
- type DeleteQuery
- func (q *DeleteQuery) DocumentID(id string) *DeleteQuery
- func (q *DeleteQuery) Exec(ctx context.Context) (*EsResult, error)
- func (q *DeleteQuery) Filter(f M) *DeleteQuery
- func (q *DeleteQuery) GetFilter() M
- func (q *DeleteQuery) GetIndex() string
- func (q *DeleteQuery) Index(name string) *DeleteQuery
- func (q *DeleteQuery) IsMany() bool
- func (q *DeleteQuery) Many() *DeleteQuery
- func (q *DeleteQuery) Refresh(r string) *DeleteQuery
- type ElasticDB
- func (db *ElasticDB) Client() *elasticsearch.Client
- func (db *ElasticDB) Close() error
- func (db *ElasticDB) CreateIndex(ctx context.Context, name string, mapping M) error
- func (db *ElasticDB) DefaultIndex() string
- func (db *ElasticDB) DeleteIndex(ctx context.Context, name string) error
- func (db *ElasticDB) GroveDelete(model any) any
- func (db *ElasticDB) GroveInsert(model any) any
- func (db *ElasticDB) GroveSelect(model ...any) any
- func (db *ElasticDB) GroveUpdate(model any) any
- func (db *ElasticDB) IndexExists(ctx context.Context, name string) (bool, error)
- func (db *ElasticDB) Name() string
- func (db *ElasticDB) NewAggregate(index string) *AggregateQuery
- func (db *ElasticDB) NewBulk() *BulkQuery
- func (db *ElasticDB) NewDelete(model any) *DeleteQuery
- func (db *ElasticDB) NewInsert(model any) *InsertQuery
- func (db *ElasticDB) NewSearch(model ...any) *SearchQuery
- func (db *ElasticDB) NewUpdate(model any) *UpdateQuery
- func (db *ElasticDB) Open(ctx context.Context, addresses string, opts ...EsOption) error
- func (db *ElasticDB) Ping(ctx context.Context) error
- func (db *ElasticDB) PutMapping(ctx context.Context, index string, mapping M) error
- func (db *ElasticDB) Refresh(ctx context.Context, indices ...string) error
- func (db *ElasticDB) SetDefaultIndex(name string)
- func (db *ElasticDB) SetHooks(engine *hook.Engine)
- type EsCursor
- type EsOption
- func WithAPIKey(key string) EsOption
- func WithAddresses(addrs ...string) EsOption
- func WithBasicAuth(username, password string) EsOption
- func WithCACert(cert []byte) EsOption
- func WithCloudID(id string) EsOption
- func WithMaxRetries(n int) EsOption
- func WithOpenSearch() EsOption
- func WithRefresh(policy string) EsOption
- func WithTransport(t http.RoundTripper) EsOption
- type EsResult
- type Hit
- type HitsResult
- type InsertQuery
- func (q *InsertQuery) BuildDoc() (M, error)
- func (q *InsertQuery) BuildDocs() ([]M, error)
- func (q *InsertQuery) DocumentID(id string) *InsertQuery
- func (q *InsertQuery) Exec(ctx context.Context) (*EsResult, error)
- func (q *InsertQuery) GetIndex() string
- func (q *InsertQuery) Index(name string) *InsertQuery
- func (q *InsertQuery) Pipeline(p string) *InsertQuery
- func (q *InsertQuery) Refresh(r string) *InsertQuery
- func (q *InsertQuery) Routing(r string) *InsertQuery
- type M
- type RangeOpts
- type Script
- type SearchQuery
- func (q *SearchQuery) Aggs(aggs M) *SearchQuery
- func (q *SearchQuery) Bool(fn func(b *BoolQuery)) *SearchQuery
- func (q *SearchQuery) BuildBody() (M, error)
- func (q *SearchQuery) Count(ctx context.Context) (int64, error)
- func (q *SearchQuery) ExcludeSource(fields ...string) *SearchQuery
- func (q *SearchQuery) Exists(field string) *SearchQuery
- func (q *SearchQuery) From(n int) *SearchQuery
- func (q *SearchQuery) GetFrom() int
- func (q *SearchQuery) GetIndex() string
- func (q *SearchQuery) GetQuery() M
- func (q *SearchQuery) GetSize() int
- func (q *SearchQuery) GetSort() []M
- func (q *SearchQuery) Highlight(h M) *SearchQuery
- func (q *SearchQuery) Index(name string) *SearchQuery
- func (q *SearchQuery) Match(field string, value any) *SearchQuery
- func (q *SearchQuery) MatchPhrase(field, phrase string) *SearchQuery
- func (q *SearchQuery) Range(field string, opts RangeOpts) *SearchQuery
- func (q *SearchQuery) RawQuery(query M) *SearchQuery
- func (q *SearchQuery) Scan(ctx context.Context) error
- func (q *SearchQuery) ScanHits(ctx context.Context) (*SearchResult, error)
- func (q *SearchQuery) Scroll(ctx context.Context, keepAlive string) (*EsCursor, error)
- func (q *SearchQuery) SearchAfter(values ...any) *SearchQuery
- func (q *SearchQuery) Size(n int) *SearchQuery
- func (q *SearchQuery) Sort(field, order string) *SearchQuery
- func (q *SearchQuery) SortBy(sorts ...M) *SearchQuery
- func (q *SearchQuery) Source(fields ...string) *SearchQuery
- func (q *SearchQuery) Term(field string, value any) *SearchQuery
- func (q *SearchQuery) Terms(field string, values ...any) *SearchQuery
- func (q *SearchQuery) TrackTotalHits() *SearchQuery
- type SearchResult
- type TotalHits
- type UpdateQuery
- func (q *UpdateQuery) DocumentID(id string) *UpdateQuery
- func (q *UpdateQuery) Exec(ctx context.Context) (*EsResult, error)
- func (q *UpdateQuery) Filter(f M) *UpdateQuery
- func (q *UpdateQuery) GetDoc() M
- func (q *UpdateQuery) GetFilter() M
- func (q *UpdateQuery) GetIndex() string
- func (q *UpdateQuery) Index(name string) *UpdateQuery
- func (q *UpdateQuery) IsMany() bool
- func (q *UpdateQuery) IsUpsert() bool
- func (q *UpdateQuery) Many() *UpdateQuery
- func (q *UpdateQuery) Refresh(r string) *UpdateQuery
- func (q *UpdateQuery) Set(field string, value any) *UpdateQuery
- func (q *UpdateQuery) SetDoc(doc M) *UpdateQuery
- func (q *UpdateQuery) SetScript(s Script) *UpdateQuery
- func (q *UpdateQuery) Upsert() *UpdateQuery
Constants ¶
This section is empty.
Variables ¶
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.
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) MinimumShouldMatch ¶
MinimumShouldMatch sets the minimum number of should clauses that must match.
func (*BoolQuery) Must ¶
Must adds a clause to the must array (AND semantics, contributes to 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) 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.
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 ¶
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 ¶
Close is a no-op for Elasticsearch since the HTTP client does not require explicit cleanup.
func (*ElasticDB) CreateIndex ¶
CreateIndex creates an Elasticsearch index with the given mapping.
func (*ElasticDB) DefaultIndex ¶
DefaultIndex returns the default index name, if set.
func (*ElasticDB) DeleteIndex ¶
DeleteIndex deletes an Elasticsearch index.
func (*ElasticDB) GroveDelete ¶
GroveDelete is the adapter method for grove.DB.NewDelete().
func (*ElasticDB) GroveInsert ¶
GroveInsert is the adapter method for grove.DB.NewInsert().
func (*ElasticDB) GroveSelect ¶
GroveSelect is the adapter method for grove.DB.NewSelect().
func (*ElasticDB) GroveUpdate ¶
GroveUpdate is the adapter method for grove.DB.NewUpdate().
func (*ElasticDB) IndexExists ¶
IndexExists checks whether an Elasticsearch index exists.
func (*ElasticDB) NewAggregate ¶
func (db *ElasticDB) NewAggregate(index string) *AggregateQuery
NewAggregate creates a new aggregation query for the given index.
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 ¶
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) PutMapping ¶
PutMapping updates the field mapping for an index.
func (*ElasticDB) Refresh ¶
Refresh forces a refresh on the given indices, making recent writes searchable.
func (*ElasticDB) SetDefaultIndex ¶
SetDefaultIndex sets the default index used when no index is specified on a query builder.
type EsCursor ¶
type EsCursor struct {
// contains filtered or unexported fields
}
EsCursor provides deep pagination over Elasticsearch search results using the scroll API.
type EsOption ¶
type EsOption func(*esOptions)
EsOption configures the Elasticsearch driver during Open.
func WithAPIKey ¶
WithAPIKey sets the API key for authentication.
func WithAddresses ¶
WithAddresses overrides the addresses parsed from the connection string.
func WithBasicAuth ¶
WithBasicAuth sets the username and password for HTTP basic authentication.
func WithCACert ¶
WithCACert sets the CA certificate for verifying the server's TLS certificate.
func WithCloudID ¶
WithCloudID sets the Elastic Cloud ID for connecting to Elastic Cloud.
func WithMaxRetries ¶
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 ¶
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 ¶
Action returns the result action: "created", "updated", "deleted", or "noop".
func (*EsResult) DocumentID ¶
DocumentID returns the _id of the affected document.
func (*EsResult) LastInsertId ¶
LastInsertId always returns 0 and an error because Elasticsearch does not provide auto-incrementing integer IDs. Use DocumentID() instead.
func (*EsResult) RowsAffected ¶
RowsAffected returns the number of documents affected by 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 ¶
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 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) 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) 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.