Documentation
¶
Overview ¶
Package es 提供 ElasticSearch 集成(对标 Spring Data Elasticsearch 的 ElasticsearchOperations/Template):常用操作封装 + 原生客户端暴露。
Index ¶
- func SetGlobal(client *Client)
- type Client
- func (c *Client) Client() *elasticsearch.Client
- func (c *Client) CreateIndex(ctx context.Context, index string, settingsJSON []byte) (bool, error)
- func (c *Client) Delete(ctx context.Context, index, id string) error
- func (c *Client) ExistsIndex(ctx context.Context, index string) (bool, error)
- func (c *Client) Get(ctx context.Context, index, id string) ([]byte, error)
- func (c *Client) Index(ctx context.Context, index, id string, document interface{}) error
- func (c *Client) IndexWithRefresh(ctx context.Context, index, id string, document interface{}) error
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Search(ctx context.Context, index string, query []byte) (*SearchResponse, error)
- type Config
- type EsTemplate
- type Hit
- type SearchResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是 ES 模板实现。
func (*Client) CreateIndex ¶
CreateIndex 创建索引;已存在返回 (false, nil)。
func (*Client) ExistsIndex ¶
ExistsIndex 索引是否存在。
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 高频操作层:索引/查询/获取/删除/批量 + 原生客户端。
Click to show internal directories.
Click to hide internal directories.