Documentation ¶
Index ¶
- Constants
- Variables
- func Client() *elasticsearch.Client
- func Init() (err error)
- func SearchTimestamp(index string, size ...int) ([]byte, error)
- type AggregationOptions
- type AggregationResult
- type GetRequest
- type GetResult
- type IndexOption
- type MatchPhraseOptions
- type Order
- type Pagination
- type QueryBuilder
- func (qb *QueryBuilder) Aggs(name string, agg map[string]any) *QueryBuilder
- func (qb *QueryBuilder) AggsAvg(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsCardinality(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsMax(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsMin(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsStats(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsSum(name string, field string) *QueryBuilder
- func (qb *QueryBuilder) AggsTerm(name string, field string, size int, orderBy ...string) *QueryBuilder
- func (qb *QueryBuilder) Bool(fn func(qb *QueryBuilder)) *QueryBuilder
- func (qb *QueryBuilder) Build() (*SearchRequest, error)
- func (qb *QueryBuilder) BuildForce() *SearchRequest
- func (qb *QueryBuilder) BuildQuery() map[string]any
- func (qb *QueryBuilder) Clone() *QueryBuilder
- func (qb *QueryBuilder) Exists(field string) *QueryBuilder
- func (qb *QueryBuilder) Filter(query map[string]any) *QueryBuilder
- func (qb *QueryBuilder) From(from int) *QueryBuilder
- func (qb *QueryBuilder) Match(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MatchAll() *QueryBuilder
- func (qb *QueryBuilder) MatchNot(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MatchPhrase(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MatchPhraseNot(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MatchPhraseOptions(field string, opts MatchPhraseOptions) *QueryBuilder
- func (qb *QueryBuilder) MatchPhraseOptionsNot(field string, opts MatchPhraseOptions) *QueryBuilder
- func (qb *QueryBuilder) MatchPhraseOptionsShould(field string, opts MatchPhraseOptions) *QueryBuilder
- func (qb *QueryBuilder) MatchPhraseShould(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MatchShould(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) MinimumShouldMatch(minimum any) *QueryBuilder
- func (qb *QueryBuilder) Must(query map[string]any) *QueryBuilder
- func (qb *QueryBuilder) MustNot(query map[string]any) *QueryBuilder
- func (qb *QueryBuilder) Range(field string, ranges map[string]any) *QueryBuilder
- func (qb *QueryBuilder) SearchAfter(value ...any) *QueryBuilder
- func (qb *QueryBuilder) Should(query map[string]any) *QueryBuilder
- func (qb *QueryBuilder) Size(size int) *QueryBuilder
- func (qb *QueryBuilder) Sort(field string, order Order) *QueryBuilder
- func (qb *QueryBuilder) Source(fields ...string) *QueryBuilder
- func (qb *QueryBuilder) String() string
- func (qb *QueryBuilder) Term(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) TermNot(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) TermShould(field string, value any) *QueryBuilder
- func (qb *QueryBuilder) TimeRange(field string, start, end time.Time) *QueryBuilder
- func (qb *QueryBuilder) TimeRangeGte(field string, tm time.Time) *QueryBuilder
- func (qb *QueryBuilder) TimeRangeLte(field string, tm time.Time) *QueryBuilder
- func (qb *QueryBuilder) Validate() error
- type SearchHit
- type SearchRequest
- type SearchResult
Constants ¶
const (
TIMESTAMP_FILE = "/tmp/timestamp"
)
Variables ¶
var ( Document = new(document) Index = new(index) )
Functions ¶
Types ¶
type AggregationOptions ¶ added in v0.0.58
type AggregationOptions struct { Field string `json:"field"` Size *int `json:"size,omitempty"` Order map[string]string `json:"order,omitempty"` }
AggregationOptions represents options for aggregation query
type AggregationResult ¶ added in v0.0.58
type AggregationResult struct { Value any `json:"value,omitempty"` // For metric aggregations DocCount int64 `json:"doc_count,omitempty"` // Document count Buckets []map[string]any `json:"buckets,omitempty"` // For bucket aggregations }
AggregationResult represents the result of an aggregation. It can contain single value metrics (min, max, avg, sum) or bucket results (terms).
type GetRequest ¶ added in v0.0.55
type GetRequest struct {
Source []string // 指定返回的字段
}
type IndexOption ¶ added in v0.0.55
type MatchPhraseOptions ¶ added in v0.0.58
type MatchPhraseOptions struct { Query any `json:"query"` Slop *int `json:"slop,omitempty"` Analyzer string `json:"analyzer,omitempty"` ZeroTermsQuery string `json:"zero_terms_query,omitempty"` MaxExpansions *int `json:"max_expansions,omitempty"` Boost *float64 `json:"boost,omitempty"` }
MatchPhraseOptions represents options for match_phrase query
type Pagination ¶
type QueryBuilder ¶ added in v0.0.55
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder helps build Elasticsearch queries. It supports must, must_not, should, filter clauses, pagination, sorting, field filtering and search_after.
func NewQueryBuilder ¶ added in v0.0.55
func NewQueryBuilder() *QueryBuilder
NewQueryBuilder creates a new query builder with default size=10 and from=0
func (*QueryBuilder) Aggs ¶ added in v0.0.58
func (qb *QueryBuilder) Aggs(name string, agg map[string]any) *QueryBuilder
Aggs adds a custom aggregation
func (*QueryBuilder) AggsAvg ¶ added in v0.0.58
func (qb *QueryBuilder) AggsAvg(name string, field string) *QueryBuilder
AggsAvg adds an avg aggregation
func (*QueryBuilder) AggsCardinality ¶ added in v0.0.58
func (qb *QueryBuilder) AggsCardinality(name string, field string) *QueryBuilder
AggsCardinality adds a cardinality aggregation
func (*QueryBuilder) AggsMax ¶ added in v0.0.58
func (qb *QueryBuilder) AggsMax(name string, field string) *QueryBuilder
AggsMax adds a max aggregation
func (*QueryBuilder) AggsMin ¶ added in v0.0.58
func (qb *QueryBuilder) AggsMin(name string, field string) *QueryBuilder
AggsMin adds a min aggregation
func (*QueryBuilder) AggsStats ¶ added in v0.0.58
func (qb *QueryBuilder) AggsStats(name string, field string) *QueryBuilder
AggsStats adds a stats aggregation
func (*QueryBuilder) AggsSum ¶ added in v0.0.58
func (qb *QueryBuilder) AggsSum(name string, field string) *QueryBuilder
AggsSum adds a sum aggregation
func (*QueryBuilder) AggsTerm ¶ added in v0.0.58
func (qb *QueryBuilder) AggsTerm(name string, field string, size int, orderBy ...string) *QueryBuilder
AggsTerm adds a terms aggregation AggsTerm adds a terms aggregation. orderBy accepts two parameters: field and order. field can be "_count" or "_key", order can be "asc" or "desc"
func (*QueryBuilder) Bool ¶ added in v0.0.57
func (qb *QueryBuilder) Bool(fn func(qb *QueryBuilder)) *QueryBuilder
Bool adds a nested bool query
func (*QueryBuilder) Build ¶ added in v0.0.55
func (qb *QueryBuilder) Build() (*SearchRequest, error)
Build creates a SearchRequest with validation
func (*QueryBuilder) BuildForce ¶ added in v0.0.57
func (qb *QueryBuilder) BuildForce() *SearchRequest
BuildForce creates a SearchRequest without validation
func (*QueryBuilder) BuildQuery ¶ added in v0.0.57
func (qb *QueryBuilder) BuildQuery() map[string]any
BuildQuery creates the query part of SearchRequest
func (*QueryBuilder) Clone ¶ added in v0.0.55
func (qb *QueryBuilder) Clone() *QueryBuilder
Clone creates a deep copy of the QueryBuilder
func (*QueryBuilder) Exists ¶ added in v0.0.55
func (qb *QueryBuilder) Exists(field string) *QueryBuilder
Exists adds an exists query to must clauses
func (*QueryBuilder) Filter ¶ added in v0.0.55
func (qb *QueryBuilder) Filter(query map[string]any) *QueryBuilder
Filter adds a filter clause to the bool query
func (*QueryBuilder) From ¶ added in v0.0.55
func (qb *QueryBuilder) From(from int) *QueryBuilder
From sets the from parameter, must be non-negative
func (*QueryBuilder) Match ¶ added in v0.0.55
func (qb *QueryBuilder) Match(field string, value any) *QueryBuilder
Match adds a match query to must clauses
func (*QueryBuilder) MatchAll ¶ added in v0.0.55
func (qb *QueryBuilder) MatchAll() *QueryBuilder
MatchAll adds a match_all query
func (*QueryBuilder) MatchNot ¶ added in v0.0.55
func (qb *QueryBuilder) MatchNot(field string, value any) *QueryBuilder
MatchNot adds a match query to must_not clauses
func (*QueryBuilder) MatchPhrase ¶ added in v0.0.55
func (qb *QueryBuilder) MatchPhrase(field string, value any) *QueryBuilder
MatchPhrase adds a match_phrase query to must clauses
func (*QueryBuilder) MatchPhraseNot ¶ added in v0.0.55
func (qb *QueryBuilder) MatchPhraseNot(field string, value any) *QueryBuilder
MatchPhraseNot adds a match_phrase query to must_not clauses
func (*QueryBuilder) MatchPhraseOptions ¶ added in v0.0.58
func (qb *QueryBuilder) MatchPhraseOptions(field string, opts MatchPhraseOptions) *QueryBuilder
MatchPhraseOptions adds a match_phrase query with options to must clauses
func (*QueryBuilder) MatchPhraseOptionsNot ¶ added in v0.0.58
func (qb *QueryBuilder) MatchPhraseOptionsNot(field string, opts MatchPhraseOptions) *QueryBuilder
MatchPhraseOptionsNot adds a match_phrase query with options to must_not clauses
func (*QueryBuilder) MatchPhraseOptionsShould ¶ added in v0.0.58
func (qb *QueryBuilder) MatchPhraseOptionsShould(field string, opts MatchPhraseOptions) *QueryBuilder
MatchPhraseOptionsShould adds a match_phrase query with options to should clauses
func (*QueryBuilder) MatchPhraseShould ¶ added in v0.0.55
func (qb *QueryBuilder) MatchPhraseShould(field string, value any) *QueryBuilder
MatchPhraseShould adds a match_phrase query to should clauses
func (*QueryBuilder) MatchShould ¶ added in v0.0.55
func (qb *QueryBuilder) MatchShould(field string, value any) *QueryBuilder
MatchShould adds a match query to should clauses
func (*QueryBuilder) MinimumShouldMatch ¶ added in v0.0.55
func (qb *QueryBuilder) MinimumShouldMatch(minimum any) *QueryBuilder
MinimumShouldMatch sets minimum_should_match for should clauses
func (*QueryBuilder) Must ¶ added in v0.0.55
func (qb *QueryBuilder) Must(query map[string]any) *QueryBuilder
Must adds a must clause to the bool query
func (*QueryBuilder) MustNot ¶ added in v0.0.55
func (qb *QueryBuilder) MustNot(query map[string]any) *QueryBuilder
MustNot adds a must_not clause to the bool query
func (*QueryBuilder) Range ¶ added in v0.0.55
func (qb *QueryBuilder) Range(field string, ranges map[string]any) *QueryBuilder
Range adds a range query to filter clauses
func (*QueryBuilder) SearchAfter ¶ added in v0.0.55
func (qb *QueryBuilder) SearchAfter(value ...any) *QueryBuilder
SearchAfter sets the search_after parameter for deep pagination SearchAfter always used with Sort.
func (*QueryBuilder) Should ¶ added in v0.0.55
func (qb *QueryBuilder) Should(query map[string]any) *QueryBuilder
Should adds a should clause to the bool query
func (*QueryBuilder) Size ¶ added in v0.0.55
func (qb *QueryBuilder) Size(size int) *QueryBuilder
Size sets the size parameter, must be positive
func (*QueryBuilder) Sort ¶ added in v0.0.55
func (qb *QueryBuilder) Sort(field string, order Order) *QueryBuilder
Sort adds a sort condition Sort always used with SearchAfter.
func (*QueryBuilder) Source ¶ added in v0.0.55
func (qb *QueryBuilder) Source(fields ...string) *QueryBuilder
Source sets the _source field filtering if fields is empty, all fields will be returned if fields is not empty, only the specified fields will be returned if fields is nil or empty array, no fields will be returned
func (*QueryBuilder) String ¶ added in v0.0.55
func (qb *QueryBuilder) String() string
String returns the JSON representation of the query
func (*QueryBuilder) Term ¶ added in v0.0.55
func (qb *QueryBuilder) Term(field string, value any) *QueryBuilder
Term adds a term query to must clauses
func (*QueryBuilder) TermNot ¶ added in v0.0.55
func (qb *QueryBuilder) TermNot(field string, value any) *QueryBuilder
TermNot adds a term query to must_not clauses
func (*QueryBuilder) TermShould ¶ added in v0.0.55
func (qb *QueryBuilder) TermShould(field string, value any) *QueryBuilder
TermShould adds a term query to should clauses
func (*QueryBuilder) TimeRange ¶ added in v0.0.55
func (qb *QueryBuilder) TimeRange(field string, start, end time.Time) *QueryBuilder
TimeRange adds a time range query with RFC3339 format
func (*QueryBuilder) TimeRangeGte ¶ added in v0.0.58
func (qb *QueryBuilder) TimeRangeGte(field string, tm time.Time) *QueryBuilder
TimeRangeGte adds a time range query with greater than or equal condition
func (*QueryBuilder) TimeRangeLte ¶ added in v0.0.58
func (qb *QueryBuilder) TimeRangeLte(field string, tm time.Time) *QueryBuilder
TimeRangeLte adds a time range query with less than or equal condition
func (*QueryBuilder) Validate ¶ added in v0.0.55
func (qb *QueryBuilder) Validate() error
Validate checks if the query parameters are valid
type SearchHit ¶ added in v0.0.55
type SearchHit struct { ID string `json:"_id"` Score *float64 `json:"_score,omitempty"` Source map[string]any `json:"_source"` }
SearchHit represents a single document in search results. It contains document ID, relevance score and source fields.
type SearchRequest ¶ added in v0.0.55
type SearchRequest struct { Query map[string]any `json:"query,omitempty"` From int `json:"from,omitempty"` Size int `json:"size,omitempty"` Sort []map[string]any `json:"sort,omitempty"` Source []string `json:"_source,omitempty"` SearchAfter []any `json:"search_after,omitempty"` Aggs map[string]any `json:"aggs,omitempty"` }
SearchRequest represents an Elasticsearch search query. It includes query conditions, pagination, sorting, field filtering and search_after for deep pagination. You can use QueryBuilder to create a SearchRequest.
type SearchResult ¶ added in v0.0.55
type SearchResult struct { Total int64 `json:"total"` MaxScore *float64 `json:"max_score,omitempty"` Hits []SearchHit `json:"hits"` Aggregations map[string]*AggregationResult `json:"aggregations,omitempty"` }
SearchResult represents an Elasticsearch search response. It contains total hits count, max score, hits array and aggregations.