Documentation
¶
Overview ¶
Package v7 provides Elasticsearch version 7 client implementation.
This package wraps the official Elasticsearch 7.x Go client, providing a simplified interface for common operations while maintaining compatibility with the ClientInterface.
Features:
- Full Elasticsearch 7.x API support
- Document operations (index, exists, delete, delete by query)
- Index management (create, delete, exists, force merge)
- Template management (put, delete, exists)
- Search operations with JSON responses
- Certificate fingerprint authentication
- Cloud ID support
Example:
client := &v7.Client{}
err := client.Initialize([]string{"https://localhost:9200"}, 30*time.Second, "", "", "elastic", "password", "", nil)
err = client.Index("products", "1", `{"name":"Product 1","price":29.99}`)
Index ¶
- type Client
- func (c *Client) Delete(index, documentID string) error
- func (c *Client) DeleteByQuery(indices []string, body string) error
- func (c *Client) Exists(index, documentID string) (bool, error)
- func (c *Client) Index(index, documentID, body string) error
- func (c *Client) IndicesCreate(index, body string) error
- func (c *Client) IndicesDelete(indices []string) error
- func (c *Client) IndicesDeleteTemplate(name string) error
- func (c *Client) IndicesExists(indices []string) (bool, error)
- func (c *Client) IndicesExistsTemplate(name []string) (bool, error)
- func (c *Client) IndicesForcemerge(indices []string) error
- func (c *Client) IndicesPutTemplate(name, body string) error
- func (c *Client) Initialize(addresses []string, timeout time.Duration, ...) error
- func (c *Client) Search(index, body string) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a struct that provides client related methods.
func (*Client) Delete ¶
Delete deletes a document from the specified index.
Parameters:
- index: The name of the index
- documentID: The document identifier to delete
Returns error if client is not initialized or deletion fails.
The deletion is immediately visible (refresh=true).
Example:
err := client.Delete("products", "product-123")
func (*Client) DeleteByQuery ¶
DeleteByQuery deletes all documents matching a query.
Parameters:
- indices: List of index names to search
- body: JSON query body as a string
Returns error if client is not initialized or deletion fails.
The deletions are immediately visible (refresh=true).
Example:
err := client.DeleteByQuery(
[]string{"products"},
`{"query": {"range": {"price": {"lt": 10}}}}`
)
func (*Client) Exists ¶
Exists checks if a document exists in the specified index.
Parameters:
- index: The name of the index
- documentID: The document identifier
Returns true if the document exists, false if not found. Returns error if client is not initialized or request fails.
The request uses refresh=true to ensure real-time visibility.
Example:
exists, err := client.Exists("products", "product-123")
if exists {
fmt.Println("Document found")
}
func (*Client) Index ¶
Index indexes a document in the specified index.
Parameters:
- index: The name of the index
- documentID: The document identifier (use empty string for auto-generated ID)
- body: JSON document body as a string
Returns error if client is not initialized or indexing fails.
The document is immediately available for search (refresh=true).
Example:
err := client.Index("products", "1", `{
"name": "Laptop",
"price": 999.99
}`)
func (*Client) IndicesCreate ¶
IndicesCreate creates an index with optional settings and mappings.
Parameters:
- index: The name of the index to create
- body: JSON configuration with settings and mappings
Returns error if client is not initialized, index already exists, or creation fails.
Example:
err := client.IndicesCreate("products", `{
"settings": {"number_of_shards": 1},
"mappings": {
"properties": {
"name": {"type": "text"},
"price": {"type": "float"}
}
}
}`)
func (*Client) IndicesDelete ¶
IndicesDelete deletes one or more indices.
Parameters:
- indices: List of index names to delete
Returns error if client is not initialized or deletion fails.
Warning: This permanently deletes all data in the specified indices.
Example:
err := client.IndicesDelete([]string{"old-logs-2023", "old-logs-2022"})
func (*Client) IndicesDeleteTemplate ¶
IndicesDeleteTemplate deletes an index template.
Parameters:
- name: The template name to delete
Returns error if client is not initialized or deletion fails.
Note: Deleting a template does not affect existing indices created from it.
Example:
err := client.IndicesDeleteTemplate("old_logs_template")
func (*Client) IndicesExists ¶
IndicesExists checks if one or more indices exist.
Parameters:
- indices: List of index names to check
Returns true if all specified indices exist, false otherwise. Returns error if client is not initialized or request fails.
Example:
exists, err := client.IndicesExists([]string{"products", "orders"})
if !exists {
// Create indices
}
func (*Client) IndicesExistsTemplate ¶
IndicesExistsTemplate checks if one or more index templates exist.
Parameters:
- name: List of template names to check
Returns true if all specified templates exist, false otherwise. Returns error if client is not initialized or request fails.
Example:
exists, err := client.IndicesExistsTemplate([]string{"logs_template"})
if !exists {
// Create template
}
func (*Client) IndicesForcemerge ¶
IndicesForcemerge performs a force merge operation on indices.
Parameters:
- indices: List of index names to force merge
Returns error if client is not initialized or operation fails.
Force merge optimizes index storage by merging segments and removing deleted documents. This operation is I/O intensive and should be used carefully on production systems.
Example:
// Optimize old read-only indices
err := client.IndicesForcemerge([]string{"logs-2023-12"})
func (*Client) IndicesPutTemplate ¶
IndicesPutTemplate creates or updates an index template.
Parameters:
- name: The template name
- body: JSON template configuration with index patterns, settings, and mappings
Returns error if client is not initialized or operation fails.
Templates automatically apply settings and mappings to matching indices.
Example:
err := client.IndicesPutTemplate("logs_template", `{
"index_patterns": ["logs-*"],
"settings": {"number_of_shards": 1},
"mappings": {
"properties": {
"timestamp": {"type": "date"}
}
}
}`)
func (*Client) Initialize ¶
func (c *Client) Initialize(addresses []string, timeout time.Duration, cloudID, apiKey, username, password, certificateFingerprint string, caCert []byte) error
Initialize initializes the Elasticsearch v7 client with connection configuration.
Parameters:
- addresses: Elasticsearch node URLs (e.g., []string{"http://localhost:9200"})
- timeout: HTTP response timeout in seconds
- cloudID: Elastic Cloud deployment ID (optional, empty string if not used)
- apiKey: Base64-encoded API key for authentication (optional)
- username: Username for basic authentication (optional)
- password: Password for basic authentication (optional)
- certificateFingerprint: Server certificate fingerprint for HTTPS (optional)
- caCert: CA certificate bytes for HTTPS verification (optional)
Returns error if client creation fails.
The client is protected by a mutex during initialization to prevent data races in the underlying elastictransport library.
Example:
err := client.Initialize(
[]string{"http://localhost:9200"},
30*time.Second,
"", "", "elastic", "password", "", nil,
)
func (*Client) Search ¶
Search executes a search query and returns the raw JSON response.
Parameters:
- index: The index name to search
- body: JSON search query body
Returns the search results as a JSON string. Returns error if client is not initialized or search fails.
The response includes hits, aggregations, and metadata. Parse the JSON string to extract specific results.
Example:
result, err := client.Search("products", `{
"query": {
"match": {"category": "electronics"}
},
"size": 10
}`)