v7

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

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

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

func (c *Client) Delete(index, documentID string) error

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

func (c *Client) DeleteByQuery(indices []string, body string) error

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

func (c *Client) Exists(index, documentID string) (bool, error)

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

func (c *Client) Index(index, documentID, body string) error

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

func (c *Client) IndicesCreate(index, body string) error

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

func (c *Client) IndicesDelete(indices []string) error

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

func (c *Client) IndicesDeleteTemplate(name string) error

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

func (c *Client) IndicesExists(indices []string) (bool, error)

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

func (c *Client) IndicesExistsTemplate(name []string) (bool, error)

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

func (c *Client) IndicesForcemerge(indices []string) error

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

func (c *Client) IndicesPutTemplate(name, body string) error

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

func (c *Client) Search(index, body string) (string, error)

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
}`)

Jump to

Keyboard shortcuts

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