elasticsearch

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

README

framework-elasticsearch

Go framework package for elasticsearch.

Installation

go get github.com/go-anyway/framework-elasticsearch@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled      bool               `yaml:"enabled" env:"ELASTICSEARCH_ENABLED" default:"true"`
	Addresses    []string           `yaml:"addresses" env:"ELASTICSEARCH_ADDRESSES" required:"true"`
	Username     string             `yaml:"username" env:"ELASTICSEARCH_USERNAME"`
	Password     string             `yaml:"password" env:"ELASTICSEARCH_PASSWORD"`
	CloudID      string             `yaml:"cloud_id" env:"ELASTICSEARCH_CLOUD_ID"`
	APIKey       string             `yaml:"api_key" env:"ELASTICSEARCH_API_KEY"`
	EnableTLS    bool               `yaml:"enable_tls" env:"ELASTICSEARCH_ENABLE_TLS" default:"false"`
	CACert       string             `yaml:"ca_cert" env:"ELASTICSEARCH_CA_CERT"`
	DialTimeout  pkgConfig.Duration `yaml:"dial_timeout" env:"ELASTICSEARCH_DIAL_TIMEOUT" default:"30s"`
	ReadTimeout  pkgConfig.Duration `yaml:"read_timeout" env:"ELASTICSEARCH_READ_TIMEOUT" default:"30s"`
	WriteTimeout pkgConfig.Duration `yaml:"write_timeout" env:"ELASTICSEARCH_WRITE_TIMEOUT" default:"30s"`
	MaxRetries   int                `yaml:"max_retries" env:"ELASTICSEARCH_MAX_RETRIES" default:"3"`
	EnableTrace  bool               `yaml:"enable_trace" env:"ELASTICSEARCH_ENABLE_TRACE" default:"true"`
}

Config Elasticsearch 配置结构体(用于从配置文件创建)

func (*Config) DialTimeoutDuration

func (c *Config) DialTimeoutDuration() time.Duration

DialTimeoutDuration 返回 time.Duration 类型的 DialTimeout

func (*Config) ReadTimeoutDuration

func (c *Config) ReadTimeoutDuration() time.Duration

ReadTimeoutDuration 返回 time.Duration 类型的 ReadTimeout

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证 Elasticsearch 配置

func (*Config) WriteTimeoutDuration

func (c *Config) WriteTimeoutDuration() time.Duration

WriteTimeoutDuration 返回 time.Duration 类型的 WriteTimeout

type ElasticsearchClient

type ElasticsearchClient struct {
	EnableTrace bool // 是否启用追踪
	// contains filtered or unexported fields
}

ElasticsearchClient Elasticsearch 客户端接口

func NewElasticsearch

func NewElasticsearch(opts *Options) (*ElasticsearchClient, error)

NewElasticsearch 根据给定的选项创建一个新的 Elasticsearch 客户端实例

func (*ElasticsearchClient) Bulk

func (c *ElasticsearchClient) Bulk(ctx context.Context, body string) error

Bulk 批量操作(自动处理追踪)

func (*ElasticsearchClient) Close

func (c *ElasticsearchClient) Close() error

Close 关闭 Elasticsearch 客户端连接

func (*ElasticsearchClient) Count

func (c *ElasticsearchClient) Count(ctx context.Context, index string, query map[string]interface{}) (int64, error)

Count 统计文档数量

func (*ElasticsearchClient) CreateIndex

func (c *ElasticsearchClient) CreateIndex(ctx context.Context, index string, settings map[string]interface{}) error

CreateIndex 创建索引

func (*ElasticsearchClient) Delete

func (c *ElasticsearchClient) Delete(ctx context.Context, index string, documentID string) error

Delete 删除文档(自动处理追踪)

func (*ElasticsearchClient) DeleteByQuery

func (c *ElasticsearchClient) DeleteByQuery(ctx context.Context, index string, query map[string]interface{}) (map[string]interface{}, error)

DeleteByQuery 根据查询删除文档

func (*ElasticsearchClient) DeleteIndex

func (c *ElasticsearchClient) DeleteIndex(ctx context.Context, index string) error

DeleteIndex 删除索引

func (*ElasticsearchClient) ExistsIndex

func (c *ElasticsearchClient) ExistsIndex(ctx context.Context, index string) (bool, error)

ExistsIndex 检查索引是否存在

func (*ElasticsearchClient) Get

func (c *ElasticsearchClient) Get(ctx context.Context, index string, documentID string) (map[string]interface{}, error)

Get 获取文档(自动处理追踪)

func (*ElasticsearchClient) GetClient

func (c *ElasticsearchClient) GetClient() *elasticsearch.Client

GetClient 获取原生客户端(用于高级操作)

func (*ElasticsearchClient) Index

func (c *ElasticsearchClient) Index(ctx context.Context, index string, documentID string, body interface{}) error

Index 索引文档(自动处理追踪)

func (*ElasticsearchClient) IsConnected

func (c *ElasticsearchClient) IsConnected() bool

IsConnected 检查连接是否正常

func (*ElasticsearchClient) Ping

Ping 检查连接是否正常

func (*ElasticsearchClient) Search

func (c *ElasticsearchClient) Search(ctx context.Context, index string, query map[string]interface{}) (map[string]interface{}, error)

Search 搜索文档(自动处理追踪)

func (*ElasticsearchClient) Update

func (c *ElasticsearchClient) Update(ctx context.Context, index string, documentID string, body interface{}) error

Update 更新文档

func (*ElasticsearchClient) UpdateByQuery

func (c *ElasticsearchClient) UpdateByQuery(ctx context.Context, index string, query map[string]interface{}, script map[string]interface{}) (map[string]interface{}, error)

UpdateByQuery 根据查询更新文档

type Options

type Options struct {
	Addresses    []string      // Elasticsearch 地址列表(如 ["http://localhost:9200"])
	Username     string        // 用户名(可选)
	Password     string        // 密码(可选)
	CloudID      string        // Elastic Cloud ID(可选)
	APIKey       string        // API Key(可选)
	EnableTLS    bool          // 是否启用 TLS
	CACert       string        // CA 证书路径(可选)
	DialTimeout  time.Duration // 连接超时
	ReadTimeout  time.Duration // 读取超时
	WriteTimeout time.Duration // 写入超时
	MaxRetries   int           // 最大重试次数
	EnableTrace  bool          // 是否启用查询追踪,用于记录查询执行时间
}

Options 结构体定义了 Elasticsearch 连接器的配置选项(内部使用)

Jump to

Keyboard shortcuts

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