es

package
v1.52.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package es 提供 ElasticSearch 集成(对标 Spring Data Elasticsearch 的 ElasticsearchOperations/Template):常用操作封装 + 原生客户端暴露。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetGlobal added in v1.36.0

func SetGlobal(client *Client)

SetGlobal 设置全局客户端(NewClient 成功后自动调用)。

Types

type Client

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

Client 是 ES 模板实现。

func Get added in v1.36.0

func Get() *Client

Get 获取全局 ES 客户端;未初始化时返回 nil。

func NewClient

func NewClient(config Config) (*Client, error)

NewClient 创建 ES 客户端并验证连通性。

func (*Client) Client

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

Client 返回原生客户端。

func (*Client) CreateIndex

func (c *Client) CreateIndex(ctx context.Context, index string, settingsJSON []byte) (bool, error)

CreateIndex 创建索引;已存在返回 (false, nil)。

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, index, id string) error

Delete 删除文档。

func (*Client) ExistsIndex

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

ExistsIndex 索引是否存在。

func (*Client) Get

func (c *Client) Get(ctx context.Context, index, id string) ([]byte, error)

Get 按 ID 获取文档原始 JSON。

func (*Client) Index

func (c *Client) Index(ctx context.Context, index, id string, document interface{}) error

Index 写入文档。

func (*Client) IndexWithRefresh

func (c *Client) IndexWithRefresh(ctx context.Context, index, id string, document interface{}) error

IndexWithRefresh 写入并强制 refresh。

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping 健康检查。

func (*Client) Search

func (c *Client) Search(ctx context.Context, index string, query []byte) (*SearchResponse, error)

Search 执行查询。

type Config

type Config struct {
	// Addresses 节点地址列表(如 http://127.0.0.1:9200)。
	Addresses []string
	// Username/Password 认证(可选)。
	Username string
	Password string
	// Timeout 请求超时(默认 10s)。
	Timeout time.Duration
	// CloudID 弹性云 ID(可选,优先于 Addresses)。
	CloudID string
	// APIKey API Key 认证(可选)。
	APIKey string
}

Config ES 客户端配置。

type EsTemplate

type EsTemplate interface {
	// Ping 健康检查。
	Ping(ctx context.Context) error
	// Index 写入文档(指定 ID;存在即覆盖)。
	Index(ctx context.Context, index, id string, document interface{}) error
	// IndexWithRefresh 写入文档并强制 refresh(测试/低延迟场景)。
	IndexWithRefresh(ctx context.Context, index, id string, document interface{}) error
	// Get 按 ID 获取文档原始 JSON。
	Get(ctx context.Context, index, id string) ([]byte, error)
	// Delete 按 ID 删除文档。
	Delete(ctx context.Context, index, id string) error
	// Search 执行查询(DSL 原始字节);返回解析后的搜索响应。
	Search(ctx context.Context, index string, query []byte) (*SearchResponse, error)
	// ExistsIndex 判断索引是否存在。
	ExistsIndex(ctx context.Context, index string) (bool, error)
	// CreateIndex 创建索引(带 settings/mappings JSON;已存在时返回 false 不报错)。
	CreateIndex(ctx context.Context, index string, settingsJSON []byte) (bool, error)
	// Client 返回底层原生客户端(高级操作入口)。
	Client() *elasticsearch.Client
}

EsTemplate 高频操作层:索引/查询/获取/删除/批量 + 原生客户端。

type Hit

type Hit struct {
	Index  string          `json:"_index"`
	ID     string          `json:"_id"`
	Score  float64         `json:"_score"`
	Source json.RawMessage `json:"_source"`
}

Hit 单条命中。

type SearchResponse

type SearchResponse struct {
	Took     int  `json:"took"`
	TimedOut bool `json:"timed_out"`
	Hits     struct {
		Total struct {
			Value int64 `json:"value"`
		} `json:"total"`
		Hits []Hit `json:"hits"`
	} `json:"hits"`
}

SearchResponse 搜索响应(常用字段)。

Jump to

Keyboard shortcuts

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