Loading

Getting started

This page guides you through the installation process of the Go client, shows you how to instantiate the client, and how to perform basic Elasticsearch operations with it. The examples use the typed API with the esdsl query builders, which is the recommended way to use the client. If you need raw-JSON control or an endpoint not yet covered by the typed API, see the low-level API.

Go version 1.24+

To install the latest version of the client, run the following command:

go get github.com/elastic/go-elasticsearch/v9@latest
		

Refer to the Installation page to learn more.

Connect to Elastic Cloud using an API key and the Elasticsearch endpoint:

client, err := elasticsearch.NewTyped(
    elasticsearch.WithCloudID("<CloudID>"),
    elasticsearch.WithAPIKey("<ApiKey>"),
)
		
  1. Your deployment's Cloud ID, found on the My deployment page.
  2. Your API key for authentication.

Your Elasticsearch endpoint can be found on the My deployment page of your deployment:

Finding Elasticsearch endpoint

You can generate an API key on the Management page under Security.

Create API key

For other connection options, refer to the Connecting section.

Time to use Elasticsearch! This section walks you through the basic, and most important, operations of Elasticsearch. For more operations and more advanced examples, refer to the Using the API section.

  1. Create an index

    client.Indices.Create("my_index").Do(context.TODO())
    		
  2. Index a document

    document := struct {
        Name string `json:"name"`
    }{
        "go-elasticsearch",
    }
    client.Index("my_index").
        Id("1").
        Document(document).
        Do(context.TODO())
    		
  3. Get a document

    client.Get("my_index", "1").Do(context.TODO())
    		
  4. Search documents

    Use the esdsl builders for a concise, fluent query syntax:

    client.Search().
        Index("my_index").
        Query(esdsl.NewMatchAllQuery()).
        Do(context.TODO())
    		
  5. Update a document

    client.Update("my_index", "1").
        Request(&update.Request{
            Doc: json.RawMessage(`{"language": "Go"}`),
        }).Do(context.TODO())
    		
  6. Delete a document

    client.Delete("my_index", "1").Do(context.TODO())
    		
  7. Delete an index

    client.Indices.Delete("my_index").Do(context.TODO())
    		
  • Learn more about the Typed API, a strongly typed Go API for Elasticsearch.
  • Explore the esdsl builders for a fluent syntax to construct queries, aggregations, and mappings.
  • If you need the low-level API or are migrating from it, see the Low-level API and the migration guide.