diff --git a/.doc/connecting.asciidoc b/.doc/connecting.asciidoc deleted file mode 100644 index bec34a2815..0000000000 --- a/.doc/connecting.asciidoc +++ /dev/null @@ -1,346 +0,0 @@ -[[connecting]] -== Connecting - -This page contains the information you need to connect and use the Client with -{es}. - -**On this page** - -* Connecting -** <> -** <> -** <> -** <> -** <> -** <> -* <> -** <> -** <> -* <> -* <> -* <> - -[discrete] -[[connecting-to-elastic-cloud]] -==== Connecting to Elastic Cloud - -If you are using https://www.elastic.co/cloud[Elastic Cloud], the client offers -an easy way to connect to it. You must pass the Cloud ID that you can find in -the cloud console and the corresponding API key. - -[source,go] ------------------------------------- -cfg := elasticsearch.Config{ - CloudID: "CLOUD_ID", - APIKey: "API_KEY" -} -es, err := elasticsearch.NewClient(cfg) ------------------------------------- -IMPORTANT: you need to copy and store the `API key` in a secure place since you will not be able to view it again in Elastic Cloud. - -[discrete] -[[connecting-to-self-managed]] -==== Connecting to a self-managed cluster - -Starting from version 8.0, {es} offers security by default with authentication and TLS enabled. - -To connect to the {es} cluster you need to configure the client to use the generated CA certificate. If you’re just getting started with {es} we recommend reading the documentation on configuring and starting {es} to ensure your cluster is running as expected. - -When you start {es} for the first time you’ll see a distinct block like the one below in the output from {es} (you may have to scroll up if it’s been a while): - -```sh ----------------------------------------------------------------- --> Elasticsearch security features have been automatically configured! --> Authentication is enabled and cluster connections are encrypted. --> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`): - lhQpLELkjkrawaBoaz0Q --> HTTP CA certificate SHA-256 fingerprint: - a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228 -... ----------------------------------------------------------------- -``` - -Note down the `elastic` user password and HTTP CA fingerprint for the next sections. In the examples below they will be stored in the variables `ELASTIC_PASSWORD` and `CERT_FINGERPRINT` respectively. - -Depending on the circumstances there are two options for verifying the HTTPS connection, either verifying with the CA certificate itself or via the HTTP CA certificate fingerprint. - -[discrete] -[[verifying-with-ca]] -==== Verifying HTTPS with CA certificates - -The generated root CA certificate can be found in the `certs` directory in your {es} config location (`$ES_CONF_PATH/certs/http_ca.crt`). If you're running {es} in Docker there is https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html[additional documentation for retrieving the CA certificate]. - -Once you have the `http_ca.crt` file somewhere accessible pass the content of the file to the client via `CACert`: - -[source,go] ------------------------------------- -cert, _ := os.ReadFile("/path/to/http_ca.crt") - -cfg := elasticsearch.Config{ - Addresses: []string{ - "https://localhost:9200", - }, - Username: "elastic", - Password: ELASTIC_PASSWORD - CACert: cert -} -es, err := elasticsearch.NewClient(cfg) ------------------------------------- - -[discrete] -[[verifying-with-fingerprint]] -==== Verifying HTTPS with certificate fingerprint - -This method of verifying the HTTPS connection takes advantage of the certificate fingerprint value noted down earlier. Take this SHA256 fingerprint value and pass it to the Go {es} client via `ca_fingerprint`: - -[source,go] ------------------------------------- -cfg := elasticsearch.Config{ - Addresses: []string{ - "https://localhost:9200", - }, - Username: "elastic", - Password: ELASTIC_PASSWORD - CertificateFingerprint: CERT_FINGERPRINT -} -es, err := elasticsearch.NewClient(cfg) ------------------------------------- - -The certificate fingerprint can be calculated using openssl x509 with the certificate file: - -[source,sh] ----- -openssl x509 -fingerprint -sha256 -noout -in /path/to/http_ca.crt ----- - -If you don't have access to the generated CA file from {es} you can use the following script to output the root CA fingerprint of the {es} instance with `openssl s_client`: - -[source,sh] ----- -# Replace the values of 'localhost' and '9200' to the -# corresponding host and port values for the cluster. -openssl s_client -connect localhost:9200 -servername localhost -showcerts /dev/null \ - | openssl x509 -fingerprint -sha256 -noout -in /dev/stdin ----- - -The output of `openssl x509` will look something like this: - -[source,sh] ----- -SHA256 Fingerprint=A5:2D:D9:35:11:E8:C6:04:5E:21:F1:66:54:B7:7C:9E:E0:F3:4A:EA:26:D9:F4:03:20:B5:31:C4:74:67:62:28 ----- - -[discrete] -[[connecting-without-security]] -==== Connecting without security enabled - -WARNING: Running {es} without security enabled is not recommended. - -If your cluster is configured with -https://www.elastic.co/guide/en/elasticsearch/reference/current/security-settings.html[security explicitly disabled] -then you can connect via HTTP: - -[source,go] ----- -cfg := elasticsearch.Config{ - Addresses: []string{ - "http://localhost:9200", - }, -} -es, err := elasticsearch.NewClient(cfg) ----- - -[discrete] -[[connecting-multiple-nodes]] -==== Connecting to multiple nodes - -The Go {es} client supports sending API requests to multiple nodes in the -cluster. This means that work will be more evenly spread across the cluster -instead of hammering the same node over and over with requests. To configure the -client with multiple nodes you can pass a list of URLs, each URL will be used as -a separate node in the pool. - -[source,go] ----- -cfg := elasticsearch.Config{ - Addresses: []string{ - "https://localhost:9200", - "https://localhost:9201", - }, - CACert: caCert, - Username: "elastic", - Password: ELASTIC_PASSWORD, -} -es, err := elasticsearch.NewClient(cfg) ----- - -By default nodes are selected using round-robin, but alternate node selection -strategies can be implemented via the `elastictransport.Selector` interface and provided to the client configuration. - -NOTE: If your {es} cluster is behind a load balancer like when using Elastic -Cloud you won't need to configure multiple nodes. Instead use the load balancer -host and port. - -[discrete] -[[auth-reference]] -=== Authentication - -This section contains code snippets to show you how to authenticate with {es}. - - -[discrete] -[[auth-basic]] -==== Basic authentication - -To set the cluster endpoints, the username, and the password programatically, pass a configuration object to the `elasticsearch.NewClient()` function. - -[source,go] ------------------------------------- -cfg := elasticsearch.Config{ - Addresses: []string{ - "https://localhost:9200", - "https://localhost:9201", - }, - Username: "foo", - Password: "bar", -} -es, err := elasticsearch.NewClient(cfg) ------------------------------------- - -You can also include the username and password in the endpoint URL: - -``` -'https://username:password@localhost:9200' -``` - -[discrete] -[[auth-token]] -==== HTTP Bearer authentication - -HTTP Bearer authentication uses the `ServiceToken` parameter by passing the token -as a string. This authentication method is used by -https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/security-api-create-service-token.html[Service Account Tokens] -and https://www.elastic.co/guide/en/elasticsearch/reference/{branch}/security-api-get-token.html[Bearer Tokens]. - -[source,go] ------------------------------------- -cfg := elasticsearch.Config{ - Addresses: []string{ - "https://localhost:9200", - }, - ServiceToken: "token-value", -} -es, err := elasticsearch.NewClient(cfg) ------------------------------------- - -[discrete] -[[compatibility-mode]] -=== Compatibility mode - -The {es} server version 8.0 is introducing a new compatibility mode that allows you a smoother upgrade experience from 7 to 8. In a nutshell, you can use the latest 7.x `go-elasticsearch` Elasticsearch client with an 8.x Elasticsearch server, giving more room to coordinate the upgrade of your codebase to the next major version. - -If you want to leverage this functionality, please make sure that you are using the latest 7.x `go-elasticsearch` client and set the environment variable `ELASTIC_CLIENT_APIVERSIONING` to `true` or the configuration option `config.EnableCompatibilityMode` in the client `Config`. The client is handling the rest internally. For every 8.0 and beyond `go-elasticsearch` client, you're all set! The compatibility mode is enabled by default. - -[discrete] -[[client-usage]] -=== Using the client - -The {es} package ties together two separate packages for calling the Elasticsearch APIs and transferring data over HTTP: `esapi` and `estransport`, respectively. - -Use the `elasticsearch.NewDefaultClient()` function to create the client with the default settings. - -[source,go] ------------------------------------- -es, err := elasticsearch.NewDefaultClient() -if err != nil { - log.Fatalf("Error creating the client: %s", err) -} - -res, err := es.Info() -if err != nil { - log.Fatalf("Error getting response: %s", err) -} - -defer res.Body.Close() -log.Println(res) ------------------------------------- - -[discrete] -[[connecting-faas]] -=== Using the Client in a Function-as-a-Service Environment - -This section illustrates the best practices for leveraging the {es} client in a Function-as-a-Service (FaaS) environment. -The most influential optimization is to initialize the client outside of the function, the global scope. -This practice does not only improve performance but also enables background functionality as – for example – -https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how[sniffing]. -The following examples provide a skeleton for the best practices. - -[discrete] -[[connecting-faas-gcp]] -==== GCP Cloud Functions - -[source,go] ----------------------------- -package httpexample - -import ( - "github.com/elastic/go-elasticsearch/v8" -) - -var client *elasticsearch.Client - -func init() { - var err error - - ... # Client configuration - client, err = elasticsearch.NewClient(cfg) - if err != nil { - log.Fatalf("elasticsearch.NewClient: %v", err) - } -} - -func HttpExample(w http.ResponseWriter, r *http.Request) { - ... # Client usage -} - ----------------------------- - -[discrete] -[[connecting-faas-aws]] -==== AWS Lambda - -[source,go] ----------------------------- -package httpexample - -import ( - "github.com/aws/aws-lambda-go/lambda" - "github.com/elastic/go-elasticsearch/v8" -) - -var client *elasticsearch.Client - -func init() { - var err error - - ... # Client configuration - client, err = elasticsearch.NewClient(cfg) - if err != nil { - log.Fatalf("elasticsearch.NewClient: %v", err) - } -} - -func HttpExample() { - ... # Client usage -} - -func main() { - lambda.Start(HttpExample) -} ----------------------------- - -Resources used to assess these recommendations: - -* https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations[GCP Cloud Functions: Tips & Tricks] -* https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html[Best practices for working with AWS Lambda functions] -* https://docs.aws.amazon.com/lambda/latest/operatorguide/global-scope.html[AWS Lambda: Comparing the effect of global scope] diff --git a/.doc/getting-started.asciidoc b/.doc/getting-started.asciidoc deleted file mode 100644 index d603c40545..0000000000 --- a/.doc/getting-started.asciidoc +++ /dev/null @@ -1,99 +0,0 @@ -[[getting-started-go]] -== 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. - -[discrete] -=== Requirements - -Go version 1.21+ - -[discrete] -=== Installation - -To install the latest version of the client, run the following command: - -[source,shell] --------------------------- -go get github.com/elastic/go-elasticsearch/v8@latest --------------------------- - -Refer to the <> page to learn more. - - -[discrete] -=== Connecting - -include::tab-widgets/connecting-widget.asciidoc[] - - -Your Elasticsearch endpoint can be found on the **My deployment** page of your -deployment: - -image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] - -You can generate an API key on the **Management** page under Security. - -image::images/create-api-key.png[alt="Create API key",align="center"] - -For other connection options, refer to the <> section. - - -[discrete] -=== Operations - -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 <> page. - - -[discrete] -==== Creating an index - -include::tab-widgets/create-index-widget.asciidoc[] - - -[discrete] -==== Indexing documents - -include::tab-widgets/index-document-widget.asciidoc[] - - -[discrete] -==== Getting documents - -include::tab-widgets/get-documents-widget.asciidoc[] - - -[discrete] -==== Searching documents - -include::tab-widgets/search-documents-widget.asciidoc[] - - -[discrete] -==== Updating documents - -include::tab-widgets/update-documents-widget.asciidoc[] - - -[discrete] -==== Deleting documents - -include::tab-widgets/delete-documents-widget.asciidoc[] - - -[discrete] -==== Deleting an index - -include::tab-widgets/delete-index-widget.asciidoc[] - - -[discrete] -== Further reading - -* Learn more about the <>, a strongly typed Golang API -for {es}. diff --git a/.doc/index.asciidoc b/.doc/index.asciidoc deleted file mode 100644 index cacd617740..0000000000 --- a/.doc/index.asciidoc +++ /dev/null @@ -1,19 +0,0 @@ -= Elasticsearch Go Client - -:doctype: book - -include::{asciidoc-dir}/../../shared/versions/stack/{source_branch}.asciidoc[] - -include::{asciidoc-dir}/../../shared/attributes.asciidoc[] - -:es-docs: https://www.elastic.co/guide/en/elasticsearch/reference/{branch} - -include::overview.asciidoc[] - -include::getting-started.asciidoc[] - -include::installation.asciidoc[] - -include::connecting.asciidoc[] - -include::typedapi/index.asciidoc[] \ No newline at end of file diff --git a/.doc/installation.asciidoc b/.doc/installation.asciidoc deleted file mode 100644 index ccc22a8b42..0000000000 --- a/.doc/installation.asciidoc +++ /dev/null @@ -1,71 +0,0 @@ -[[installation]] -== Installation - -To install the 8.x version of the client, add the package to your `go.mod` file: - -[source,text] ------------------------------------- -require github.com/elastic/go-elasticsearch/v8 8.5 ------------------------------------- - -Or, clone the repository: - -[source,text] ------------------------------------- -git clone --branch 8.5 https://github.com/elastic/go-elasticsearch.git $GOPATH/src/github ------------------------------------- - -To install another version, modify the path or the branch name accordingly. The -client major versions correspond to the {es} major versions. - -You can find a complete example of installation below: - -[source,text] ------------------------------------- -mkdir my-elasticsearch-app && cd my-elasticsearch-app - -cat > go.mod <<-END - module my-elasticsearch-app - - require github.com/elastic/go-elasticsearch/v8 main -END - -cat > main.go <<-END - package main - - import ( - "log" - - "github.com/elastic/go-elasticsearch/v8" - ) - - func main() { - es, _ := elasticsearch.NewDefaultClient() - log.Println(elasticsearch.Version) - log.Println(es.Info()) - } -END - -go run main.go ------------------------------------- - - -[discrete] -=== {es} Version Compatibility - -The language clients are forward compatible; meaning that the clients support -communicating with greater or equal minor versions of {es} without breaking. It -does not mean that the clients automatically support new features of newer -{es} versions; it is only possible after a release of a new client version. For -example, a 8.12 client version won't automatically support the new features of -the 8.13 version of {es}, the 8.13 client version is required for that. {es} -language clients are only backwards compatible with default distributions and -without guarantees made. - -|=== -| Elasticsearch Version | Elasticsearch-Go Branch | Supported - -| main | main | -| 8.x | 8.x | 8.x -| 7.x | 7.x | 7.17 -|=== diff --git a/.doc/overview.asciidoc b/.doc/overview.asciidoc deleted file mode 100644 index 2749aaa32c..0000000000 --- a/.doc/overview.asciidoc +++ /dev/null @@ -1,67 +0,0 @@ -[[overview]] -== Overview - -This is the official Go client for {es}. - -Full documentation is hosted at -https://github.com/elastic/go-elasticsearch[GitHub] -and https://pkg.go.dev/github.com/elastic/go-elasticsearch[PkgGoDev]. This -documentation provides only an overview of features. - -[discrete] -=== Features - -* One-to-one mapping with REST API. -* Generalized, pluggable architecture. -* Helpers for convenience. -* Rich set of examples. - - -[discrete] -=== Usage - -[source,go] ------------------------------------- -package main - -import ( - "log" - - "github.com/elastic/go-elasticsearch/v7" -) - -func main() { - es, _ := elasticsearch.NewDefaultClient() - log.Println(es.Info()) -} ------------------------------------- - -[NOTE] -Please have a look at the collection of comprehensive examples in the repository -at https://github.com/elastic/go-elasticsearch/tree/master/_examples. - - -[discrete] -=== Resources - -* https://github.com/elastic/go-elasticsearch[Source Code] -* https://pkg.go.dev/github.com/elastic/go-elasticsearch[API Documentation] -* https://github.com/elastic/go-elasticsearch/tree/master/_examples[Examples and Recipes] - - -[discrete] -=== License - -Copyright 2019 {es}. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/.doc/tab-widgets/connecting-widget.asciidoc b/.doc/tab-widgets/connecting-widget.asciidoc deleted file mode 100644 index abc796c78d..0000000000 --- a/.doc/tab-widgets/connecting-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::connecting.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/connecting.asciidoc b/.doc/tab-widgets/connecting.asciidoc deleted file mode 100644 index d2fbd8379c..0000000000 --- a/.doc/tab-widgets/connecting.asciidoc +++ /dev/null @@ -1,30 +0,0 @@ -// tag::low-level[] - -You can connect to the Elastic Cloud using an API key and the Elasticsearch -endpoint for the low level API: - -[source,go] ----- -client, err := elasticsearch.NewClient(elasticsearch.Config{ - CloudID: "", - APIKey: "", -}) ----- - -// end::low-level[] - - -// tag::fully-typed[] - -You can connect to the Elastic Cloud using an API key and the Elasticsearch -endpoint for the fully-typed API: - -[source,go] ----- -typedClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{ - CloudID: "", - APIKey: "", -}) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/create-index-widget.asciidoc b/.doc/tab-widgets/create-index-widget.asciidoc deleted file mode 100644 index fbe20959b8..0000000000 --- a/.doc/tab-widgets/create-index-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::create-index.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/create-index.asciidoc b/.doc/tab-widgets/create-index.asciidoc deleted file mode 100644 index 567a4d64c6..0000000000 --- a/.doc/tab-widgets/create-index.asciidoc +++ /dev/null @@ -1,22 +0,0 @@ -// tag::low-level[] - -This is how you create the `my_index` index with the low level API: - -[source,go] ----- -client.Indices.Create("my_index") ----- - -// end::low-level[] - - -// tag::fully-typed[] - -This is how you create the `my_index` index with the fully-typed API: - -[source,go] ----- -typedClient.Indices.Create("my_index").Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/delete-documents-widget.asciidoc b/.doc/tab-widgets/delete-documents-widget.asciidoc deleted file mode 100644 index d04a961236..0000000000 --- a/.doc/tab-widgets/delete-documents-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::delete-documents.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/delete-documents.asciidoc b/.doc/tab-widgets/delete-documents.asciidoc deleted file mode 100644 index e9de234f2e..0000000000 --- a/.doc/tab-widgets/delete-documents.asciidoc +++ /dev/null @@ -1,18 +0,0 @@ -// tag::low-level[] - -[source,go] ----- -client.Delete("my_index", "id") ----- - -// end::low-level[] - - -// tag::fully-typed[] - -[source,go] ----- -typedClient.Delete("my_index", "id").Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/delete-index-widget.asciidoc b/.doc/tab-widgets/delete-index-widget.asciidoc deleted file mode 100644 index 9303a96d7c..0000000000 --- a/.doc/tab-widgets/delete-index-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::delete-index.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/delete-index.asciidoc b/.doc/tab-widgets/delete-index.asciidoc deleted file mode 100644 index 66e8812901..0000000000 --- a/.doc/tab-widgets/delete-index.asciidoc +++ /dev/null @@ -1,18 +0,0 @@ -// tag::low-level[] - -[source,go] ----- -client.Indices.Delete([]string{"my_index"}) ----- - -// end::low-level[] - - -// tag::fully-typed[] - -[source,go] ----- -typedClient.Indices.Delete("my_index").Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/get-documents-widget.asciidoc b/.doc/tab-widgets/get-documents-widget.asciidoc deleted file mode 100644 index fbb0db0a22..0000000000 --- a/.doc/tab-widgets/get-documents-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::get-documents.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/get-documents.asciidoc b/.doc/tab-widgets/get-documents.asciidoc deleted file mode 100644 index 9451f247fa..0000000000 --- a/.doc/tab-widgets/get-documents.asciidoc +++ /dev/null @@ -1,22 +0,0 @@ -// tag::low-level[] - -You can get documents by using the following code with the low-level API: - -[source,go] ----- -client.Get("my_index", "id") ----- - -// end::low-level[] - - -// tag::fully-typed[] - -This is how you can get documents by using the fully-typed API: - -[source,go] ----- -typedClient.Get("my_index", "id").Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/index-document-widget.asciidoc b/.doc/tab-widgets/index-document-widget.asciidoc deleted file mode 100644 index 81c3f6a22f..0000000000 --- a/.doc/tab-widgets/index-document-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::index-document.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/index-document.asciidoc b/.doc/tab-widgets/index-document.asciidoc deleted file mode 100644 index b36f2a67e6..0000000000 --- a/.doc/tab-widgets/index-document.asciidoc +++ /dev/null @@ -1,36 +0,0 @@ -// tag::low-level[] - -This is a simple way of indexing a document by using the low-level API: - -[source,go] ----- -document := struct { - Name string `json:"name"` -}{ - "go-elasticsearch", -} -data, _ := json.Marshal(document) -client.Index("my_index", bytes.NewReader(data)) ----- - -// end::low-level[] - - -// tag::fully-typed[] - -This is a simple way of indexing a document by using the fully-typed API: - -[source,go] ----- -document := struct { - Name string `json:"name"` -}{ - "go-elasticsearch", -} -typedClient.Index("my_index"). - Id("1"). - Request(document). - Do(context.TODO()) ----- - -// end::fully-typed[] diff --git a/.doc/tab-widgets/search-documents-widget.asciidoc b/.doc/tab-widgets/search-documents-widget.asciidoc deleted file mode 100644 index 26cfdd23f9..0000000000 --- a/.doc/tab-widgets/search-documents-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::search-documents.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/search-documents.asciidoc b/.doc/tab-widgets/search-documents.asciidoc deleted file mode 100644 index f7f8a65187..0000000000 --- a/.doc/tab-widgets/search-documents.asciidoc +++ /dev/null @@ -1,31 +0,0 @@ -// tag::low-level[] - -This is how you can create a single match query with the low-level API: - -[source,go] ----- -query := `{ "query": { "match_all": {} } }` -client.Search( - client.Search.WithIndex("my_index"), - client.Search.WithBody(strings.NewReader(query)), -) ----- - -// end::low-level[] - - -// tag::fully-typed[] - -This is how you can perform a single match query with the fully-typed API: - -[source,go] ----- -typedClient.Search(). - Index("my_index"). - Request(&search.Request{ - Query: &types.Query{MatchAll: &types.MatchAllQuery{}}, - }). - Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/tab-widgets/update-documents-widget.asciidoc b/.doc/tab-widgets/update-documents-widget.asciidoc deleted file mode 100644 index 1f539e1987..0000000000 --- a/.doc/tab-widgets/update-documents-widget.asciidoc +++ /dev/null @@ -1,39 +0,0 @@ -++++ -
-
- - -
-
-++++ - -include::update-documents.asciidoc[tag=low-level] - -++++ -
- -
-++++ \ No newline at end of file diff --git a/.doc/tab-widgets/update-documents.asciidoc b/.doc/tab-widgets/update-documents.asciidoc deleted file mode 100644 index f54025b285..0000000000 --- a/.doc/tab-widgets/update-documents.asciidoc +++ /dev/null @@ -1,26 +0,0 @@ -// tag::low-level[] - -This is how you can update a document, for example to add a new field, by using -the low-level API: - -[source,go] ----- -client.Update("my_index", "id", strings.NewReader(`{doc: { language: "Go" }}`)) ----- - -// end::low-level[] - - -// tag::fully-typed[] - -This is how you can update a document with the fully-typed API: - -[source,go] ----- -typedClient.Update("my_index", "id"). - Request(&update.Request{ - Doc: json.RawMessage(`{ language: "Go" }`), - }).Do(context.TODO()) ----- - -// end::fully-typed[] \ No newline at end of file diff --git a/.doc/typedapi/conventions/endpoints.asciidoc b/.doc/typedapi/conventions/endpoints.asciidoc deleted file mode 100644 index 05576807e7..0000000000 --- a/.doc/typedapi/conventions/endpoints.asciidoc +++ /dev/null @@ -1,46 +0,0 @@ -[[endpoints]] -==== Endpoints - -All the available endpoints are generated in separate packages and assembled in the client. The `core` namespace is duplicated at the root of the client for convenient access. - -Each endpoint follows a factory pattern which returns a pointer to a new instance each time. - -[source,go] ------ -res, err := es.Search().Index("index_name").AllowPartialSearchResults(true).Do(context.Background()) ------ - -If parameters are needed for the specific endpoint you are using, those will be present as arguments in the same order as the API: - -[source,go] ------------------------------------- -es.Create("index_name", "doc_id").Do(context.Background()) ------------------------------------- - -Otherwise, you can find them within the builder: - -[source,go] ------------------------------------- -es.Search().Index("index_name").Do(context.Background()) ------------------------------------- - -Alternatively each endpoint can be instantiated directly from its package: - -[source,go] ------------------------------------- -transport, err := elastictransport.New(elastictransport.Config{}) -res, err = search.New(transport).Do(context.Background()) ------------------------------------- - -The `Do` method takes an optional `context`, runs the request through the transport and returns the results as well as an error. - -For body-empty endpoints such as `core.Exists`, an additional method `IsSuccess` is available. As the `Do` method, it takes an optional `context`, drains and closes the body if needed, and returns a boolean alongside an error - -[source,go] ------ -if exists, err := es.Core.Exists("index_name", "doc_id").IsSuccess(context.Background()); exists { - // The document exists! -} else if err != nil { - // An error occurred. -} ------ \ No newline at end of file diff --git a/.doc/typedapi/conventions/index.asciidoc b/.doc/typedapi/conventions/index.asciidoc deleted file mode 100644 index 49c42c32d9..0000000000 --- a/.doc/typedapi/conventions/index.asciidoc +++ /dev/null @@ -1,18 +0,0 @@ -[[conventions]] -=== Conventions - -This section details the conventions upon which the typed client is built. - -* <> -* <> -* <> -* <> -* <> -* <> - -include::structure.asciidoc[] -include::naming.asciidoc[] -include::endpoints.asciidoc[] -include::requests.asciidoc[] -include::responses.asciidoc[] -include::types.asciidoc[] \ No newline at end of file diff --git a/.doc/typedapi/conventions/naming.asciidoc b/.doc/typedapi/conventions/naming.asciidoc deleted file mode 100644 index 2c46385f66..0000000000 --- a/.doc/typedapi/conventions/naming.asciidoc +++ /dev/null @@ -1,7 +0,0 @@ -[[naming]] -==== Naming - -Whenever appropriate, names may be suffixed with an underscore: - -* To avoid collision with protected keywords (`range`, `if`, `type`, and so on). -* To reflect the presence of a leading underscore in the API like `\_index` vs `Index_` or `\_source` vs `Source_`. \ No newline at end of file diff --git a/.doc/typedapi/conventions/requests.asciidoc b/.doc/typedapi/conventions/requests.asciidoc deleted file mode 100644 index f3cf3f3513..0000000000 --- a/.doc/typedapi/conventions/requests.asciidoc +++ /dev/null @@ -1,14 +0,0 @@ -[[requests]] -==== Requests - -Requests are modeled around structures that follows as closely as possible the {es} API and uses the standard `json/encoding` for serialization. -Corresponding request can be found withing the same package as its endpoint and comes with a Builder that allows you to deep dive into the API by following the types. - -[source,go] ------------------------------------- -types.Query{ - Term: map[string]types.TermQuery{ - "name": {Value: "Foo"}, - }, -} ------------------------------------- \ No newline at end of file diff --git a/.doc/typedapi/conventions/responses.asciidoc b/.doc/typedapi/conventions/responses.asciidoc deleted file mode 100644 index 1f7a49202b..0000000000 --- a/.doc/typedapi/conventions/responses.asciidoc +++ /dev/null @@ -1,4 +0,0 @@ -[[responses]] -==== Responses - -While not part of the initial release responses will be added at a later date. \ No newline at end of file diff --git a/.doc/typedapi/conventions/structure.asciidoc b/.doc/typedapi/conventions/structure.asciidoc deleted file mode 100644 index 72281fe168..0000000000 --- a/.doc/typedapi/conventions/structure.asciidoc +++ /dev/null @@ -1,10 +0,0 @@ -[[structure]] -==== Structure - -The Typed client lives in the `typedapi` package within the `go-elasticsearch` repository. - -The entire client is summed in an index at the root of the package for convenient access after instantiation. - -Each endpoint resides in its own package within `typedapi` and contains the client for this endpoint, and the `Request` struct if applicable. - -The requests are based on a collection of structures generated from the https://github.com/elastic/elasticsearch-specification[elasticsearch-specification] repository and gathered in a `types` package within `typedapi`. \ No newline at end of file diff --git a/.doc/typedapi/conventions/types.asciidoc b/.doc/typedapi/conventions/types.asciidoc deleted file mode 100644 index 2b8e28c0e0..0000000000 --- a/.doc/typedapi/conventions/types.asciidoc +++ /dev/null @@ -1,22 +0,0 @@ -[[types]] -==== Types - -Requests and responses are relying on a collection of structures generated from the https://github.com/elastic/elasticsearch-specification[elasticsearch-specification] in the `types` package. -Each type comes with json tags. - -==== Enums - -The {es} API has several instances of enumerations, each has a package within `types/enums`. -An enum is declared as a type and each member of the enum is an exported variable with its value. -The enum types serializes to the relevant API value, for example the `refresh` options which can be found in the Search API: - -[source,go] ------------------------------------- -refresh.True => "true" -refresh.False => "false" -refresh.Waitfor => "wait_for" ------------------------------------- - -==== Unions - -To capture the expressiveness of the API union fields are represented by a type alias to an interface. \ No newline at end of file diff --git a/.doc/typedapi/examples/aggregations.asciidoc b/.doc/typedapi/examples/aggregations.asciidoc deleted file mode 100644 index 827c65db6b..0000000000 --- a/.doc/typedapi/examples/aggregations.asciidoc +++ /dev/null @@ -1,25 +0,0 @@ -[[aggregations]] -==== Aggregations - -Given documents with a `price` field, we run a sum aggregation on `index_name`: -[source,go] ------ -totalPricesAgg, err := es.Search(). - Index("index_name"). // <1> - Request( - &search.Request{ - Size: some.Int(0), // <2> - Aggregations: map[string]types.Aggregations{ - "total_prices": { // <3> - Sum: &types.SumAggregation{ - Field: some.String("price"), // <4> - }, - }, - }, - }, - ).Do(context.Background()) ------ -<1> Specifies the index name. -<2> Sets the size to 0 to retrieve only the result of the aggregation. -<3> Specifies the field name on which the sum aggregation runs. -<4> The `SumAggregation` is part of the `Aggregations` map. \ No newline at end of file diff --git a/.doc/typedapi/examples/getdoc.asciidoc b/.doc/typedapi/examples/getdoc.asciidoc deleted file mode 100644 index eaeb29f6fb..0000000000 --- a/.doc/typedapi/examples/getdoc.asciidoc +++ /dev/null @@ -1,24 +0,0 @@ -[[retrieving_document]] -==== Retrieving a document - -Retrieving a document follows the API as part of the argument of the endpoint. -In order you provide the `index`, the `id` and then run the query: -[source,go] ------ -res, err := es.Get("index_name", "doc_id").Do(context.Background()) ------ - -==== Checking for a document existence - -If you do not wish to retrieve the content of the document and want only to check if it exists in your index, we provide the `IsSuccess` shortcut: -[source,go] ------ -if exists, err := es.Exists("index_name", "doc_id").IsSuccess(nil); exists { - // The document exists ! -} else if err != nil { - // Handle error. -} ------ - -Result is `true` if everything succeeds, `false` if the document doesn't exist. -If an error occurs during the request, you will be granted with a `false` and the relevant error. \ No newline at end of file diff --git a/.doc/typedapi/examples/index.asciidoc b/.doc/typedapi/examples/index.asciidoc deleted file mode 100644 index 667ac5301c..0000000000 --- a/.doc/typedapi/examples/index.asciidoc +++ /dev/null @@ -1,18 +0,0 @@ -[[examples]] -=== Examples - -This sections lists a series of frequent use cases that will help you start with this new API. - -* <> -* <> -* <> -* <> -* <> - -NOTE: This is a work in progress, the documentation will evolve in the future. - -include::indices.asciidoc[] -include::indexing.asciidoc[] -include::getdoc.asciidoc[] -include::search.asciidoc[] -include::aggregations.asciidoc[] \ No newline at end of file diff --git a/.doc/typedapi/examples/indexing.asciidoc b/.doc/typedapi/examples/indexing.asciidoc deleted file mode 100644 index 0b4901fcfe..0000000000 --- a/.doc/typedapi/examples/indexing.asciidoc +++ /dev/null @@ -1,33 +0,0 @@ -[[indexing]] -==== Indexing a document - -The standard way of indexing a document is to provide a `struct` to the `Request` method, the standard `json/encoder` will be run on your structure and the result will be sent to {es}. - -[source,go] ------ -document := struct { - Id int `json:"id"` - Name string `json:"name"` - Price int `json:"price"` -}{ - Id: 1, - Name: "Foo", - Price: 10, -} - -res, err := es.Index("index_name"). - Request(document). - Do(context.Background()) ------ - -Alternatively, you can use the `Raw` method and provide the already serialized JSON: - -[source,go] ------ -res, err := es.Index("index_name"). - Raw([]byte(`{ - "id": 1, - "name": "Foo", - "price": 10 - }`)).Do(context.Background()) ------ diff --git a/.doc/typedapi/examples/indices.asciidoc b/.doc/typedapi/examples/indices.asciidoc deleted file mode 100644 index 54a98ef1f3..0000000000 --- a/.doc/typedapi/examples/indices.asciidoc +++ /dev/null @@ -1,18 +0,0 @@ -[[indices]] -==== Creating an index - -For this example on how to create an index, lets create an index named `test-index` and provide a mapping for the field `price` which will be an integer. -Notice how using the builder for the `IntegerNumberProperty` will automatically apply the correct value for the `type` field. - -[source,go] ------ -res, err := es.Indices.Create("test-index"). - Request(&create.Request{ - Mappings: &types.TypeMapping{ - Properties: map[string]types.Property{ - "price": types.NewIntegerNumberProperty(), - }, - }, - }). - Do(nil) ------ diff --git a/.doc/typedapi/examples/search.asciidoc b/.doc/typedapi/examples/search.asciidoc deleted file mode 100644 index 6e2ca22172..0000000000 --- a/.doc/typedapi/examples/search.asciidoc +++ /dev/null @@ -1,37 +0,0 @@ -[[search]] -==== Search - -Building a search query can be done with structs or builder. As an example, let's search for a document with a field `name` with a value of `Foo` in the index named `index_name`. - -With a struct request: -[source,go] ------ -res, err := es.Search(). - Index("index_name"). // <1> - Request(&search.Request{ // <2> - Query: &types.Query{ - Match: map[string]types.MatchQuery{ - "name": {Query: "Foo"}, // <3> - }, - }, - }).Do(context.Background()) // <4> ------ -<1> The targeted index for this search. -<2> The request part of the search. -<3> Match query specifies that `name` should match `Foo`. -<4> The query is run with a `context.Background` and returns the response. - -It produces the following JSON: - -[source,json] ------ -{ - "query": { - "match": { - "name": { - "query": "Foo" - } - } - } -} ------ diff --git a/.doc/typedapi/index.asciidoc b/.doc/typedapi/index.asciidoc deleted file mode 100644 index acd7d4dd6c..0000000000 --- a/.doc/typedapi/index.asciidoc +++ /dev/null @@ -1,38 +0,0 @@ -[[typedapi]] -== Typed API - -The goal for this API is to provide a strongly typed Golang API for -{es}. - -This was designed with structures and the Golang runtime in mind, following as -closely as possible the API and its objects. - -The first version focuses on the requests and does not yet include NDJSON -endpoints such as `bulk` or `msearch`. These will be added later on along with -typed responses and error handling. - - -[getting-started] -=== Getting started with the API - -The new typed client can be obtained from the `elasticsearch` package using the -`NewTypedClient` function. This new API takes the same arguments as the previous -one and uses the same transport underneath. - -Connection to an {es} cluster is identical to the existing client, only the API -changes: - -[source,go] ------ -client, err := elasticsearch.NewTypedClient(elasticsearch.Config{ - // Proper configuration for your {es} cluster. -}) ------ - -The code is generated from the -https://github.com/elastic/elasticsearch-specification[elasticsearch-specification]. - -include::conventions/index.asciidoc[] -include::queries.asciidoc[] -include::esql.asciidoc[] -include::examples/index.asciidoc[] \ No newline at end of file diff --git a/docs/docset.yml b/docs/docset.yml new file mode 100644 index 0000000000..341ad488a2 --- /dev/null +++ b/docs/docset.yml @@ -0,0 +1,486 @@ +project: 'Go client' +cross_links: + - docs-content + - elasticsearch +toc: + - toc: reference +subs: + ref: "https://www.elastic.co/guide/en/elasticsearch/reference/current" + ref-bare: "https://www.elastic.co/guide/en/elasticsearch/reference" + ref-8x: "https://www.elastic.co/guide/en/elasticsearch/reference/8.1" + ref-80: "https://www.elastic.co/guide/en/elasticsearch/reference/8.0" + ref-7x: "https://www.elastic.co/guide/en/elasticsearch/reference/7.17" + ref-70: "https://www.elastic.co/guide/en/elasticsearch/reference/7.0" + ref-60: "https://www.elastic.co/guide/en/elasticsearch/reference/6.0" + ref-64: "https://www.elastic.co/guide/en/elasticsearch/reference/6.4" + xpack-ref: "https://www.elastic.co/guide/en/x-pack/6.2" + logstash-ref: "https://www.elastic.co/guide/en/logstash/current" + kibana-ref: "https://www.elastic.co/guide/en/kibana/current" + kibana-ref-all: "https://www.elastic.co/guide/en/kibana" + beats-ref-root: "https://www.elastic.co/guide/en/beats" + beats-ref: "https://www.elastic.co/guide/en/beats/libbeat/current" + beats-ref-60: "https://www.elastic.co/guide/en/beats/libbeat/6.0" + beats-ref-63: "https://www.elastic.co/guide/en/beats/libbeat/6.3" + beats-devguide: "https://www.elastic.co/guide/en/beats/devguide/current" + auditbeat-ref: "https://www.elastic.co/guide/en/beats/auditbeat/current" + packetbeat-ref: "https://www.elastic.co/guide/en/beats/packetbeat/current" + metricbeat-ref: "https://www.elastic.co/guide/en/beats/metricbeat/current" + filebeat-ref: "https://www.elastic.co/guide/en/beats/filebeat/current" + functionbeat-ref: "https://www.elastic.co/guide/en/beats/functionbeat/current" + winlogbeat-ref: "https://www.elastic.co/guide/en/beats/winlogbeat/current" + heartbeat-ref: "https://www.elastic.co/guide/en/beats/heartbeat/current" + journalbeat-ref: "https://www.elastic.co/guide/en/beats/journalbeat/current" + ingest-guide: "https://www.elastic.co/guide/en/ingest/current" + fleet-guide: "https://www.elastic.co/guide/en/fleet/current" + apm-guide-ref: "https://www.elastic.co/guide/en/apm/guide/current" + apm-guide-7x: "https://www.elastic.co/guide/en/apm/guide/7.17" + apm-app-ref: "https://www.elastic.co/guide/en/kibana/current" + apm-agents-ref: "https://www.elastic.co/guide/en/apm/agent" + apm-android-ref: "https://www.elastic.co/guide/en/apm/agent/android/current" + apm-py-ref: "https://www.elastic.co/guide/en/apm/agent/python/current" + apm-py-ref-3x: "https://www.elastic.co/guide/en/apm/agent/python/3.x" + apm-node-ref-index: "https://www.elastic.co/guide/en/apm/agent/nodejs" + apm-node-ref: "https://www.elastic.co/guide/en/apm/agent/nodejs/current" + apm-node-ref-1x: "https://www.elastic.co/guide/en/apm/agent/nodejs/1.x" + apm-rum-ref: "https://www.elastic.co/guide/en/apm/agent/rum-js/current" + apm-ruby-ref: "https://www.elastic.co/guide/en/apm/agent/ruby/current" + apm-java-ref: "https://www.elastic.co/guide/en/apm/agent/java/current" + apm-go-ref: "https://www.elastic.co/guide/en/apm/agent/go/current" + apm-dotnet-ref: "https://www.elastic.co/guide/en/apm/agent/dotnet/current" + apm-php-ref: "https://www.elastic.co/guide/en/apm/agent/php/current" + apm-ios-ref: "https://www.elastic.co/guide/en/apm/agent/swift/current" + apm-lambda-ref: "https://www.elastic.co/guide/en/apm/lambda/current" + apm-attacher-ref: "https://www.elastic.co/guide/en/apm/attacher/current" + docker-logging-ref: "https://www.elastic.co/guide/en/beats/loggingplugin/current" + esf-ref: "https://www.elastic.co/guide/en/esf/current" + kinesis-firehose-ref: "https://www.elastic.co/guide/en/kinesis/{{kinesis_version}}" + estc-welcome-current: "https://www.elastic.co/guide/en/starting-with-the-elasticsearch-platform-and-its-solutions/current" + estc-welcome: "https://www.elastic.co/guide/en/starting-with-the-elasticsearch-platform-and-its-solutions/current" + estc-welcome-all: "https://www.elastic.co/guide/en/starting-with-the-elasticsearch-platform-and-its-solutions" + hadoop-ref: "https://www.elastic.co/guide/en/elasticsearch/hadoop/current" + stack-ref: "https://www.elastic.co/guide/en/elastic-stack/current" + stack-ref-67: "https://www.elastic.co/guide/en/elastic-stack/6.7" + stack-ref-68: "https://www.elastic.co/guide/en/elastic-stack/6.8" + stack-ref-70: "https://www.elastic.co/guide/en/elastic-stack/7.0" + stack-ref-80: "https://www.elastic.co/guide/en/elastic-stack/8.0" + stack-ov: "https://www.elastic.co/guide/en/elastic-stack-overview/current" + stack-gs: "https://www.elastic.co/guide/en/elastic-stack-get-started/current" + stack-gs-current: "https://www.elastic.co/guide/en/elastic-stack-get-started/current" + javaclient: "https://www.elastic.co/guide/en/elasticsearch/client/java-api/current" + java-api-client: "https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current" + java-rest: "https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current" + jsclient: "https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current" + jsclient-current: "https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current" + es-ruby-client: "https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current" + es-dotnet-client: "https://www.elastic.co/guide/en/elasticsearch/client/net-api/current" + es-php-client: "https://www.elastic.co/guide/en/elasticsearch/client/php-api/current" + es-python-client: "https://www.elastic.co/guide/en/elasticsearch/client/python-api/current" + defguide: "https://www.elastic.co/guide/en/elasticsearch/guide/2.x" + painless: "https://www.elastic.co/guide/en/elasticsearch/painless/current" + plugins: "https://www.elastic.co/guide/en/elasticsearch/plugins/current" + plugins-8x: "https://www.elastic.co/guide/en/elasticsearch/plugins/8.1" + plugins-7x: "https://www.elastic.co/guide/en/elasticsearch/plugins/7.17" + plugins-6x: "https://www.elastic.co/guide/en/elasticsearch/plugins/6.8" + glossary: "https://www.elastic.co/guide/en/elastic-stack-glossary/current" + upgrade_guide: "https://www.elastic.co/products/upgrade_guide" + blog-ref: "https://www.elastic.co/blog/" + curator-ref: "https://www.elastic.co/guide/en/elasticsearch/client/curator/current" + curator-ref-current: "https://www.elastic.co/guide/en/elasticsearch/client/curator/current" + metrics-ref: "https://www.elastic.co/guide/en/metrics/current" + metrics-guide: "https://www.elastic.co/guide/en/metrics/guide/current" + logs-ref: "https://www.elastic.co/guide/en/logs/current" + logs-guide: "https://www.elastic.co/guide/en/logs/guide/current" + uptime-guide: "https://www.elastic.co/guide/en/uptime/current" + observability-guide: "https://www.elastic.co/guide/en/observability/current" + observability-guide-all: "https://www.elastic.co/guide/en/observability" + siem-guide: "https://www.elastic.co/guide/en/siem/guide/current" + security-guide: "https://www.elastic.co/guide/en/security/current" + security-guide-all: "https://www.elastic.co/guide/en/security" + endpoint-guide: "https://www.elastic.co/guide/en/endpoint/current" + sql-odbc: "https://www.elastic.co/guide/en/elasticsearch/sql-odbc/current" + ecs-ref: "https://www.elastic.co/guide/en/ecs/current" + ecs-logging-ref: "https://www.elastic.co/guide/en/ecs-logging/overview/current" + ecs-logging-go-logrus-ref: "https://www.elastic.co/guide/en/ecs-logging/go-logrus/current" + ecs-logging-go-zap-ref: "https://www.elastic.co/guide/en/ecs-logging/go-zap/current" + ecs-logging-go-zerolog-ref: "https://www.elastic.co/guide/en/ecs-logging/go-zap/current" + ecs-logging-java-ref: "https://www.elastic.co/guide/en/ecs-logging/java/current" + ecs-logging-dotnet-ref: "https://www.elastic.co/guide/en/ecs-logging/dotnet/current" + ecs-logging-nodejs-ref: "https://www.elastic.co/guide/en/ecs-logging/nodejs/current" + ecs-logging-php-ref: "https://www.elastic.co/guide/en/ecs-logging/php/current" + ecs-logging-python-ref: "https://www.elastic.co/guide/en/ecs-logging/python/current" + ecs-logging-ruby-ref: "https://www.elastic.co/guide/en/ecs-logging/ruby/current" + ml-docs: "https://www.elastic.co/guide/en/machine-learning/current" + eland-docs: "https://www.elastic.co/guide/en/elasticsearch/client/eland/current" + eql-ref: "https://eql.readthedocs.io/en/latest/query-guide" + extendtrial: "https://www.elastic.co/trialextension" + wikipedia: "https://en.wikipedia.org/wiki" + forum: "https://discuss.elastic.co/" + xpack-forum: "https://discuss.elastic.co/c/50-x-pack" + security-forum: "https://discuss.elastic.co/c/x-pack/shield" + watcher-forum: "https://discuss.elastic.co/c/x-pack/watcher" + monitoring-forum: "https://discuss.elastic.co/c/x-pack/marvel" + graph-forum: "https://discuss.elastic.co/c/x-pack/graph" + apm-forum: "https://discuss.elastic.co/c/apm" + enterprise-search-ref: "https://www.elastic.co/guide/en/enterprise-search/current" + app-search-ref: "https://www.elastic.co/guide/en/app-search/current" + workplace-search-ref: "https://www.elastic.co/guide/en/workplace-search/current" + enterprise-search-node-ref: "https://www.elastic.co/guide/en/enterprise-search-clients/enterprise-search-node/current" + enterprise-search-php-ref: "https://www.elastic.co/guide/en/enterprise-search-clients/php/current" + enterprise-search-python-ref: "https://www.elastic.co/guide/en/enterprise-search-clients/python/current" + enterprise-search-ruby-ref: "https://www.elastic.co/guide/en/enterprise-search-clients/ruby/current" + elastic-maps-service: "https://maps.elastic.co" + integrations-docs: "https://docs.elastic.co/en/integrations" + integrations-devguide: "https://www.elastic.co/guide/en/integrations-developer/current" + time-units: "https://www.elastic.co/guide/en/elasticsearch/reference/current/api-conventions.html#time-units" + byte-units: "https://www.elastic.co/guide/en/elasticsearch/reference/current/api-conventions.html#byte-units" + apm-py-ref-v: "https://www.elastic.co/guide/en/apm/agent/python/current" + apm-node-ref-v: "https://www.elastic.co/guide/en/apm/agent/nodejs/current" + apm-rum-ref-v: "https://www.elastic.co/guide/en/apm/agent/rum-js/current" + apm-ruby-ref-v: "https://www.elastic.co/guide/en/apm/agent/ruby/current" + apm-java-ref-v: "https://www.elastic.co/guide/en/apm/agent/java/current" + apm-go-ref-v: "https://www.elastic.co/guide/en/apm/agent/go/current" + apm-ios-ref-v: "https://www.elastic.co/guide/en/apm/agent/swift/current" + apm-dotnet-ref-v: "https://www.elastic.co/guide/en/apm/agent/dotnet/current" + apm-php-ref-v: "https://www.elastic.co/guide/en/apm/agent/php/current" + ecloud: "Elastic Cloud" + esf: "Elastic Serverless Forwarder" + ess: "Elasticsearch Service" + ece: "Elastic Cloud Enterprise" + eck: "Elastic Cloud on Kubernetes" + serverless-full: "Elastic Cloud Serverless" + serverless-short: "Serverless" + es-serverless: "Elasticsearch Serverless" + es3: "Elasticsearch Serverless" + obs-serverless: "Elastic Observability Serverless" + sec-serverless: "Elastic Security Serverless" + serverless-docs: "https://docs.elastic.co/serverless" + cloud: "https://www.elastic.co/guide/en/cloud/current" + ess-utm-params: "?page=docs&placement=docs-body" + ess-baymax: "?page=docs&placement=docs-body" + ess-trial: "https://cloud.elastic.co/registration?page=docs&placement=docs-body" + ess-product: "https://www.elastic.co/cloud/elasticsearch-service?page=docs&placement=docs-body" + ess-console: "https://cloud.elastic.co?page=docs&placement=docs-body" + ess-console-name: "Elasticsearch Service Console" + ess-deployments: "https://cloud.elastic.co/deployments?page=docs&placement=docs-body" + ece-ref: "https://www.elastic.co/guide/en/cloud-enterprise/current" + eck-ref: "https://www.elastic.co/guide/en/cloud-on-k8s/current" + ess-leadin: "You can run Elasticsearch on your own hardware or use our hosted Elasticsearch Service that is available on AWS, GCP, and Azure. https://cloud.elastic.co/registration{ess-utm-params}[Try the Elasticsearch Service for free]." + ess-leadin-short: "Our hosted Elasticsearch Service is available on AWS, GCP, and Azure, and you can https://cloud.elastic.co/registration{ess-utm-params}[try it for free]." + ess-icon: "image:https://doc-icons.s3.us-east-2.amazonaws.com/logo_cloud.svg[link=\"https://cloud.elastic.co/registration{ess-utm-params}\", title=\"Supported on Elasticsearch Service\"]" + ece-icon: "image:https://doc-icons.s3.us-east-2.amazonaws.com/logo_cloud_ece.svg[link=\"https://cloud.elastic.co/registration{ess-utm-params}\", title=\"Supported on Elastic Cloud Enterprise\"]" + cloud-only: "This feature is designed for indirect use by https://cloud.elastic.co/registration{ess-utm-params}[Elasticsearch Service], https://www.elastic.co/guide/en/cloud-enterprise/{ece-version-link}[Elastic Cloud Enterprise], and https://www.elastic.co/guide/en/cloud-on-k8s/current[Elastic Cloud on Kubernetes]. Direct use is not supported." + ess-setting-change: "image:https://doc-icons.s3.us-east-2.amazonaws.com/logo_cloud.svg[link=\"{ess-trial}\", title=\"Supported on {ess}\"] indicates a change to a supported https://www.elastic.co/guide/en/cloud/current/ec-add-user-settings.html[user setting] for Elasticsearch Service." + ess-skip-section: "If you use Elasticsearch Service, skip this section. Elasticsearch Service handles these changes for you." + api-cloud: "https://www.elastic.co/docs/api/doc/cloud" + api-ece: "https://www.elastic.co/docs/api/doc/cloud-enterprise" + api-kibana-serverless: "https://www.elastic.co/docs/api/doc/serverless" + es-feature-flag: "This feature is in development and not yet available for use. This documentation is provided for informational purposes only." + es-ref-dir: "'{{elasticsearch-root}}/docs/reference'" + apm-app: "APM app" + uptime-app: "Uptime app" + synthetics-app: "Synthetics app" + logs-app: "Logs app" + metrics-app: "Metrics app" + infrastructure-app: "Infrastructure app" + siem-app: "SIEM app" + security-app: "Elastic Security app" + ml-app: "Machine Learning" + dev-tools-app: "Dev Tools" + ingest-manager-app: "Ingest Manager" + stack-manage-app: "Stack Management" + stack-monitor-app: "Stack Monitoring" + alerts-ui: "Alerts and Actions" + rules-ui: "Rules" + rac-ui: "Rules and Connectors" + connectors-ui: "Connectors" + connectors-feature: "Actions and Connectors" + stack-rules-feature: "Stack Rules" + user-experience: "User Experience" + ems: "Elastic Maps Service" + ems-init: "EMS" + hosted-ems: "Elastic Maps Server" + ipm-app: "Index Pattern Management" + ingest-pipelines: "ingest pipelines" + ingest-pipelines-app: "Ingest Pipelines" + ingest-pipelines-cap: "Ingest pipelines" + ls-pipelines: "Logstash pipelines" + ls-pipelines-app: "Logstash Pipelines" + maint-windows: "maintenance windows" + maint-windows-app: "Maintenance Windows" + maint-windows-cap: "Maintenance windows" + custom-roles-app: "Custom Roles" + data-source: "data view" + data-sources: "data views" + data-source-caps: "Data View" + data-sources-caps: "Data Views" + data-source-cap: "Data view" + data-sources-cap: "Data views" + project-settings: "Project settings" + manage-app: "Management" + index-manage-app: "Index Management" + data-views-app: "Data Views" + rules-app: "Rules" + saved-objects-app: "Saved Objects" + tags-app: "Tags" + api-keys-app: "API keys" + transforms-app: "Transforms" + connectors-app: "Connectors" + files-app: "Files" + reports-app: "Reports" + maps-app: "Maps" + alerts-app: "Alerts" + crawler: "Enterprise Search web crawler" + ents: "Enterprise Search" + app-search-crawler: "App Search web crawler" + agent: "Elastic Agent" + agents: "Elastic Agents" + fleet: "Fleet" + fleet-server: "Fleet Server" + integrations-server: "Integrations Server" + ingest-manager: "Ingest Manager" + ingest-management: "ingest management" + package-manager: "Elastic Package Manager" + integrations: "Integrations" + package-registry: "Elastic Package Registry" + artifact-registry: "Elastic Artifact Registry" + aws: "AWS" + stack: "Elastic Stack" + xpack: "X-Pack" + es: "Elasticsearch" + kib: "Kibana" + esms: "Elastic Stack Monitoring Service" + esms-init: "ESMS" + ls: "Logstash" + beats: "Beats" + auditbeat: "Auditbeat" + filebeat: "Filebeat" + heartbeat: "Heartbeat" + metricbeat: "Metricbeat" + packetbeat: "Packetbeat" + winlogbeat: "Winlogbeat" + functionbeat: "Functionbeat" + journalbeat: "Journalbeat" + es-sql: "Elasticsearch SQL" + esql: "ES|QL" + elastic-agent: "Elastic Agent" + k8s: "Kubernetes" + log-driver-long: "Elastic Logging Plugin for Docker" + security: "X-Pack security" + security-features: "security features" + operator-feature: "operator privileges feature" + es-security-features: "Elasticsearch security features" + stack-security-features: "Elastic Stack security features" + endpoint-sec: "Endpoint Security" + endpoint-cloud-sec: "Endpoint and Cloud Security" + elastic-defend: "Elastic Defend" + elastic-sec: "Elastic Security" + elastic-endpoint: "Elastic Endpoint" + swimlane: "Swimlane" + sn: "ServiceNow" + sn-itsm: "ServiceNow ITSM" + sn-itom: "ServiceNow ITOM" + sn-sir: "ServiceNow SecOps" + jira: "Jira" + ibm-r: "IBM Resilient" + webhook: "Webhook" + webhook-cm: "Webhook - Case Management" + opsgenie: "Opsgenie" + bedrock: "Amazon Bedrock" + gemini: "Google Gemini" + hive: "TheHive" + monitoring: "X-Pack monitoring" + monitor-features: "monitoring features" + stack-monitor-features: "Elastic Stack monitoring features" + watcher: "Watcher" + alert-features: "alerting features" + reporting: "X-Pack reporting" + report-features: "reporting features" + graph: "X-Pack graph" + graph-features: "graph analytics features" + searchprofiler: "Search Profiler" + xpackml: "X-Pack machine learning" + ml: "machine learning" + ml-cap: "Machine learning" + ml-init: "ML" + ml-features: "machine learning features" + stack-ml-features: "Elastic Stack machine learning features" + ccr: "cross-cluster replication" + ccr-cap: "Cross-cluster replication" + ccr-init: "CCR" + ccs: "cross-cluster search" + ccs-cap: "Cross-cluster search" + ccs-init: "CCS" + ilm: "index lifecycle management" + ilm-cap: "Index lifecycle management" + ilm-init: "ILM" + dlm: "data lifecycle management" + dlm-cap: "Data lifecycle management" + dlm-init: "DLM" + search-snap: "searchable snapshot" + search-snaps: "searchable snapshots" + search-snaps-cap: "Searchable snapshots" + slm: "snapshot lifecycle management" + slm-cap: "Snapshot lifecycle management" + slm-init: "SLM" + rollup-features: "data rollup features" + ipm: "index pattern management" + ipm-cap: "Index pattern" + rollup: "rollup" + rollup-cap: "Rollup" + rollups: "rollups" + rollups-cap: "Rollups" + rollup-job: "rollup job" + rollup-jobs: "rollup jobs" + rollup-jobs-cap: "Rollup jobs" + dfeed: "datafeed" + dfeeds: "datafeeds" + dfeed-cap: "Datafeed" + dfeeds-cap: "Datafeeds" + ml-jobs: "machine learning jobs" + ml-jobs-cap: "Machine learning jobs" + anomaly-detect: "anomaly detection" + anomaly-detect-cap: "Anomaly detection" + anomaly-job: "anomaly detection job" + anomaly-jobs: "anomaly detection jobs" + anomaly-jobs-cap: "Anomaly detection jobs" + dataframe: "data frame" + dataframes: "data frames" + dataframe-cap: "Data frame" + dataframes-cap: "Data frames" + watcher-transform: "payload transform" + watcher-transforms: "payload transforms" + watcher-transform-cap: "Payload transform" + watcher-transforms-cap: "Payload transforms" + transform: "transform" + transforms: "transforms" + transform-cap: "Transform" + transforms-cap: "Transforms" + dataframe-transform: "transform" + dataframe-transform-cap: "Transform" + dataframe-transforms: "transforms" + dataframe-transforms-cap: "Transforms" + dfanalytics-cap: "Data frame analytics" + dfanalytics: "data frame analytics" + dataframe-analytics-config: "'{dataframe} analytics config'" + dfanalytics-job: "'{dataframe} analytics job'" + dfanalytics-jobs: "'{dataframe} analytics jobs'" + dfanalytics-jobs-cap: "'{dataframe-cap} analytics jobs'" + cdataframe: "continuous data frame" + cdataframes: "continuous data frames" + cdataframe-cap: "Continuous data frame" + cdataframes-cap: "Continuous data frames" + cdataframe-transform: "continuous transform" + cdataframe-transforms: "continuous transforms" + cdataframe-transforms-cap: "Continuous transforms" + ctransform: "continuous transform" + ctransform-cap: "Continuous transform" + ctransforms: "continuous transforms" + ctransforms-cap: "Continuous transforms" + oldetection: "outlier detection" + oldetection-cap: "Outlier detection" + olscore: "outlier score" + olscores: "outlier scores" + fiscore: "feature influence score" + evaluatedf-api: "evaluate {dataframe} analytics API" + evaluatedf-api-cap: "Evaluate {dataframe} analytics API" + binarysc: "binary soft classification" + binarysc-cap: "Binary soft classification" + regression: "regression" + regression-cap: "Regression" + reganalysis: "regression analysis" + reganalysis-cap: "Regression analysis" + depvar: "dependent variable" + feature-var: "feature variable" + feature-vars: "feature variables" + feature-vars-cap: "Feature variables" + classification: "classification" + classification-cap: "Classification" + classanalysis: "classification analysis" + classanalysis-cap: "Classification analysis" + infer-cap: "Inference" + infer: "inference" + lang-ident-cap: "Language identification" + lang-ident: "language identification" + data-viz: "Data Visualizer" + file-data-viz: "File Data Visualizer" + feat-imp: "feature importance" + feat-imp-cap: "Feature importance" + nlp: "natural language processing" + nlp-cap: "Natural language processing" + apm-agent: "APM agent" + apm-go-agent: "Elastic APM Go agent" + apm-go-agents: "Elastic APM Go agents" + apm-ios-agent: "Elastic APM iOS agent" + apm-ios-agents: "Elastic APM iOS agents" + apm-java-agent: "Elastic APM Java agent" + apm-java-agents: "Elastic APM Java agents" + apm-dotnet-agent: "Elastic APM .NET agent" + apm-dotnet-agents: "Elastic APM .NET agents" + apm-node-agent: "Elastic APM Node.js agent" + apm-node-agents: "Elastic APM Node.js agents" + apm-php-agent: "Elastic APM PHP agent" + apm-php-agents: "Elastic APM PHP agents" + apm-py-agent: "Elastic APM Python agent" + apm-py-agents: "Elastic APM Python agents" + apm-ruby-agent: "Elastic APM Ruby agent" + apm-ruby-agents: "Elastic APM Ruby agents" + apm-rum-agent: "Elastic APM Real User Monitoring (RUM) JavaScript agent" + apm-rum-agents: "Elastic APM RUM JavaScript agents" + apm-lambda-ext: "Elastic APM AWS Lambda extension" + project-monitors: "project monitors" + project-monitors-cap: "Project monitors" + private-location: "Private Location" + private-locations: "Private Locations" + pwd: "YOUR_PASSWORD" + esh: "ES-Hadoop" + default-dist: "default distribution" + oss-dist: "OSS-only distribution" + observability: "Observability" + api-request-title: "Request" + api-prereq-title: "Prerequisites" + api-description-title: "Description" + api-path-parms-title: "Path parameters" + api-query-parms-title: "Query parameters" + api-request-body-title: "Request body" + api-response-codes-title: "Response codes" + api-response-body-title: "Response body" + api-example-title: "Example" + api-examples-title: "Examples" + api-definitions-title: "Properties" + multi-arg: "†footnoteref:[multi-arg,This parameter accepts multiple arguments.]" + multi-arg-ref: "†footnoteref:[multi-arg]" + yes-icon: "image:https://doc-icons.s3.us-east-2.amazonaws.com/icon-yes.png[Yes,20,15]" + no-icon: "image:https://doc-icons.s3.us-east-2.amazonaws.com/icon-no.png[No,20,15]" + es-repo: "https://github.com/elastic/elasticsearch/" + es-issue: "https://github.com/elastic/elasticsearch/issues/" + es-pull: "https://github.com/elastic/elasticsearch/pull/" + es-commit: "https://github.com/elastic/elasticsearch/commit/" + kib-repo: "https://github.com/elastic/kibana/" + kib-issue: "https://github.com/elastic/kibana/issues/" + kibana-issue: "'{kib-repo}issues/'" + kib-pull: "https://github.com/elastic/kibana/pull/" + kibana-pull: "'{kib-repo}pull/'" + kib-commit: "https://github.com/elastic/kibana/commit/" + ml-repo: "https://github.com/elastic/ml-cpp/" + ml-issue: "https://github.com/elastic/ml-cpp/issues/" + ml-pull: "https://github.com/elastic/ml-cpp/pull/" + ml-commit: "https://github.com/elastic/ml-cpp/commit/" + apm-repo: "https://github.com/elastic/apm-server/" + apm-issue: "https://github.com/elastic/apm-server/issues/" + apm-pull: "https://github.com/elastic/apm-server/pull/" + kibana-blob: "https://github.com/elastic/kibana/blob/current/" + apm-get-started-ref: "https://www.elastic.co/guide/en/apm/get-started/current" + apm-server-ref: "https://www.elastic.co/guide/en/apm/server/current" + apm-server-ref-v: "https://www.elastic.co/guide/en/apm/server/current" + apm-server-ref-m: "https://www.elastic.co/guide/en/apm/server/master" + apm-server-ref-62: "https://www.elastic.co/guide/en/apm/server/6.2" + apm-server-ref-64: "https://www.elastic.co/guide/en/apm/server/6.4" + apm-server-ref-70: "https://www.elastic.co/guide/en/apm/server/7.0" + apm-overview-ref-v: "https://www.elastic.co/guide/en/apm/get-started/current" + apm-overview-ref-70: "https://www.elastic.co/guide/en/apm/get-started/7.0" + apm-overview-ref-m: "https://www.elastic.co/guide/en/apm/get-started/master" + infra-guide: "https://www.elastic.co/guide/en/infrastructure/guide/current" + a-data-source: "a data view" + icon-bug: "pass:[]" + icon-checkInCircleFilled: "pass:[]" + icon-warningFilled: "pass:[]" diff --git a/.doc/images/create-api-key.png b/docs/images/create-api-key.png similarity index 100% rename from .doc/images/create-api-key.png rename to docs/images/create-api-key.png diff --git a/.doc/images/es-endpoint.jpg b/docs/images/es-endpoint.jpg similarity index 100% rename from .doc/images/es-endpoint.jpg rename to docs/images/es-endpoint.jpg diff --git a/docs/reference/_getting_started_with_the_api.md b/docs/reference/_getting_started_with_the_api.md new file mode 100644 index 0000000000..780124ec63 --- /dev/null +++ b/docs/reference/_getting_started_with_the_api.md @@ -0,0 +1,19 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/_getting_started_with_the_api.html +--- + +# Getting started with the API [_getting_started_with_the_api] + +The new typed client can be obtained from the `elasticsearch` package using the `NewTypedClient` function. This new API takes the same arguments as the previous one and uses the same transport underneath. + +Connection to an {{es}} cluster is identical to the existing client, only the API changes: + +```go +client, err := elasticsearch.NewTypedClient(elasticsearch.Config{ + // Proper configuration for your {es} cluster. +}) +``` + +The code is generated from the [elasticsearch-specification](https://github.com/elastic/elasticsearch-specification). + diff --git a/docs/reference/connecting.md b/docs/reference/connecting.md new file mode 100644 index 0000000000..062d75e71b --- /dev/null +++ b/docs/reference/connecting.md @@ -0,0 +1,295 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/connecting.html +--- + +# Connecting [connecting] + +This page contains the information you need to connect and use the Client with {{es}}. + +### Connecting to Elastic Cloud [connecting-to-elastic-cloud] + +If you are using [Elastic Cloud](https://www.elastic.co/cloud), the client offers an easy way to connect to it. You must pass the Cloud ID that you can find in the cloud console and the corresponding API key. + +```go +cfg := elasticsearch.Config{ + CloudID: "CLOUD_ID", + APIKey: "API_KEY" +} +es, err := elasticsearch.NewClient(cfg) +``` + +::::{important} +you need to copy and store the `API key` in a secure place since you will not be able to view it again in Elastic Cloud. +:::: + + + +### Connecting to a self-managed cluster [connecting-to-self-managed] + +Starting from version 8.0, {{es}} offers security by default with authentication and TLS enabled. + +To connect to the {{es}} cluster you need to configure the client to use the generated CA certificate. If you’re just getting started with {{es}} we recommend reading the documentation on configuring and starting {{es}} to ensure your cluster is running as expected. + +When you start {{es}} for the first time you’ll see a distinct block like the one below in the output from {{es}} (you may have to scroll up if it’s been a while): + +```sh +---------------------------------------------------------------- +-> Elasticsearch security features have been automatically configured! +-> Authentication is enabled and cluster connections are encrypted. +-> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`): + lhQpLELkjkrawaBoaz0Q +-> HTTP CA certificate SHA-256 fingerprint: + a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228 +... +---------------------------------------------------------------- +``` + +Note down the `elastic` user password and HTTP CA fingerprint for the next sections. In the examples below they will be stored in the variables `ELASTIC_PASSWORD` and `CERT_FINGERPRINT` respectively. + +Depending on the circumstances there are two options for verifying the HTTPS connection, either verifying with the CA certificate itself or via the HTTP CA certificate fingerprint. + + +### Verifying HTTPS with CA certificates [verifying-with-ca] + +The generated root CA certificate can be found in the `certs` directory in your {{es}} config location (`$ES_CONF_PATH/certs/http_ca.crt`). If you’re running {{es}} in Docker there is [additional documentation for retrieving the CA certificate](docs-content://deploy-manage/deploy/self-managed/install-elasticsearch-with-docker.md). + +Once you have the `http_ca.crt` file somewhere accessible pass the content of the file to the client via `CACert`: + +```go +cert, _ := os.ReadFile("/path/to/http_ca.crt") + +cfg := elasticsearch.Config{ + Addresses: []string{ + "https://localhost:9200", + }, + Username: "elastic", + Password: ELASTIC_PASSWORD + CACert: cert +} +es, err := elasticsearch.NewClient(cfg) +``` + + +### Verifying HTTPS with certificate fingerprint [verifying-with-fingerprint] + +This method of verifying the HTTPS connection takes advantage of the certificate fingerprint value noted down earlier. Take this SHA256 fingerprint value and pass it to the Go {{es}} client via `ca_fingerprint`: + +```go +cfg := elasticsearch.Config{ + Addresses: []string{ + "https://localhost:9200", + }, + Username: "elastic", + Password: ELASTIC_PASSWORD + CertificateFingerprint: CERT_FINGERPRINT +} +es, err := elasticsearch.NewClient(cfg) +``` + +The certificate fingerprint can be calculated using openssl x509 with the certificate file: + +```sh +openssl x509 -fingerprint -sha256 -noout -in /path/to/http_ca.crt +``` + +If you don’t have access to the generated CA file from {{es}} you can use the following script to output the root CA fingerprint of the {{es}} instance with `openssl s_client`: + +```sh +# Replace the values of 'localhost' and '9200' to the +# corresponding host and port values for the cluster. +openssl s_client -connect localhost:9200 -servername localhost -showcerts /dev/null \ + | openssl x509 -fingerprint -sha256 -noout -in /dev/stdin +``` + +The output of `openssl x509` will look something like this: + +```sh +SHA256 Fingerprint=A5:2D:D9:35:11:E8:C6:04:5E:21:F1:66:54:B7:7C:9E:E0:F3:4A:EA:26:D9:F4:03:20:B5:31:C4:74:67:62:28 +``` + + +### Connecting without security enabled [connecting-without-security] + +::::{warning} +Running {{es}} without security enabled is not recommended. +:::: + + +If your cluster is configured with [security explicitly disabled](elasticsearch://docs/reference/elasticsearch/configuration-reference/security-settings.md) then you can connect via HTTP: + +```go +cfg := elasticsearch.Config{ + Addresses: []string{ + "http://localhost:9200", + }, +} +es, err := elasticsearch.NewClient(cfg) +``` + + +### Connecting to multiple nodes [connecting-multiple-nodes] + +The Go {{es}} client supports sending API requests to multiple nodes in the cluster. This means that work will be more evenly spread across the cluster instead of hammering the same node over and over with requests. To configure the client with multiple nodes you can pass a list of URLs, each URL will be used as a separate node in the pool. + +```go +cfg := elasticsearch.Config{ + Addresses: []string{ + "https://localhost:9200", + "https://localhost:9201", + }, + CACert: caCert, + Username: "elastic", + Password: ELASTIC_PASSWORD, +} +es, err := elasticsearch.NewClient(cfg) +``` + +By default nodes are selected using round-robin, but alternate node selection strategies can be implemented via the `elastictransport.Selector` interface and provided to the client configuration. + +::::{note} +If your {{es}} cluster is behind a load balancer like when using Elastic Cloud you won’t need to configure multiple nodes. Instead use the load balancer host and port. +:::: + + + +## Authentication [auth-reference] + +This section contains code snippets to show you how to authenticate with {{es}}. + + +### Basic authentication [auth-basic] + +To set the cluster endpoints, the username, and the password programatically, pass a configuration object to the `elasticsearch.NewClient()` function. + +```go +cfg := elasticsearch.Config{ + Addresses: []string{ + "https://localhost:9200", + "https://localhost:9201", + }, + Username: "foo", + Password: "bar", +} +es, err := elasticsearch.NewClient(cfg) +``` + +You can also include the username and password in the endpoint URL: + +``` +'https://username:password@localhost:9200' +``` + + +### HTTP Bearer authentication [auth-token] + +HTTP Bearer authentication uses the `ServiceToken` parameter by passing the token as a string. This authentication method is used by [Service Account Tokens](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-security-create-service-token) and [Bearer Tokens](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-security-get-token). + +```go +cfg := elasticsearch.Config{ + Addresses: []string{ + "https://localhost:9200", + }, + ServiceToken: "token-value", +} +es, err := elasticsearch.NewClient(cfg) +``` + + +## Compatibility mode [compatibility-mode] + +The {{es}} server version 8.0 is introducing a new compatibility mode that allows you a smoother upgrade experience from 7 to 8. In a nutshell, you can use the latest 7.x `go-elasticsearch` Elasticsearch client with an 8.x Elasticsearch server, giving more room to coordinate the upgrade of your codebase to the next major version. + +If you want to leverage this functionality, please make sure that you are using the latest 7.x `go-elasticsearch` client and set the environment variable `ELASTIC_CLIENT_APIVERSIONING` to `true` or the configuration option `config.EnableCompatibilityMode` in the client `Config`. The client is handling the rest internally. For every 8.0 and beyond `go-elasticsearch` client, you’re all set! The compatibility mode is enabled by default. + + +## Using the client [client-usage] + +The {{es}} package ties together two separate packages for calling the Elasticsearch APIs and transferring data over HTTP: `esapi` and `estransport`, respectively. + +Use the `elasticsearch.NewDefaultClient()` function to create the client with the default settings. + +```go +es, err := elasticsearch.NewDefaultClient() +if err != nil { + log.Fatalf("Error creating the client: %s", err) +} + +res, err := es.Info() +if err != nil { + log.Fatalf("Error getting response: %s", err) +} + +defer res.Body.Close() +log.Println(res) +``` + + +## Using the client in a function-as-a-service environment [connecting-faas] + +This section illustrates the best practices for leveraging the {{es}} client in a Function-as-a-Service (FaaS) environment. The most influential optimization is to initialize the client outside of the function, the global scope. This practice does not only improve performance but also enables background functionality as – for example – [sniffing](https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how). The following examples provide a skeleton for the best practices. + + +### GCP Cloud Functions [connecting-faas-gcp] + +```go +package httpexample + +import ( + "github.com/elastic/go-elasticsearch/v8" +) + +var client *elasticsearch.Client + +func init() { + var err error + + ... # Client configuration + client, err = elasticsearch.NewClient(cfg) + if err != nil { + log.Fatalf("elasticsearch.NewClient: %v", err) + } +} + +func HttpExample(w http.ResponseWriter, r *http.Request) { + ... # Client usage +} +``` + + +### AWS Lambda [connecting-faas-aws] + +```go +package httpexample + +import ( + "github.com/aws/aws-lambda-go/lambda" + "github.com/elastic/go-elasticsearch/v8" +) + +var client *elasticsearch.Client + +func init() { + var err error + + ... # Client configuration + client, err = elasticsearch.NewClient(cfg) + if err != nil { + log.Fatalf("elasticsearch.NewClient: %v", err) + } +} + +func HttpExample() { + ... # Client usage +} + +func main() { + lambda.Start(HttpExample) +} +``` + +Resources used to assess these recommendations: + +* [GCP Cloud Functions: Tips & Tricks](https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations) +* [Best practices for working with AWS Lambda functions](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.md) +* [AWS Lambda: Comparing the effect of global scope](https://docs.aws.amazon.com/lambda/latest/operatorguide/global-scope.md) diff --git a/docs/reference/conventions.md b/docs/reference/conventions.md new file mode 100644 index 0000000000..85254820fc --- /dev/null +++ b/docs/reference/conventions.md @@ -0,0 +1,116 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/conventions.html +--- + +# Conventions [conventions] + +This section details the conventions upon which the typed client is built. + +* [Structure](#structure) +* [Naming](#naming) +* [Endpoints](#endpoints) +* [Requests](#requests) +* [Responses](#responses) +* [Types](#types) + +## Structure [structure] + +The Typed client lives in the `typedapi` package within the `go-elasticsearch` repository. + +The entire client is summed in an index at the root of the package for convenient access after instantiation. + +Each endpoint resides in its own package within `typedapi` and contains the client for this endpoint, and the `Request` struct if applicable. + +The requests are based on a collection of structures generated from the [elasticsearch-specification](https://github.com/elastic/elasticsearch-specification) repository and gathered in a `types` package within `typedapi`. + + +## Naming [naming] + +Whenever appropriate, names may be suffixed with an underscore: + +* To avoid collision with protected keywords (`range`, `if`, `type`, and so on). +* To reflect the presence of a leading underscore in the API like `\_index` vs `Index_` or `\_source` vs `Source_`. + + +## Endpoints [endpoints] + +All the available endpoints are generated in separate packages and assembled in the client. The `core` namespace is duplicated at the root of the client for convenient access. + +Each endpoint follows a factory pattern which returns a pointer to a new instance each time. + +```go +res, err := es.Search().Index("index_name").AllowPartialSearchResults(true).Do(context.Background()) +``` + +If parameters are needed for the specific endpoint you are using, those will be present as arguments in the same order as the API: + +```go +es.Create("index_name", "doc_id").Do(context.Background()) +``` + +Otherwise, you can find them within the builder: + +```go +es.Search().Index("index_name").Do(context.Background()) +``` + +Alternatively each endpoint can be instantiated directly from its package: + +```go +transport, err := elastictransport.New(elastictransport.Config{}) +res, err = search.New(transport).Do(context.Background()) +``` + +The `Do` method takes an optional `context`, runs the request through the transport and returns the results as well as an error. + +For body-empty endpoints such as `core.Exists`, an additional method `IsSuccess` is available. As the `Do` method, it takes an optional `context`, drains and closes the body if needed, and returns a boolean alongside an error + +```go +if exists, err := es.Core.Exists("index_name", "doc_id").IsSuccess(context.Background()); exists { + // The document exists! +} else if err != nil { + // An error occurred. +} +``` + + +## Requests [requests] + +Requests are modeled around structures that follows as closely as possible the {{es}} API and uses the standard `json/encoding` for serialization. Corresponding request can be found withing the same package as its endpoint and comes with a Builder that allows you to deep dive into the API by following the types. + +```go +types.Query{ + Term: map[string]types.TermQuery{ + "name": {Value: "Foo"}, + }, +} +``` + + +## Responses [responses] + +While not part of the initial release responses will be added at a later date. + + +## Types [types] + +Requests and responses are relying on a collection of structures generated from the [elasticsearch-specification](https://github.com/elastic/elasticsearch-specification) in the `types` package. Each type comes with json tags. + + +## Enums [_enums] + +The {{es}} API has several instances of enumerations, each has a package within `types/enums`. An enum is declared as a type and each member of the enum is an exported variable with its value. The enum types serializes to the relevant API value, for example the `refresh` options which can be found in the Search API: + +```go +refresh.True => "true" +refresh.False => "false" +refresh.Waitfor => "wait_for" +``` + + +## Unions [_unions] + +To capture the expressiveness of the API union fields are represented by a type alias to an interface. + + diff --git a/.doc/typedapi/esql.asciidoc b/docs/reference/esql.md similarity index 57% rename from .doc/typedapi/esql.asciidoc rename to docs/reference/esql.md index c7c2700c1b..d7f3b23e25 100644 --- a/.doc/typedapi/esql.asciidoc +++ b/docs/reference/esql.md @@ -1,38 +1,27 @@ -[[esql]] -=== ES|QL in the Go client -++++ -Using ES|QL -++++ +--- +navigation_title: "Using ES|QL" +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/esql.html +--- -This page helps you understand and use {ref}/esql.html[ES|QL] in the -Go client. +# ES|QL in the Go client [esql] -There are two ways to use ES|QL in the Go client: -* Use the Elasticsearch {es-docs}/esql-apis.html[ES|QL API] directly: This -is the most flexible approach, but it's also the most complex because you must handle -results in their raw form. You can choose the precise format of results, -such as JSON, CSV, or text. -* Use ES|QL mapping helpers: These mappers take care of parsing the raw -response into something readily usable by the application. Helpers are -available for object mapping and iterative objects. +This page helps you understand and use [ES|QL](docs-content://explore-analyze/query-filter/languages/esql.md) in the Go client. + +There are two ways to use ES|QL in the Go client: +* Use the Elasticsearch [ES|QL API](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-esql) directly: This is the most flexible approach, but it’s also the most complex because you must handle results in their raw form. You can choose the precise format of results, such as JSON, CSV, or text. +* Use ES|QL mapping helpers: These mappers take care of parsing the raw response into something readily usable by the application. Helpers are available for object mapping and iterative objects. -[discrete] -[[esql-how-to]] -==== How to use the ES|QL API +## How to use the ES|QL API [esql-how-to] -The {es-docs}/esql-query-api.html[ES|QL query API] allows you to specify how -results should be returned. You can choose a -{es-docs}/esql-rest.html#esql-rest-format[response format] such as CSV, text, or -JSON, then fine-tune it with parameters like column separators -and locale. +The [ES|QL query API](https://www.elastic.co/docs/api/doc/elasticsearch/group/endpoint-esql) allows you to specify how results should be returned. You can choose a [response format](docs-content://explore-analyze/query-filter/languages/esql-rest.md#esql-rest-format) such as CSV, text, or JSON, then fine-tune it with parameters like column separators and locale. The following example gets ES|QL results as CSV and parses them: -[source,go] ------------------------------------- +```go queryAuthor := `from library | where author == "Isaac Asimov" | sort release_date desc @@ -51,26 +40,18 @@ rows, err := reader.ReadAll() for _, row := range rows { fmt.Println(row) } ------------------------------------- +``` -[discrete] -[[esql-consume-results]] -==== Consume ES|QL results +## Consume ES|QL results [esql-consume-results] -The previous example showed that although the raw ES|QL API offers maximum -flexibility, additional work is required in order to make use of the -result data. +The previous example showed that although the raw ES|QL API offers maximum flexibility, additional work is required in order to make use of the result data. -To simplify things, try working with these two main representations of ES|QL -results (each with its own mapping helper): +To simplify things, try working with these two main representations of ES|QL results (each with its own mapping helper): -* **Objects**, where each row in the results is mapped to an object from your -application domain. This is similar to what ORMs (object relational mappers) -commonly do. +* **Objects**, where each row in the results is mapped to an object from your application domain. This is similar to what ORMs (object relational mappers) commonly do. -[source,go] ------------------------------------- +```go package main import ( @@ -110,14 +91,11 @@ func main() { fmt.Println(book) } } +``` ------------------------------------- - -* **Iterative Objects**, where each row in the results is mapped to an object from your -application domain, one at a time. +* **Iterative Objects**, where each row in the results is mapped to an object from your application domain, one at a time. -[source,go] ------------------------------------- +```go queryAuthor := `from library | where author == "Isaac Asimov" | sort release_date desc @@ -136,4 +114,4 @@ for books.More() { } fmt.Println(book) } ------------------------------------- \ No newline at end of file +``` diff --git a/docs/reference/examples.md b/docs/reference/examples.md new file mode 100644 index 0000000000..982a15cce8 --- /dev/null +++ b/docs/reference/examples.md @@ -0,0 +1,160 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/examples.html +--- + +# Examples [examples] + +This sections lists a series of frequent use cases that will help you start with this new API. + +* [Creating an index](#indices) +* [Indexing a document](#indexing) +* [Retrieving a document](#retrieving_document) +* [Search](#search) +* [Aggregations](#aggregations) + +::::{note} +This is a work in progress, the documentation will evolve in the future. +:::: + + +## Creating an index [indices] + +For this example on how to create an index, lets create an index named `test-index` and provide a mapping for the field `price` which will be an integer. Notice how using the builder for the `IntegerNumberProperty` will automatically apply the correct value for the `type` field. + +```go +res, err := es.Indices.Create("test-index"). + Request(&create.Request{ + Mappings: &types.TypeMapping{ + Properties: map[string]types.Property{ + "price": types.NewIntegerNumberProperty(), + }, + }, + }). + Do(nil) +``` + + +## Indexing a document [indexing] + +The standard way of indexing a document is to provide a `struct` to the `Request` method, the standard `json/encoder` will be run on your structure and the result will be sent to {{es}}. + +```go +document := struct { + Id int `json:"id"` + Name string `json:"name"` + Price int `json:"price"` +}{ + Id: 1, + Name: "Foo", + Price: 10, +} + +res, err := es.Index("index_name"). + Request(document). + Do(context.Background()) +``` + +Alternatively, you can use the `Raw` method and provide the already serialized JSON: + +```go +res, err := es.Index("index_name"). + Raw([]byte(`{ + "id": 1, + "name": "Foo", + "price": 10 + }`)).Do(context.Background()) +``` + + +## Retrieving a document [retrieving_document] + +Retrieving a document follows the API as part of the argument of the endpoint. In order you provide the `index`, the `id` and then run the query: + +```go +res, err := es.Get("index_name", "doc_id").Do(context.Background()) +``` + + +## Checking for a document existence [_checking_for_a_document_existence] + +If you do not wish to retrieve the content of the document and want only to check if it exists in your index, we provide the `IsSuccess` shortcut: + +```go +if exists, err := es.Exists("index_name", "doc_id").IsSuccess(nil); exists { + // The document exists ! +} else if err != nil { + // Handle error. +} +``` + +Result is `true` if everything succeeds, `false` if the document doesn’t exist. If an error occurs during the request, you will be granted with a `false` and the relevant error. + + +## Search [search] + +Building a search query can be done with structs or builder. As an example, let’s search for a document with a field `name` with a value of `Foo` in the index named `index_name`. + +With a struct request: + +```go +res, err := es.Search(). + Index("index_name"). <1> + Request(&search.Request{ <2> + Query: &types.Query{ + Match: map[string]types.MatchQuery{ + "name": {Query: "Foo"}, <3> + }, + }, + }).Do(context.Background()) <4> +``` + +1. The targeted index for this search. +2. The request part of the search. +3. Match query specifies that `name` should match `Foo`. +4. The query is run with a `context.Background` and returns the response. + + +It produces the following JSON: + +```json +{ + "query": { + "match": { + "name": { + "query": "Foo" + } + } + } +} +``` + + +## Aggregations [aggregations] + +Given documents with a `price` field, we run a sum aggregation on `index_name`: + +```go +totalPricesAgg, err := es.Search(). + Index("index_name"). <1> + Request( + &search.Request{ + Size: some.Int(0), <2> + Aggregations: map[string]types.Aggregations{ + "total_prices": { <3> + Sum: &types.SumAggregation{ + Field: some.String("price"), <4> + }, + }, + }, + }, + ).Do(context.Background()) +``` + +1. Specifies the index name. +2. Sets the size to 0 to retrieve only the result of the aggregation. +3. Specifies the field name on which the sum aggregation runs. +4. The `SumAggregation` is part of the `Aggregations` map. + + + diff --git a/docs/reference/getting-started-serverless.md b/docs/reference/getting-started-serverless.md new file mode 100644 index 0000000000..e7acaf767a --- /dev/null +++ b/docs/reference/getting-started-serverless.md @@ -0,0 +1,205 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/serverless/current/elasticsearch-go-client-getting-started.html +navigation_title: Getting started in {{serverless-short}} +--- + +# Getting started with {{es}} Go in {{serverless-full}}[elasticsearch-go-client-getting-started] + +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. + + +## Requirements [elasticsearch-go-client-getting-started-requirements] + +* Go 1.22 or higher installed on your system. + + +## Installation [elasticsearch-go-client-getting-started-installation] + + +### Using the command line [elasticsearch-go-client-getting-started-using-the-command-line] + +You can install the Go client with the following commands: + +```bash +go get -u github.com/elastic/go-elasticsearch/v8@latest +``` + + +## Imports [elasticsearch-go-client-getting-started-imports] + +The following snippets use these imports: + +```go +import ( + "context" + "encoding/json" + "fmt" + "log" + "strconv" + + "github.com/elastic/go-elasticsearch/v8" + "github.com/elastic/go-elasticsearch/v8/typedapi/types" + "github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/result" +) +``` + + +## Initialize the client [elasticsearch-go-client-getting-started-initialize-the-client] + +Initialize the client using your API key and {{es}} endpoint: + +```go +client, err := elasticsearch.NewTypedClient(elasticsearch.Config{ + Addresses: []string{"https://my-project-url"}, + APIKey: "your-api-key", +}) +if err != nil { + log.Fatalf("Error creating the client: %s", err) +} +``` + +To get API keys for the {{es}} endpoint for a project, see [Get started](docs-content://solutions/search/get-started.md). + + +## Using the API [elasticsearch-go-client-getting-started-using-the-api] + +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. + + +### Creating an index and ingesting documents [elasticsearch-go-client-getting-started-creating-an-index-and-ingesting-documents] + +You can call the `bulk` API with a body parameter, an array of hashes that define the action, and a document. + +The following is an example of indexing some classic books into the `books` index: + +```go +type Book struct { + Name string `json:"name"` + Author string `json:"author"` + ReleaseDate string `json:"release_date"` + PageCount int `json:"page_count"` +} + +books := []Book{ + {Name: "Snow Crash", Author: "Neal Stephenson", ReleaseDate: "1992-06-01", PageCount: 470}, + {Name: "Revelation Space", Author: "Alastair Reynolds", ReleaseDate: "2000-03-15", PageCount: 585}, + {Name: "1984", Author: "George Orwell", ReleaseDate: "1949-06-08", PageCount: 328}, + {Name: "Fahrenheit 451", Author: "Ray Bradbury", ReleaseDate: "1953-10-15", PageCount: 227}, + {Name: "Brave New World", Author: "Aldous Huxley", ReleaseDate: "1932-06-01", PageCount: 268}, + {Name: "The Handmaid's Tale", Author: "Margaret Atwood", ReleaseDate: "1985-06-01", PageCount: 311}, +} +indexName := "books" + +bulk := client.Bulk() +for i, book := range books { + id := strconv.Itoa(i) + err := bulk.CreateOp(types.CreateOperation{Index_: &indexName, Id_: &id}, book) + if err != nil { + log.Fatal(err) + } +} +bulkRes, err := bulk.Do(context.TODO()) +if err != nil { + log.Fatal(err) +} + +fmt.Printf("Bulk: %#v\n", bulkRes.Items) +``` + +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`. + + +### Getting documents [elasticsearch-go-client-getting-started-getting-documents] + +You can get documents by using the following code: + +```go +getRes, err := client.Get(indexName, "5").Do(context.TODO()) +if err != nil { + log.Fatal(err) +} +book := Book{} +if err := json.Unmarshal(getRes.Source_, &book); err != nil { + log.Fatal(err) +} +fmt.Printf("Get book: %#v\n", book) +``` + + +### Searching [elasticsearch-go-client-getting-started-searching] + +Now that some data is available, you can search your documents using the `search` API: + +```go +searchRes, err := client.Search(). + Index("books"). + Q("snow"). + Do(context.TODO()) +if err != nil { + log.Fatal(err) +} + +bookSearch := []Book{} +for _, hit := range searchRes.Hits.Hits { + book := Book{} + if err := json.Unmarshal(hit.Source_, &book); err != nil { + log.Fatal(err) + } + bookSearch = append(bookSearch, book) +} +fmt.Printf("Search books: %#v\n", bookSearch) +``` + + +### Updating a document [elasticsearch-go-client-getting-started-updating-a-document] + +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": + +```go +updateRes, err := client.Update("books", "5"). + Doc( + struct { + PageCount int `json:"page_count"` + }{PageCount: 312}, + ). + Do(context.TODO()) +if err != nil { + log.Fatal(err) +} + +if updateRes.Result == result.Updated { + fmt.Printf("Update book: %#v\n", updateRes) +} +``` + + +### Deleting a document [elasticsearch-go-client-getting-started-deleting-a-document] + +You can call the `Delete` API to delete a document: + +```go +deleteRes, err := client.Delete("books", "5").Do(context.TODO()) +if err != nil { + log.Fatal(err) +} + +if deleteRes.Result == result.Deleted { + fmt.Printf("Delete book: %#v\n", deleteRes) +} +``` + + +### Deleting an index [elasticsearch-go-client-getting-started-deleting-an-index] + +```go +indexDeleteRes, err := client.Indices.Delete("books").Do(context.TODO()) +if err != nil { + log.Fatal(err) +} + +if indexDeleteRes.Acknowledged { + fmt.Printf("Delete index: %#v\n", indexDeleteRes) +} +``` + diff --git a/docs/reference/getting-started.md b/docs/reference/getting-started.md new file mode 100644 index 0000000000..b37d6ac59b --- /dev/null +++ b/docs/reference/getting-started.md @@ -0,0 +1,248 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html +--- + +# Getting started [getting-started-go] + +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. + + +### Requirements [_requirements] + +Go version 1.21+ + + +### Installation [_installation] + +To install the latest version of the client, run the following command: + +```shell +go get github.com/elastic/go-elasticsearch/v8@latest +``` + +Refer to the [*Installation*](/reference/installation.md) page to learn more. + + +### Connecting [_connecting] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint for the low level API: + +```go +client, err := elasticsearch.NewClient(elasticsearch.Config{ + CloudID: "", + APIKey: "", +}) +``` +:::::: + +::::::{tab-item} Fully-typed API +You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint for the fully-typed API: + +```go +typedClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{ + CloudID: "", + APIKey: "", +}) +``` +:::::: + +::::::: +Your Elasticsearch endpoint can be found on the **My deployment** page of your deployment: + +:::{image} ../images/es-endpoint.jpg +:alt: Finding Elasticsearch endpoint +::: + +You can generate an API key on the **Management** page under Security. + +:::{image} ../images/create-api-key.png +:alt: Create API key +::: + +For other connection options, refer to the [*Connecting*](/reference/connecting.md) section. + + +### Operations [_operations] + +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 [Examples](/reference/examples.md) page. + + +#### Creating an index [_creating_an_index] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +This is how you create the `my_index` index with the low level API: + +```go +client.Indices.Create("my_index") +``` +:::::: + +::::::{tab-item} Fully-typed API +This is how you create the `my_index` index with the fully-typed API: + +```go +typedClient.Indices.Create("my_index").Do(context.TODO()) +``` +:::::: + +::::::: + +#### Indexing documents [_indexing_documents] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +This is a simple way of indexing a document by using the low-level API: + +```go +document := struct { + Name string `json:"name"` +}{ + "go-elasticsearch", +} +data, _ := json.Marshal(document) +client.Index("my_index", bytes.NewReader(data)) +``` +:::::: + +::::::{tab-item} Fully-typed API +This is a simple way of indexing a document by using the fully-typed API: + +```go +document := struct { + Name string `json:"name"` +}{ + "go-elasticsearch", +} +typedClient.Index("my_index"). + Id("1"). + Request(document). + Do(context.TODO()) +``` +:::::: + +::::::: + +#### Getting documents [_getting_documents] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +You can get documents by using the following code with the low-level API: + +```go +client.Get("my_index", "id") +``` +:::::: + +::::::{tab-item} Fully-typed API +This is how you can get documents by using the fully-typed API: + +```go +typedClient.Get("my_index", "id").Do(context.TODO()) +``` +:::::: + +::::::: + +#### Searching documents [_searching_documents] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +This is how you can create a single match query with the low-level API: + +```go +query := `{ "query": { "match_all": {} } }` +client.Search( + client.Search.WithIndex("my_index"), + client.Search.WithBody(strings.NewReader(query)), +) +``` +:::::: + +::::::{tab-item} Fully-typed API +This is how you can perform a single match query with the fully-typed API: + +```go +typedClient.Search(). + Index("my_index"). + Request(&search.Request{ + Query: &types.Query{MatchAll: &types.MatchAllQuery{}}, + }). + Do(context.TODO()) +``` +:::::: + +::::::: + +#### Updating documents [_updating_documents] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +This is how you can update a document, for example to add a new field, by using the low-level API: + +```go +client.Update("my_index", "id", strings.NewReader(`{doc: { language: "Go" }}`)) +``` +:::::: + +::::::{tab-item} Fully-typed API +This is how you can update a document with the fully-typed API: + +```go +typedClient.Update("my_index", "id"). + Request(&update.Request{ + Doc: json.RawMessage(`{ language: "Go" }`), + }).Do(context.TODO()) +``` +:::::: + +::::::: + +#### Deleting documents [_deleting_documents] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +```go +client.Delete("my_index", "id") +``` +:::::: + +::::::{tab-item} Fully-typed API +```go +typedClient.Delete("my_index", "id").Do(context.TODO()) +``` +:::::: + +::::::: + +#### Deleting an index [_deleting_an_index] + +:::::::{tab-set} + +::::::{tab-item} Low-level API +```go +client.Indices.Delete([]string{"my_index"}) +``` +:::::: + +::::::{tab-item} Fully-typed API +```go +typedClient.Indices.Delete("my_index").Do(context.TODO()) +``` +:::::: + +::::::: + +## Further reading [_further_reading] + +* Learn more about the [*Typed API*](/reference/typed-api.md), a strongly typed Golang API for {{es}}. diff --git a/docs/reference/index.md b/docs/reference/index.md new file mode 100644 index 0000000000..d8b36e6d59 --- /dev/null +++ b/docs/reference/index.md @@ -0,0 +1,62 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/index.html + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/overview.html +--- + +# Go [overview] + +This is the official Go client for {{es}}. + +Full documentation is hosted at [GitHub](https://github.com/elastic/go-elasticsearch) and [PkgGoDev](https://pkg.go.dev/github.com/elastic/go-elasticsearch). This documentation provides only an overview of features. + + +## Features [_features] + +* One-to-one mapping with REST API. +* Generalized, pluggable architecture. +* Helpers for convenience. +* Rich set of examples. + + +## Usage [_usage] + +```go +package main + +import ( + "log" + + "github.com/elastic/go-elasticsearch/v7" +) + +func main() { + es, _ := elasticsearch.NewDefaultClient() + log.Println(es.Info()) +} +``` + +::::{note} +Please have a look at the collection of comprehensive examples in the repository at [https://github.com/elastic/go-elasticsearch/tree/master/_examples](https://github.com/elastic/go-elasticsearch/tree/master/_examples). +:::: + + + +## Resources [_resources] + +* [Source Code](https://github.com/elastic/go-elasticsearch) +* [API Documentation](https://pkg.go.dev/github.com/elastic/go-elasticsearch) +* [Examples and Recipes](https://github.com/elastic/go-elasticsearch/tree/master/_examples) + + +## License [_license] + +Copyright 2019 {{es}}. + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +``` +http://www.apache.org/licenses/LICENSE-2.0 +``` +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + diff --git a/docs/reference/installation.md b/docs/reference/installation.md new file mode 100644 index 0000000000..74ed7f5a5e --- /dev/null +++ b/docs/reference/installation.md @@ -0,0 +1,62 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/installation.html +--- + +# Installation [installation] + +To install the 8.x version of the client, add the package to your `go.mod` file: + +```text +require github.com/elastic/go-elasticsearch/v8 8.5 +``` + +Or, clone the repository: + +```text +git clone --branch 8.5 https://github.com/elastic/go-elasticsearch.git $GOPATH/src/github +``` + +To install another version, modify the path or the branch name accordingly. The client major versions correspond to the {{es}} major versions. + +You can find a complete example of installation below: + +```text +mkdir my-elasticsearch-app && cd my-elasticsearch-app + +cat > go.mod <<-END + module my-elasticsearch-app + + require github.com/elastic/go-elasticsearch/v8 main +END + +cat > main.go <<-END + package main + + import ( + "log" + + "github.com/elastic/go-elasticsearch/v8" + ) + + func main() { + es, _ := elasticsearch.NewDefaultClient() + log.Println(elasticsearch.Version) + log.Println(es.Info()) + } +END + +go run main.go +``` + + +## {{es}} version compatibility [_es_version_compatibility] + +The language clients are forward compatible; meaning that the clients support communicating with greater or equal minor versions of {{es}} without breaking. It does not mean that the clients automatically support new features of newer {{es}} versions; it is only possible after a release of a new client version. For example, a 8.12 client version won’t automatically support the new features of the 8.13 version of {{es}}, the 8.13 client version is required for that. {{es}} language clients are only backwards compatible with default distributions and without guarantees made. + +| Elasticsearch Version | Elasticsearch-Go Branch | Supported | +| --- | --- | --- | +| main | main | | +| 8.x | 8.x | 8.x | +| 7.x | 7.x | 7.17 | + diff --git a/.doc/typedapi/queries.asciidoc b/docs/reference/runningqueries.md similarity index 66% rename from .doc/typedapi/queries.asciidoc rename to docs/reference/runningqueries.md index 48861c9a1b..8f7a80ac85 100644 --- a/.doc/typedapi/queries.asciidoc +++ b/docs/reference/runningqueries.md @@ -1,13 +1,15 @@ -[[runningqueries]] -=== Running Queries +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/runningqueries.html +--- -==== Request structures +# Running queries [runningqueries] -Each endpoint comes with a Request type that represents the body of its request. -For example, a simple search request for a term "Foo" in the `name` field could be written like this: +## Request structures [_request_structures] -[source,go] ------ +Each endpoint comes with a Request type that represents the body of its request. For example, a simple search request for a term "Foo" in the `name` field could be written like this: + +```go search.Request{ Query: &types.Query{ Term: map[string]types.TermQuery{ @@ -15,13 +17,14 @@ search.Request{ }, }, } ------ +``` + -==== Raw JSON +## Raw JSON [_raw_json] Lastly if you want to use your own pre-baked JSON queries using templates or even a specific encoder, you can pass the body directly to the `Raw` method of the endpoint: -[source,go] ------ + +```go es.Search().Raw([]byte(`{ "query": { "term": { @@ -32,6 +35,8 @@ es.Search().Raw([]byte(`{ } } }`)) ------ +``` No further validation or serialization is done on what is sent through this method, setting a payload with this takes precedence over any request structure you may submit before running the query. + + diff --git a/docs/reference/toc.yml b/docs/reference/toc.yml new file mode 100644 index 0000000000..277d50debb --- /dev/null +++ b/docs/reference/toc.yml @@ -0,0 +1,14 @@ +toc: + - file: index.md + # TO DO: Do we want these to be separate pages? + - file: getting-started.md + - file: getting-started-serverless.md + - file: installation.md + - file: connecting.md + - file: typed-api.md + children: + - file: _getting_started_with_the_api.md + - file: conventions.md + - file: runningqueries.md + - file: esql.md + - file: examples.md diff --git a/docs/reference/typed-api.md b/docs/reference/typed-api.md new file mode 100644 index 0000000000..98c5f1386a --- /dev/null +++ b/docs/reference/typed-api.md @@ -0,0 +1,18 @@ +--- +mapped_pages: + - https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/typedapi.html +--- + +# Typed API [typedapi] + +The goal for this API is to provide a strongly typed Golang API for {{es}}. + +This was designed with structures and the Golang runtime in mind, following as closely as possible the API and its objects. + +The first version focuses on the requests and does not yet include NDJSON endpoints such as `bulk` or `msearch`. These will be added later on along with typed responses and error handling. + + + + + +