|
| 1 | +[[getting-started-go]] |
| 2 | +== Getting started |
| 3 | + |
| 4 | +This page guides you through the installation process of the Go client, shows |
| 5 | +you how to instantiate the client, and how to perform basic Elasticsearch |
| 6 | +operations with it. You can use the client with either a low-level API or a |
| 7 | +fully typed API. This getting started shows you examples of both APIs. |
| 8 | + |
| 9 | +[discrete] |
| 10 | +=== Requirements |
| 11 | + |
| 12 | +Go version 1.13+ |
| 13 | + |
| 14 | +[discrete] |
| 15 | +=== Installation |
| 16 | + |
| 17 | +To install the latest version of the client, run the following command: |
| 18 | + |
| 19 | +[source,shell] |
| 20 | +-------------------------- |
| 21 | +go get github.com/elastic/go-elasticsearch/v8@latest |
| 22 | +-------------------------- |
| 23 | + |
| 24 | +Refer to the <<installation>> page to learn more. |
| 25 | + |
| 26 | + |
| 27 | +[discrete] |
| 28 | +=== Connecting |
| 29 | + |
| 30 | +You can connect to the Elastic Cloud using an API key and the Elasticsearch |
| 31 | +endpoint for the low level API: |
| 32 | + |
| 33 | +[source,go] |
| 34 | +---- |
| 35 | +client, err := elasticsearch.NewClient(elasticsearch.Config{ |
| 36 | + CloudID: "<CloudID>", |
| 37 | + APIKey: "<ApiKey>", |
| 38 | +}) |
| 39 | +---- |
| 40 | + |
| 41 | +This is the same for the fully-typed API: |
| 42 | + |
| 43 | +[source,go] |
| 44 | +---- |
| 45 | +typedClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{ |
| 46 | + CloudID: "<CloudID>", |
| 47 | + APIKey: "<ApiKey>", |
| 48 | +}) |
| 49 | +---- |
| 50 | + |
| 51 | + |
| 52 | +Your Elasticsearch endpoint can be found on the **My deployment** page of your |
| 53 | +deployment: |
| 54 | + |
| 55 | +image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] |
| 56 | + |
| 57 | +You can generate an API key on the **Management** page under Security. |
| 58 | + |
| 59 | +image::images/create-api-key.png[alt="Create API key",align="center"] |
| 60 | + |
| 61 | +For other connection options, refer to the <<connecting>> section. |
| 62 | + |
| 63 | + |
| 64 | +[discrete] |
| 65 | +=== Operations |
| 66 | + |
| 67 | +Time to use Elasticsearch! This section walks you through the basic, and most |
| 68 | +important, operations of Elasticsearch. For more operations and more advanced |
| 69 | +examples, refer to the <<examples>> page. |
| 70 | + |
| 71 | + |
| 72 | +[discrete] |
| 73 | +==== Creating an index |
| 74 | + |
| 75 | +This is how you create the `my_index` index with the low level API: |
| 76 | + |
| 77 | +[source,go] |
| 78 | +---- |
| 79 | +client.Indices.Create("my_index") |
| 80 | +---- |
| 81 | + |
| 82 | +This is how you create the `my_index` index with the fully-typed API: |
| 83 | + |
| 84 | +[source,go] |
| 85 | +---- |
| 86 | +typedClient.Indices.Create("my_index").Do(context.TODO()) |
| 87 | +---- |
| 88 | + |
| 89 | + |
| 90 | +[discrete] |
| 91 | +==== Indexing documents |
| 92 | + |
| 93 | +This is a simple way of indexing a document by using the low-level API: |
| 94 | + |
| 95 | +[source,go] |
| 96 | +---- |
| 97 | +document := struct { |
| 98 | + Name string `json:"name"` |
| 99 | +}{ |
| 100 | + "go-elasticsearch", |
| 101 | +} |
| 102 | +data, _ := json.Marshal(document) |
| 103 | +client.Index("my_index", bytes.NewReader(data)) |
| 104 | +---- |
| 105 | + |
| 106 | +The same operation by using the fully-typed API: |
| 107 | + |
| 108 | +[source,go] |
| 109 | +---- |
| 110 | +document := struct { |
| 111 | + Name string `json:"name"` |
| 112 | +}{ |
| 113 | + "go-elasticsearch", |
| 114 | +} |
| 115 | +typedClient.Index("my_index"). |
| 116 | + Id("1"). |
| 117 | + Request(document). |
| 118 | + Do(context.TODO()) |
| 119 | +---- |
| 120 | + |
| 121 | +[discrete] |
| 122 | +==== Getting documents |
| 123 | + |
| 124 | +You can get documents by using the following code with the low-level API: |
| 125 | + |
| 126 | +[source,go] |
| 127 | +---- |
| 128 | +client.Get("my_index", "id") |
| 129 | +---- |
| 130 | + |
| 131 | +This is how you can get documents by using the fully-typed API: |
| 132 | + |
| 133 | +[source,go] |
| 134 | +---- |
| 135 | +typedClient.Get("my_index", "id").Do(context.TODO()) |
| 136 | +---- |
| 137 | + |
| 138 | + |
| 139 | +[discrete] |
| 140 | +==== Searching documents |
| 141 | + |
| 142 | +This is how you can create a single match query with the low-level API: |
| 143 | + |
| 144 | +[source,go] |
| 145 | +---- |
| 146 | +query := `{ query: { match_all: {} } }` |
| 147 | +client.Search( |
| 148 | + client.Search.WithIndex("my_index"), |
| 149 | + client.Search.WithBody(strings.NewReader(query)), |
| 150 | +) |
| 151 | +---- |
| 152 | + |
| 153 | +You can perform a single match query with the fully-typed API, too: |
| 154 | + |
| 155 | +[source,go] |
| 156 | +---- |
| 157 | +typedClient.Search(). |
| 158 | + Index("my_index"). |
| 159 | + Request(&search.Request{ |
| 160 | + Query: &types.Query{MatchAll: &types.MatchAllQuery{}}, |
| 161 | + }). |
| 162 | + Do(context.TODO()) |
| 163 | +---- |
| 164 | + |
| 165 | + |
| 166 | +[discrete] |
| 167 | +==== Updating documents |
| 168 | + |
| 169 | +This is how you can update a document, for example to add a new field, by using |
| 170 | +the low-level API: |
| 171 | + |
| 172 | +[source,go] |
| 173 | +---- |
| 174 | +client.Update("my_index", "id", strings.NewReader(`{doc: { language: "Go" }}`)) |
| 175 | +---- |
| 176 | + |
| 177 | +And this is how you can update a document with the fully-typed API: |
| 178 | + |
| 179 | +[source,go] |
| 180 | +---- |
| 181 | +typedClient.Update("my_index", "id"). |
| 182 | + Request(&update.Request{ |
| 183 | + Doc: json.RawMessage(`{ language: "Go" }`), |
| 184 | + }).Do(context.TODO()) |
| 185 | +---- |
| 186 | + |
| 187 | + |
| 188 | +[discrete] |
| 189 | +==== Deleting documents |
| 190 | + |
| 191 | +Low-level API: |
| 192 | + |
| 193 | +[source,go] |
| 194 | +---- |
| 195 | +client.Delete("my_index", "id") |
| 196 | +---- |
| 197 | + |
| 198 | +Fully-typed API: |
| 199 | + |
| 200 | +[source,go] |
| 201 | +---- |
| 202 | +typedClient.Delete("my_index", "id").Do(context.TODO()) |
| 203 | +---- |
| 204 | + |
| 205 | + |
| 206 | +[discrete] |
| 207 | +==== Deleting an index |
| 208 | + |
| 209 | +Low-level API: |
| 210 | + |
| 211 | +[source,go] |
| 212 | +---- |
| 213 | +client.Indices.Delete([]string{"my_index"}) |
| 214 | +---- |
| 215 | + |
| 216 | +Fully-typed API: |
| 217 | + |
| 218 | +[source,go] |
| 219 | +---- |
| 220 | +typedClient.Indices.Delete("my_index").Do(context.TODO()) |
| 221 | +---- |
| 222 | + |
| 223 | + |
| 224 | +[discrete] |
| 225 | +== Further reading |
| 226 | + |
| 227 | +* Learn more about the <<typedapi>>, a strongly typed Golang API |
| 228 | +for {es}. |
0 commit comments