elastic

package
v0.10.14 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TIMESTAMP_FILE = "/tmp/timestamp" //nolint:staticcheck

)

Variables

View Source
var (
	Document = new(document)
	Index    = new(index)
)

Functions

func Client

func Client() *elasticsearch.Client

func Init

func Init() (err error)

Init initializes the global elasticsearch client. It reads elasticsearch configuration from config.App.ElasticsearchConfig. If elasticsearch not enabled, it returns nil. The functions also starts a background goroutines to ensure connection health.

func New

New returns a new Elasticsearch client with given configuration. It's the caller's responsibility to ensure proper usage of the client.

func SearchTimestamp

func SearchTimestamp(index string, size ...int) ([]byte, error)

SearchTimestamp WithIndex("winlog*"), // 使用通配符查询多个索引

Types

type AggregationOptions

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

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

type GetRequest struct {
	Source []string // 指定返回的字段
}

type GetResult

type GetResult struct {
	ID     string         `json:"_id"`
	Found  bool           `json:"found"`
	Source map[string]any `json:"_source,omitempty"`
}

type IndexOption

type IndexOption struct {
	Settings map[string]any
	Mappings map[string]any
}

type MatchPhraseOptions

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 Order

type Order string
const (
	Asc  Order = "asc"
	Desc Order = "desc"
)

type Pagination

type Pagination struct {
	Page int // page number
	Size int // page size
}

type QueryBuilder

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

func NewQueryBuilder() *QueryBuilder

NewQueryBuilder creates a new query builder with default size=10 and from=0

func (*QueryBuilder) Aggs

func (qb *QueryBuilder) Aggs(name string, agg map[string]any) *QueryBuilder

Aggs adds a custom aggregation

func (*QueryBuilder) AggsAvg

func (qb *QueryBuilder) AggsAvg(name string, field string) *QueryBuilder

AggsAvg adds an avg aggregation

func (*QueryBuilder) AggsCardinality

func (qb *QueryBuilder) AggsCardinality(name string, field string) *QueryBuilder

AggsCardinality adds a cardinality aggregation

func (*QueryBuilder) AggsMax

func (qb *QueryBuilder) AggsMax(name string, field string) *QueryBuilder

AggsMax adds a max aggregation

func (*QueryBuilder) AggsMin

func (qb *QueryBuilder) AggsMin(name string, field string) *QueryBuilder

AggsMin adds a min aggregation

func (*QueryBuilder) AggsStats

func (qb *QueryBuilder) AggsStats(name string, field string) *QueryBuilder

AggsStats adds a stats aggregation

func (*QueryBuilder) AggsSum

func (qb *QueryBuilder) AggsSum(name string, field string) *QueryBuilder

AggsSum adds a sum aggregation

func (*QueryBuilder) AggsTerm

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

func (qb *QueryBuilder) Bool(fn func(qb *QueryBuilder)) *QueryBuilder

Bool adds a nested bool query

func (*QueryBuilder) Build

func (qb *QueryBuilder) Build() (*SearchRequest, error)

Build creates a SearchRequest with validation

func (*QueryBuilder) BuildForce

func (qb *QueryBuilder) BuildForce() *SearchRequest

BuildForce creates a SearchRequest without validation

func (*QueryBuilder) BuildQuery

func (qb *QueryBuilder) BuildQuery() map[string]any

BuildQuery creates the query part of SearchRequest

func (*QueryBuilder) Clone

func (qb *QueryBuilder) Clone() *QueryBuilder

Clone creates a deep copy of the QueryBuilder

func (*QueryBuilder) Exists

func (qb *QueryBuilder) Exists(field string) *QueryBuilder

Exists adds an exists query to must clauses

func (*QueryBuilder) Filter

func (qb *QueryBuilder) Filter(query map[string]any) *QueryBuilder

Filter adds a filter clause to the bool query

func (*QueryBuilder) From

func (qb *QueryBuilder) From(from int) *QueryBuilder

From sets the from parameter, must be non-negative

func (*QueryBuilder) Match

func (qb *QueryBuilder) Match(field string, value any) *QueryBuilder

Match adds a match query to must clauses

func (*QueryBuilder) MatchAll

func (qb *QueryBuilder) MatchAll() *QueryBuilder

MatchAll adds a match_all query

func (*QueryBuilder) MatchNot

func (qb *QueryBuilder) MatchNot(field string, value any) *QueryBuilder

MatchNot adds a match query to must_not clauses

func (*QueryBuilder) MatchPhrase

func (qb *QueryBuilder) MatchPhrase(field string, value any) *QueryBuilder

MatchPhrase adds a match_phrase query to must clauses

func (*QueryBuilder) MatchPhraseNot

func (qb *QueryBuilder) MatchPhraseNot(field string, value any) *QueryBuilder

MatchPhraseNot adds a match_phrase query to must_not clauses

func (*QueryBuilder) MatchPhraseOptions

func (qb *QueryBuilder) MatchPhraseOptions(field string, opts MatchPhraseOptions) *QueryBuilder

MatchPhraseOptions adds a match_phrase query with options to must clauses

func (*QueryBuilder) MatchPhraseOptionsNot

func (qb *QueryBuilder) MatchPhraseOptionsNot(field string, opts MatchPhraseOptions) *QueryBuilder

MatchPhraseOptionsNot adds a match_phrase query with options to must_not clauses

func (*QueryBuilder) MatchPhraseOptionsShould

func (qb *QueryBuilder) MatchPhraseOptionsShould(field string, opts MatchPhraseOptions) *QueryBuilder

MatchPhraseOptionsShould adds a match_phrase query with options to should clauses

func (*QueryBuilder) MatchPhraseShould

func (qb *QueryBuilder) MatchPhraseShould(field string, value any) *QueryBuilder

MatchPhraseShould adds a match_phrase query to should clauses

func (*QueryBuilder) MatchShould

func (qb *QueryBuilder) MatchShould(field string, value any) *QueryBuilder

MatchShould adds a match query to should clauses

func (*QueryBuilder) MinimumShouldMatch

func (qb *QueryBuilder) MinimumShouldMatch(minimum any) *QueryBuilder

MinimumShouldMatch sets minimum_should_match for should clauses

func (*QueryBuilder) Must

func (qb *QueryBuilder) Must(query map[string]any) *QueryBuilder

Must adds a must clause to the bool query

func (*QueryBuilder) MustNot

func (qb *QueryBuilder) MustNot(query map[string]any) *QueryBuilder

MustNot adds a must_not clause to the bool query

func (*QueryBuilder) Range

func (qb *QueryBuilder) Range(field string, ranges map[string]any) *QueryBuilder

Range adds a range query to filter clauses

func (*QueryBuilder) SearchAfter

func (qb *QueryBuilder) SearchAfter(value ...any) *QueryBuilder

SearchAfter sets the search_after parameter for deep pagination SearchAfter always used with Sort.

func (*QueryBuilder) Should

func (qb *QueryBuilder) Should(query map[string]any) *QueryBuilder

Should adds a should clause to the bool query

func (*QueryBuilder) Size

func (qb *QueryBuilder) Size(size int) *QueryBuilder

Size sets the size parameter, must be positive

func (*QueryBuilder) Sort

func (qb *QueryBuilder) Sort(field string, order Order) *QueryBuilder

Sort adds a sort condition Sort always used with SearchAfter.

func (*QueryBuilder) Source

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

func (qb *QueryBuilder) String() string

String returns the JSON representation of the query

func (*QueryBuilder) Term

func (qb *QueryBuilder) Term(field string, value any) *QueryBuilder

Term adds a term query to must clauses

func (*QueryBuilder) TermNot

func (qb *QueryBuilder) TermNot(field string, value any) *QueryBuilder

TermNot adds a term query to must_not clauses

func (*QueryBuilder) TermShould

func (qb *QueryBuilder) TermShould(field string, value any) *QueryBuilder

TermShould adds a term query to should clauses

func (*QueryBuilder) TimeRange

func (qb *QueryBuilder) TimeRange(field string, start, end time.Time) *QueryBuilder

TimeRange adds a time range query with RFC3339 format

func (*QueryBuilder) TimeRangeGte

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

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

func (qb *QueryBuilder) Validate() error

Validate checks if the query parameters are valid

type SearchHit

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

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

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.

Jump to

Keyboard shortcuts

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