elasticsearch

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 13 Imported by: 0

README

ElasticSearch

概念对比

ES存储结构 RDBMS存储结构
Index
Document
Field 表字段
Mapping 表结构定义

mapping

  • 动态映射(dynamic mapping)
  • 显式映射(explicit mapping)
  • 严格映射(strict mappings)

Docker部署

docker pull bitnami/elasticsearch:latest

docker run -itd \
    --name elasticsearch \
    -p 9200:9200 \
    -p 9300:9300 \
    -e ELASTICSEARCH_USERNAME=elastic \
    -e ELASTICSEARCH_PASSWORD=elastic \
    -e ELASTICSEARCH_NODE_NAME=elasticsearch-node-1 \
    -e ELASTICSEARCH_CLUSTER_NAME=elasticsearch-cluster \
    bitnami/elasticsearch:latest

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRequestFailed is returned when a request to Elasticsearch fails.
	ErrRequestFailed = errors.InternalServer("REQUEST_FAILED", "request failed")

	// ErrIndexNotFound is returned when the specified index does not exist.
	ErrIndexNotFound = errors.InternalServer("INDEX_NOT_FOUND", "index not found")

	// ErrIndexAlreadyExists is returned when trying to create an index that already exists.
	ErrIndexAlreadyExists = errors.InternalServer("INDEX_ALREADY_EXISTS", "index already exists")

	ErrCreateIndex = errors.InternalServer("CREATE_INDEX_FAILED", "failed to create index")

	ErrDeleteIndex = errors.InternalServer("DELETE_INDEX_FAILED", "failed to delete index")

	// ErrDocumentNotFound is returned when a document is not found in the index.
	ErrDocumentNotFound = errors.InternalServer("DOCUMENT_NOT_FOUND", "document not found")

	// ErrDocumentAlreadyExists is returned when trying to create a document that already exists.
	ErrDocumentAlreadyExists = errors.InternalServer("DOCUMENT_ALREADY_EXISTS", "document already exists")

	// ErrInvalidQuery is returned when the query provided to Elasticsearch is invalid.
	ErrInvalidQuery = errors.InternalServer("INVALID_QUERY", "invalid query")

	// ErrUnmarshalResponse is returned when the response from Elasticsearch cannot be unmarshalled.
	ErrUnmarshalResponse = errors.InternalServer("UNMARSHAL_RESPONSE_FAILED", "failed to unmarshal response")

	ErrInsertDocument = errors.InternalServer("INSERT_DOCUMENT_FAILED", "failed to insert document")

	ErrBatchInsertDocument = errors.InternalServer("BATCH_INSERT_DOCUMENT_FAILED", "failed to batch insert documents")

	ErrGetDocument = errors.InternalServer("GET_DOCUMENT_FAILED", "failed to get document")

	ErrSearchDocument = errors.InternalServer("SEARCH_DOCUMENT_FAILED", "failed to search document")
)

Functions

func MakeQueryString

func MakeQueryString(andQuery, orQuery string) string

func MergeOptions

func MergeOptions(mapping, settings string) (string, error)

MergeOptions 合并 Elasticsearch 索引的映射和设置

func ParseQueryString

func ParseQueryString(query string) []string

Types

type Client

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

func NewClient

func NewClient(logger log.Logger, cfg *conf.Bootstrap) (*Client, error)

func (*Client) BatchInsertDocument

func (c *Client) BatchInsertDocument(ctx context.Context, indexName string, dataSet []interface{}) error

BatchInsertDocument 批量插入数据

func (*Client) CheckConnectStatus

func (c *Client) CheckConnectStatus() bool

CheckConnectStatus 检查Elasticsearch连接

func (*Client) Close

func (c *Client) Close()

func (*Client) CreateIndex

func (c *Client) CreateIndex(ctx context.Context, indexName string, mapping, settings string) error

CreateIndex 创建一条索引

如果mapping为空("")则表示不创建模型

func (*Client) DeleteDocument

func (c *Client) DeleteDocument(ctx context.Context, indexName, id string) error

DeleteDocument 删除一条数据

func (*Client) DeleteIndex

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

DeleteIndex 删除一条索引

func (*Client) GetDocument

func (c *Client) GetDocument(
	ctx context.Context,
	indexName string,
	id string,
	sourceFields []string,
	out interface{},
) error

GetDocument 查询数据

func (*Client) IndexExists

func (c *Client) IndexExists(ctx context.Context, indexName string) (bool, error)

IndexExists 检查索引是否存在

func (*Client) InsertDocument

func (c *Client) InsertDocument(ctx context.Context, indexName, id string, data interface{}) error

InsertDocument 插入一条数据

func (*Client) Search

func (c *Client) Search(
	ctx context.Context,
	indexName string,
	req *pagination.PagingRequest,
) (*SearchResult, error)

func (*Client) UpdateDocument

func (c *Client) UpdateDocument(ctx context.Context, indexName string, pk string, doc interface{}) error

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		RootCause []struct {
			Type   string `json:"type"`
			Reason string `json:"reason"`
		} `json:"root_cause"`
		Type     string `json:"type"`
		Reason   string `json:"reason"`
		CausedBy struct {
			Type   string `json:"type"`
			Reason string `json:"reason"`
		} `json:"caused_by,omitempty"`
	} `json:"error"`
	Status int `json:"status"`
}

ErrorResponse 表示 Elasticsearch 错误响应的结构

func ParseErrorMessage

func ParseErrorMessage(body io.ReadCloser) (*ErrorResponse, error)

ParseErrorMessage 解析 Elasticsearch 错误消息

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"`
			Type   string          `json:"_type"`
			ID     string          `json:"_id"`
			Score  float64         `json:"_score"`
			Source json.RawMessage `json:"_source"`
		} `json:"hits"`
	} `json:"hits"`
}

Jump to

Keyboard shortcuts

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