es

package
v1.4.13 Latest Latest
Warning

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

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

Documentation

Overview

config.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplicationPrivileges

type ApplicationPrivileges struct {
	Application string   `json:"application"`
	Privileges  []string `json:"privileges"`
	Resources   []string `json:"resources,omitempty"`
}

ApplicationPrivileges 应用权限

type Bulk

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

Bulk 批量操作接口

func (*Bulk) BulkCreate

func (b *Bulk) BulkCreate(ctx context.Context, index string, docs []map[string]interface{}) error

BulkCreate 批量创建文档

func (*Bulk) BulkDelete

func (b *Bulk) BulkDelete(ctx context.Context, index string, ids []string) error

BulkDelete 批量删除文档

func (*Bulk) BulkExecute

func (b *Bulk) BulkExecute(ctx context.Context, operations []BulkOperation) error

BulkExecute 执行批量操作

func (*Bulk) BulkIndex

func (b *Bulk) BulkIndex(ctx context.Context, index string, docs []map[string]interface{}) error

BulkIndex 批量索引文档

func (*Bulk) BulkUpdate

func (b *Bulk) BulkUpdate(ctx context.Context, index string, docs []map[string]interface{}) error

BulkUpdate 批量更新文档

func (*Bulk) MixedBulkExecute

func (b *Bulk) MixedBulkExecute(ctx context.Context, indexOperations map[string][]BulkOperation) error

MixedBulkExecute 混合批量操作

type BulkOperation

type BulkOperation struct {
	Index   string      `json:"_index"`
	ID      string      `json:"_id,omitempty"`
	Action  string      `json:"-"` // index, create, update, delete
	Payload interface{} `json:"-"`
}

BulkOperation 批量操作项

type Client

type Client struct {
	*elasticsearch.Client
	// contains filtered or unexported fields
}

Client ES客户端封装

func NewClient

func NewClient(opts ...ESOptions) (*Client, error)

NewClient 创建ES客户端,启用连接池

func (*Client) Bulk

func (c *Client) Bulk() *Bulk

Bulk NewBulk 创建批量操作实例

func (*Client) ChangeUserPassword

func (c *Client) ChangeUserPassword(ctx context.Context, username string, password string) error

ChangeUserPassword 修改用户密码

func (*Client) CreateRole

func (c *Client) CreateRole(ctx context.Context, roleName string, role Role) error

CreateRole 创建角色

func (*Client) CreateUser

func (c *Client) CreateUser(ctx context.Context, username string, user User) error

CreateUser 创建用户

func (*Client) DeleteRole

func (c *Client) DeleteRole(ctx context.Context, roleName string) error

DeleteRole 删除角色

func (*Client) DeleteUser

func (c *Client) DeleteUser(ctx context.Context, username string) error

DeleteUser 删除用户

func (*Client) Document

func (c *Client) Document() *Document

NewDocument 创建文档操作实例

func (*Client) GetRole

func (c *Client) GetRole(ctx context.Context, roleName string) (*Role, error)

GetRole 获取角色信息

func (*Client) GetUser

func (c *Client) GetUser(ctx context.Context, username string) (*User, error)

GetUser 获取用户信息

func (*Client) HealthCheck

func (c *Client) HealthCheck(ctx context.Context) (string, error)

HealthCheck 检查集群健康状态

func (*Client) Info

func (c *Client) Info(ctx context.Context) (map[string]interface{}, error)

Info 获取ES集群信息

func (*Client) NewSearch

func (c *Client) NewSearch() SearchService

NewSearch 创建搜索操作实例

func (*Client) Ping

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

Ping 检查ES服务状态

func (*Client) Search

func (c *Client) Search() SearchService

Search 返回搜索操作实例

func (*Client) UpdateRole

func (c *Client) UpdateRole(ctx context.Context, roleName string, role Role) error

UpdateRole 更新角色

func (*Client) UpdateUser

func (c *Client) UpdateUser(ctx context.Context, username string, user User) error

UpdateUser 更新用户

type Config

type Config struct {
	Addresses []string      `json:"addresses"`
	Username  string        `json:"username"`
	Password  string        `json:"password"`
	APIKey    string        `json:"api_key"`
	Timeout   time.Duration `json:"timeout"`

	// 连接池配置
	MaxIdleConns          int           `json:"max_idle_conns"`
	MaxIdleConnsPerHost   int           `json:"max_idle_conns_per_host"`
	MaxConnsPerHost       int           `json:"max_conns_per_host"`
	IdleConnTimeout       time.Duration `json:"idle_conn_timeout"`
	TLSHandshakeTimeout   time.Duration `json:"tls_handshake_timeout"`
	ExpectContinueTimeout time.Duration `json:"expect_continue_timeout"`
	ResponseHeaderTimeout time.Duration `json:"response_header_timeout"`
	ConnectionTimeout     time.Duration `json:"connection_timeout"`
	ConnectionKeepAlive   time.Duration `json:"connection_keep_alive"`

	// 重试配置
	RetryOnStatus []int         `json:"retry_on_status"`
	MaxRetries    int           `json:"max_retries"`
	RetryBackoff  time.Duration `json:"retry_backoff"` // 基础重试间隔
}

Config Elasticsearch配置

func GetDefaultConfig

func GetDefaultConfig() Config

GetDefaultConfig 返回默认配置

func (Config) Validate

func (c Config) Validate() error

Validate 验证配置

type Document

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

Document 文档操作接口

func (*Document) BulkCreate

func (d *Document) BulkCreate(ctx context.Context, index string, docs []map[string]interface{}) error

BulkCreate 批量创建文档

func (*Document) BulkDelete

func (d *Document) BulkDelete(ctx context.Context, index string, ids []string) error

BulkDelete 批量删除文档

func (*Document) BulkIndex

func (d *Document) BulkIndex(ctx context.Context, index string, docs []map[string]interface{}) error

BulkIndex 添加批量操作示例

func (*Document) BulkUpdate

func (d *Document) BulkUpdate(ctx context.Context, index string, updates []map[string]interface{}) error

BulkUpdate 批量更新文档

func (*Document) CreateIndex

func (d *Document) CreateIndex(ctx context.Context, index string, mapping interface{}) error

CreateIndex 创建索引

func (*Document) Delete

func (d *Document) Delete(ctx context.Context, index string, docID string) error

Delete 删除文档

func (*Document) DeleteIndex

func (d *Document) DeleteIndex(ctx context.Context, index string) error

DeleteIndex 删除索引

func (*Document) Get

func (d *Document) Get(ctx context.Context, index string, docID string, result interface{}) error

Get 获取文档

func (*Document) Index

func (d *Document) Index(ctx context.Context, index string, doc interface{}, docID string) error

Index 索引文档

func (*Document) IndexExists

func (d *Document) IndexExists(ctx context.Context, index string) (bool, error)

IndexExists 检查索引是否存在

func (*Document) Update

func (d *Document) Update(ctx context.Context, index string, docID string, updateData interface{}) error

Update 添加更新文档功能

type ESOptions

type ESOptions func(*esOptions)

ESOptions ES客户端选项配置

func WithConfig

func WithConfig(config Config) ESOptions

WithConfig 设置ES配置

func WithLogger

func WithLogger(logger *zap.Logger) ESOptions

WithLogger 设置日志记录器

type FieldSecurity

type FieldSecurity struct {
	Grant  []string `json:"grant,omitempty"`
	Except []string `json:"except,omitempty"`
}

FieldSecurity 字段安全设置

type PaginatedResult

type PaginatedResult struct {
	SearchResult
	Pagination PaginationResult `json:"pagination"`
}

PaginatedResult 分页结果

type PaginatedSearchRequest

type PaginatedSearchRequest struct {
	Query      interface{} `json:"query,omitempty"`
	Pagination Pagination  `json:"pagination,omitempty"`
	Sort       interface{} `json:"sort,omitempty"`
	Source     interface{} `json:"_source,omitempty"`
}

PaginatedSearchRequest 支持分页的搜索请求

type Pagination

type Pagination struct {
	Page     int `json:"page"`      // 页码,从1开始
	PageSize int `json:"page_size"` // 每页大小
}

Pagination 分页信息

type PaginationResult

type PaginationResult struct {
	Page       int `json:"page"`        // 当前页码
	PageSize   int `json:"page_size"`   // 每页大小
	Total      int `json:"total"`       // 总记录数
	TotalPages int `json:"total_pages"` // 总页数
}

PaginationResult 分页结果信息

type Role

type Role struct {
	Cluster           []string                 `json:"cluster,omitempty"`
	Indices           []RoleIndicesPermissions `json:"indices,omitempty"`
	Applications      []ApplicationPrivileges  `json:"applications,omitempty"`
	Global            interface{}              `json:"global,omitempty"`
	Metadata          map[string]interface{}   `json:"metadata,omitempty"`
	TransientMetadata map[string]interface{}   `json:"transient_metadata,omitempty"`
}

Role 角色结构

type RoleIndicesPermissions

type RoleIndicesPermissions struct {
	Names                  []string       `json:"names"`
	Privileges             []string       `json:"privileges"`
	FieldSecurity          *FieldSecurity `json:"field_security,omitempty"`
	Query                  *string        `json:"query,omitempty"`
	AllowRestrictedIndices *bool          `json:"allow_restricted_indices,omitempty"`
}

RoleIndicesPermissions 索引权限

type ScrollSearchResult

type ScrollSearchResult struct {
	SearchResult
	ScrollID string `json:"_scroll_id,omitempty"`
}

ScrollSearchResult Scroll搜索结果

type SearchRequest

type SearchRequest struct {
	Query  interface{} `json:"query,omitempty"`
	Size   int         `json:"size,omitempty"`
	From   int         `json:"from,omitempty"`
	Sort   interface{} `json:"sort,omitempty"`
	Source interface{} `json:"_source,omitempty"`
}

SearchRequest 搜索请求

type SearchRequestWithSearchAfter

type SearchRequestWithSearchAfter struct {
	SearchRequest
	SearchAfter []interface{} `json:"search_after,omitempty"`
}

SearchRequestWithSearchAfter 带search_after的搜索请求

type SearchResult

type SearchResult struct {
	Took     int  `json:"took"`
	TimedOut bool `json:"timed_out"`
	Hits     struct {
		Total struct {
			Value    int    `json:"value"`
			Relation string `json:"relation"`
		} `json:"total"`
		Hits []struct {
			Index  string          `json:"_index"`
			ID     string          `json:"_id"`
			Score  float64         `json:"_score"`
			Source json.RawMessage `json:"_source"`
		} `json:"hits"`
	} `json:"hits"`
}

SearchResult 搜索结果

type SearchService

type SearchService interface {
	Search(ctx context.Context, index string, req SearchRequest) (*SearchResult, error)
	SearchWithRawQuery(ctx context.Context, index string, query []byte) (*SearchResult, error)
	SearchWithPagination(ctx context.Context, index string, req PaginatedSearchRequest) (*PaginatedResult, error)
	ScrollSearch(ctx context.Context, index string, req SearchRequest, scrollTime time.Duration) (*ScrollSearchResult, error)
	ScrollContinue(ctx context.Context, scrollID string, scrollTime time.Duration) (*ScrollSearchResult, error)
	ScrollClear(ctx context.Context, scrollIDs []string) error
	SearchWithSearchAfter(ctx context.Context, index string, req SearchRequestWithSearchAfter) (*SearchResult, error)
}

SearchService 搜索服务接口

type User

type User struct {
	Username     string                 `json:"username,omitempty"`
	FullName     string                 `json:"full_name,omitempty"`
	Email        string                 `json:"email,omitempty"`
	Roles        []string               `json:"roles"`
	Password     string                 `json:"password,omitempty"`
	PasswordHash string                 `json:"password_hash,omitempty"`
	Metadata     map[string]interface{} `json:"metadata,omitempty"`
	Enabled      bool                   `json:"enabled"`
}

User Elasticsearch 用户结构

Directories

Path Synopsis
demo
document command
security command

Jump to

Keyboard shortcuts

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