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. You can use the client with either a low-level API or a fully typed API. This getting started shows you examples of both APIs.

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.

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint for the low level API:

client, err := elasticsearch.NewClient(elasticsearch.Config{
    CloudID: "<CloudID>",
    APIKey: "<ApiKey>",
})
		
  1. Your deployment's Cloud ID, found on the My deployment page.
  2. Your API key for authentication.

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint for the fully-typed API:

typedClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{
    CloudID: "<CloudID>",
    APIKey:  "<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

    This is how you create the my_index index with the low level API:

    res, err := client.Indices.Create("my_index")
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		

    This is how you create the my_index index with the fully-typed API:

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

    This is a simple way of indexing a document by using the low-level API:

    document := struct {
        Name string `json:"name"`
    }{
        "go-elasticsearch",
    }
    data, _ := json.Marshal(document)
    res, err := client.Index("my_index", bytes.NewReader(data))
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		

    This is a simple way of indexing a document by using the fully-typed API:

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

    You can get documents by using the following code with the low-level API:

    res, err := client.Get("my_index", "id")
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		

    This is how you can get documents by using the fully-typed API:

    typedClient.Get("my_index", "id").Do(context.TODO())
    		
  4. Search documents

    This is how you can create a single match query with the low-level API:

    query := `{ "query": { "match_all": {} } }`
    res, err := client.Search(
        client.Search.WithIndex("my_index"),
        client.Search.WithBody(strings.NewReader(query)),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		

    This is how you can perform a single match query with the fully-typed API:

    typedClient.Search().
        Index("my_index").
        Request(&search.Request{
            Query: &types.Query{MatchAll: &types.MatchAllQuery{}},
        }).
        Do(context.TODO())
    		

    The esdsl package provides fluent builders for queries, aggregations, and mappings:

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

    This is how you can update a document, for example to add a new field, by using the low-level API:

    res, err := client.Update("my_index", "id", strings.NewReader(`{"doc": {"language": "Go"}}`))
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		

    This is how you can update a document with the fully-typed API:

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

    res, err := client.Delete("my_index", "id")
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		
    typedClient.Delete("my_index", "id").Do(context.TODO())
    		
  7. Delete an index

    res, err := client.Indices.Delete([]string{"my_index"})
    if err != nil {
        log.Fatal(err)
    }
    defer res.Body.Close()
    		
    typedClient.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.