|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/serverless/current/elasticsearch-go-client-getting-started.html |
| 4 | +navigation_title: Getting started in {{serverless-short}} |
| 5 | +--- |
| 6 | + |
| 7 | +# Getting started with {{es}} Go in {{serverless-full}}[elasticsearch-go-client-getting-started] |
| 8 | + |
| 9 | +This page guides you through the installation process of the {{es}} Go client, shows you how to initialize the client, and how to perform basic {{es}} operations with it. |
| 10 | + |
| 11 | + |
| 12 | +## Requirements [elasticsearch-go-client-getting-started-requirements] |
| 13 | + |
| 14 | +* Go 1.22 or higher installed on your system. |
| 15 | + |
| 16 | + |
| 17 | +## Installation [elasticsearch-go-client-getting-started-installation] |
| 18 | + |
| 19 | + |
| 20 | +### Using the command line [elasticsearch-go-client-getting-started-using-the-command-line] |
| 21 | + |
| 22 | +You can install the Go client with the following commands: |
| 23 | + |
| 24 | +```bash |
| 25 | +go get -u github.com/elastic/go-elasticsearch/v9@latest |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Imports [elasticsearch-go-client-getting-started-imports] |
| 30 | + |
| 31 | +The following snippets use these imports: |
| 32 | + |
| 33 | +```go |
| 34 | +import ( |
| 35 | + "context" |
| 36 | + "encoding/json" |
| 37 | + "fmt" |
| 38 | + "log" |
| 39 | + "strconv" |
| 40 | + |
| 41 | + "github.com/elastic/go-elasticsearch/v9" |
| 42 | + "github.com/elastic/go-elasticsearch/v9/typedapi/types" |
| 43 | + "github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/result" |
| 44 | +) |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +## Initialize the client [elasticsearch-go-client-getting-started-initialize-the-client] |
| 49 | + |
| 50 | +Initialize the client using your API key and {{es}} endpoint: |
| 51 | + |
| 52 | +```go |
| 53 | +client, err := elasticsearch.NewTypedClient(elasticsearch.Config{ |
| 54 | + Addresses: []string{"https://my-project-url"}, |
| 55 | + APIKey: "your-api-key", |
| 56 | +}) |
| 57 | +if err != nil { |
| 58 | + log.Fatalf("Error creating the client: %s", err) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +To get API keys for the {{es}} endpoint for a project, see [Get started](docs-content://solutions/search/get-started.md). |
| 63 | + |
| 64 | + |
| 65 | +## Using the API [elasticsearch-go-client-getting-started-using-the-api] |
| 66 | + |
| 67 | +After you’ve initialized the client, you can start ingesting documents. You can use the `bulk` API for this. This API enables you to index, update, and delete several documents in one request. |
| 68 | + |
| 69 | + |
| 70 | +### Creating an index and ingesting documents [elasticsearch-go-client-getting-started-creating-an-index-and-ingesting-documents] |
| 71 | + |
| 72 | +You can call the `bulk` API with a body parameter, an array of hashes that define the action, and a document. |
| 73 | + |
| 74 | +The following is an example of indexing some classic books into the `books` index: |
| 75 | + |
| 76 | +```go |
| 77 | +type Book struct { |
| 78 | + Name string `json:"name"` |
| 79 | + Author string `json:"author"` |
| 80 | + ReleaseDate string `json:"release_date"` |
| 81 | + PageCount int `json:"page_count"` |
| 82 | +} |
| 83 | + |
| 84 | +books := []Book{ |
| 85 | + {Name: "Snow Crash", Author: "Neal Stephenson", ReleaseDate: "1992-06-01", PageCount: 470}, |
| 86 | + {Name: "Revelation Space", Author: "Alastair Reynolds", ReleaseDate: "2000-03-15", PageCount: 585}, |
| 87 | + {Name: "1984", Author: "George Orwell", ReleaseDate: "1949-06-08", PageCount: 328}, |
| 88 | + {Name: "Fahrenheit 451", Author: "Ray Bradbury", ReleaseDate: "1953-10-15", PageCount: 227}, |
| 89 | + {Name: "Brave New World", Author: "Aldous Huxley", ReleaseDate: "1932-06-01", PageCount: 268}, |
| 90 | + {Name: "The Handmaid's Tale", Author: "Margaret Atwood", ReleaseDate: "1985-06-01", PageCount: 311}, |
| 91 | +} |
| 92 | +indexName := "books" |
| 93 | + |
| 94 | +bulk := client.Bulk() |
| 95 | +for i, book := range books { |
| 96 | + id := strconv.Itoa(i) |
| 97 | + err := bulk.CreateOp(types.CreateOperation{Index_: &indexName, Id_: &id}, book) |
| 98 | + if err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | +} |
| 102 | +bulkRes, err := bulk.Do(context.TODO()) |
| 103 | +if err != nil { |
| 104 | + log.Fatal(err) |
| 105 | +} |
| 106 | + |
| 107 | +fmt.Printf("Bulk: %#v\n", bulkRes.Items) |
| 108 | +``` |
| 109 | + |
| 110 | +When you use the client to make a request to {{es-serverless}}, it returns an API response object. You can access the body values directly as seen on the previous example with `bulkRes`. |
| 111 | + |
| 112 | + |
| 113 | +### Getting documents [elasticsearch-go-client-getting-started-getting-documents] |
| 114 | + |
| 115 | +You can get documents by using the following code: |
| 116 | + |
| 117 | +```go |
| 118 | +getRes, err := client.Get(indexName, "5").Do(context.TODO()) |
| 119 | +if err != nil { |
| 120 | + log.Fatal(err) |
| 121 | +} |
| 122 | +book := Book{} |
| 123 | +if err := json.Unmarshal(getRes.Source_, &book); err != nil { |
| 124 | + log.Fatal(err) |
| 125 | +} |
| 126 | +fmt.Printf("Get book: %#v\n", book) |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +### Searching [elasticsearch-go-client-getting-started-searching] |
| 131 | + |
| 132 | +Now that some data is available, you can search your documents using the `search` API: |
| 133 | + |
| 134 | +```go |
| 135 | +searchRes, err := client.Search(). |
| 136 | + Index("books"). |
| 137 | + Q("snow"). |
| 138 | + Do(context.TODO()) |
| 139 | +if err != nil { |
| 140 | + log.Fatal(err) |
| 141 | +} |
| 142 | + |
| 143 | +bookSearch := []Book{} |
| 144 | +for _, hit := range searchRes.Hits.Hits { |
| 145 | + book := Book{} |
| 146 | + if err := json.Unmarshal(hit.Source_, &book); err != nil { |
| 147 | + log.Fatal(err) |
| 148 | + } |
| 149 | + bookSearch = append(bookSearch, book) |
| 150 | +} |
| 151 | +fmt.Printf("Search books: %#v\n", bookSearch) |
| 152 | +``` |
| 153 | + |
| 154 | + |
| 155 | +### Updating a document [elasticsearch-go-client-getting-started-updating-a-document] |
| 156 | + |
| 157 | +You can call the `Update` API to update a document, in this example updating the `page_count` for "The Handmaid’s Tale" with id "5": |
| 158 | + |
| 159 | +```go |
| 160 | +updateRes, err := client.Update("books", "5"). |
| 161 | + Doc( |
| 162 | + struct { |
| 163 | + PageCount int `json:"page_count"` |
| 164 | + }{PageCount: 312}, |
| 165 | + ). |
| 166 | + Do(context.TODO()) |
| 167 | +if err != nil { |
| 168 | + log.Fatal(err) |
| 169 | +} |
| 170 | + |
| 171 | +if updateRes.Result == result.Updated { |
| 172 | + fmt.Printf("Update book: %#v\n", updateRes) |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | + |
| 177 | +### Deleting a document [elasticsearch-go-client-getting-started-deleting-a-document] |
| 178 | + |
| 179 | +You can call the `Delete` API to delete a document: |
| 180 | + |
| 181 | +```go |
| 182 | +deleteRes, err := client.Delete("books", "5").Do(context.TODO()) |
| 183 | +if err != nil { |
| 184 | + log.Fatal(err) |
| 185 | +} |
| 186 | + |
| 187 | +if deleteRes.Result == result.Deleted { |
| 188 | + fmt.Printf("Delete book: %#v\n", deleteRes) |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | + |
| 193 | +### Deleting an index [elasticsearch-go-client-getting-started-deleting-an-index] |
| 194 | + |
| 195 | +```go |
| 196 | +indexDeleteRes, err := client.Indices.Delete("books").Do(context.TODO()) |
| 197 | +if err != nil { |
| 198 | + log.Fatal(err) |
| 199 | +} |
| 200 | + |
| 201 | +if indexDeleteRes.Acknowledged { |
| 202 | + fmt.Printf("Delete index: %#v\n", indexDeleteRes) |
| 203 | +} |
| 204 | +``` |
| 205 | + |
0 commit comments