2021-08-25 14:05:24 -04:00
---
layout: default
title: Go client
2022-10-11 12:08:28 -04:00
nav_order: 50
2021-08-25 14:05:24 -04:00
---
# Go client
2021-09-20 16:29:58 -04:00
The OpenSearch Go client lets you connect your Go application with the data in your OpenSearch cluster.
2021-08-26 14:33:21 -04:00
2021-08-25 14:05:24 -04:00
## Setup
2022-09-06 10:58:15 -04:00
If you're starting a new project, create a new module by running the following command:
2021-08-25 14:05:24 -04:00
```go
2022-09-06 10:58:15 -04:00
go mod init < mymodulename >
2021-08-26 14:39:32 -04:00
```
2022-09-06 10:58:15 -04:00
To add the Go client to your project, import it like any other module:
2021-08-26 14:39:32 -04:00
```go
2021-08-25 14:05:24 -04:00
go get github.com/opensearch-project/opensearch-go
```
2022-09-06 10:58:15 -04:00
## Connecting to OpenSearch
2021-08-25 14:05:24 -04:00
2022-09-06 10:58:15 -04:00
To connect to the default OpenSearch host, create a client object with the address `https://localhost:9200` if you are using the Security plugin:
2021-08-25 14:05:24 -04:00
2022-09-06 10:58:15 -04:00
```go
client, err := opensearch.NewClient(opensearch.Config{
Transport: & http.Transport{
TLSClientConfig: & tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{"https://localhost:9200"},
Username: "admin", // For testing only. Don't store credentials in code.
Password: "admin",
})
```
If you are not using the Security plugin, create a client object with the address `http://localhost:9200` :
```go
client, err := opensearch.NewClient(opensearch.Config{
Transport: & http.Transport{
TLSClientConfig: & tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{"http://localhost:9200"},
})
```
The Go client constructor takes an `opensearch.Config{}` type, which can be customized using options such as a list of OpenSearch node addresses or a username and password combination.
To connect to multiple OpenSearch nodes, specify them in the `Addresses` parameter:
```go
var (
urls = []string{"http://localhost:9200", "http://localhost:9201", "http://localhost:9202"}
)
client, err := opensearch.NewClient(opensearch.Config{
Transport: & http.Transport{
TLSClientConfig: & tls.Config{InsecureSkipVerify: true},
},
Addresses: urls,
})
```
The Go client retries requests for a maximum of three times by default. To customize the number of retries, set the `MaxRetries` parameter. Additionally, you can change the list of response codes for which a request is retried by setting the `RetryOnStatus` parameter. The following code snippet creates a new Go client with custom `MaxRetries` and `RetryOnStatus` values:
```go
client, err := opensearch.NewClient(opensearch.Config{
Transport: & http.Transport{
TLSClientConfig: & tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{"http://localhost:9200"},
MaxRetries: 5,
RetryOnStatus: []int{502, 503, 504},
})
```
## Creating an index
To create an OpenSearch index, use the `IndicesCreateRequest` method. You can use the following code to construct a JSON object with custom settings :
```go
settings := strings.NewReader(`{
'settings': {
'index': {
'number_of_shards': 1,
'number_of_replicas': 0
}
}
}`)
res := opensearchapi.IndicesCreateRequest{
Index: "go-test-index1",
Body: settings,
}
```
## Indexing a document
You can index a document into OpenSearch using the `IndexRequest` method:
```go
document := strings.NewReader(`{
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011"
}`)
docId := "1"
req := opensearchapi.IndexRequest{
Index: "go-test-index1",
DocumentID: docId,
Body: document,
}
insertResponse, err := req.Do(context.Background(), client)
```
## Performing bulk operations
You can perform several operations at the same time by using the `Bulk` method of the client. The operations may be of the same type or of different types.
```go
blk, err := client.Bulk(
strings.NewReader(`
{ "index" : { "_index" : "go-test-index1", "_id" : "2" } }
{ "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"}
{ "create" : { "_index" : "go-test-index1", "_id" : "3" } }
{ "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"}
{ "update" : {"_id" : "3", "_index" : "go-test-index1" } }
{ "doc" : {"year" : "2016"} }
`),
)
```
## Searching for documents
The easiest way to search for documents is to construct a query string. The following code uses a `multi_match` query to search for "miller" in the title and director fields. It boosts the documents where "miller" is in the title field.
```go
content := strings.NewReader(`{
"size": 5,
"query": {
"multi_match": {
"query": "miller",
"fields": ["title^2", "director"]
}
}
}`)
search := opensearchapi.SearchRequest{
Index: []string{"go-test-index1"},
Body: content,
}
searchResponse, err := search.Do(context.Background(), client)
```
## Deleting a document
You can delete a document using the `DeleteRequest` method:
```go
delete := opensearchapi.DeleteRequest{
Index: "go-test-index1",
DocumentID: "1",
}
deleteResponse, err := delete.Do(context.Background(), client)
```
## Deleting an index
You can delete an index using the `IndicesDeleteRequest` method:
```go
deleteIndex := opensearchapi.IndicesDeleteRequest{
Index: []string{"go-test-index1"},
}
deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
```
## Sample program
This sample program creates a client, adds an index with non-default settings, inserts a document, performs bulk operations, searches for the document, deletes the document, and, finally, deletes the index:
2021-08-27 16:20:59 -04:00
2021-08-25 14:05:24 -04:00
```go
package main
import (
2021-08-27 17:00:26 -04:00
"os"
"context"
"crypto/tls"
"fmt"
opensearch "github.com/opensearch-project/opensearch-go"
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
"net/http"
"strings"
2021-08-25 14:05:24 -04:00
)
2021-08-26 14:33:21 -04:00
const IndexName = "go-test-index1"
func main() {
2021-08-27 17:00:26 -04:00
// Initialize the client with SSL/TLS enabled.
client, err := opensearch.NewClient(opensearch.Config{
Transport: & http.Transport{
TLSClientConfig: & tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{"https://localhost:9200"},
Username: "admin", // For testing only. Don't store credentials in code.
Password: "admin",
})
if err != nil {
fmt.Println("cannot initialize", err)
os.Exit(1)
}
// Print OpenSearch version information on console.
fmt.Println(client.Info())
2022-09-06 10:58:15 -04:00
// Define index settings.
settings := strings.NewReader(`{
2021-08-27 17:00:26 -04:00
'settings': {
'index': {
2022-09-06 10:58:15 -04:00
'number_of_shards': 1,
'number_of_replicas': 2
2021-08-26 14:33:21 -04:00
}
}
2021-08-27 17:00:26 -04:00
}`)
// Create an index with non-default settings.
2021-12-07 15:33:40 -05:00
res := opensearchapi.IndicesCreateRequest{
Index: IndexName,
2022-09-06 10:58:15 -04:00
Body: settings,
2021-08-27 17:00:26 -04:00
}
2022-09-06 10:58:15 -04:00
fmt.Println("Creating index")
fmt.Println(res)
2021-08-27 17:00:26 -04:00
// Add a document to the index.
document := strings.NewReader(`{
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011"
}`)
docId := "1"
req := opensearchapi.IndexRequest{
Index: IndexName,
DocumentID: docId,
Body: document,
}
insertResponse, err := req.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to insert document ", err)
os.Exit(1)
}
2022-09-06 10:58:15 -04:00
fmt.Println("Inserting a document")
2021-08-27 17:00:26 -04:00
fmt.Println(insertResponse)
2022-09-06 10:58:15 -04:00
// Perform bulk operations.
blk, err := client.Bulk(
strings.NewReader(`
{ "index" : { "_index" : "go-test-index1", "_id" : "2" } }
{ "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"}
{ "create" : { "_index" : "go-test-index1", "_id" : "3" } }
{ "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"}
{ "update" : {"_id" : "3", "_index" : "go-test-index1" } }
{ "doc" : {"year" : "2016"} }
`),
)
if err != nil {
fmt.Println("failed to perform bulk operations", err)
os.Exit(1)
}
fmt.Println("Performing bulk operations")
fmt.Println(blk)
2021-08-27 17:00:26 -04:00
// Search for the document.
content := strings.NewReader(`{
"size": 5,
"query": {
"multi_match": {
"query": "miller",
"fields": ["title^2", "director"]
}
}
}`)
search := opensearchapi.SearchRequest{
2022-09-06 10:58:15 -04:00
Index: []string{IndexName},
2021-08-27 17:00:26 -04:00
Body: content,
}
searchResponse, err := search.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to search document ", err)
os.Exit(1)
}
2022-09-06 10:58:15 -04:00
fmt.Println("Searching for a document")
2021-08-27 17:00:26 -04:00
fmt.Println(searchResponse)
// Delete the document.
delete := opensearchapi.DeleteRequest{
Index: IndexName,
DocumentID: docId,
}
deleteResponse, err := delete.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to delete document ", err)
os.Exit(1)
}
2022-09-06 10:58:15 -04:00
fmt.Println("Deleting a document")
2021-08-27 17:00:26 -04:00
fmt.Println(deleteResponse)
2022-09-06 10:58:15 -04:00
// Delete the previously created index.
2021-08-27 17:00:26 -04:00
deleteIndex := opensearchapi.IndicesDeleteRequest{
Index: []string{IndexName},
}
deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to delete index ", err)
os.Exit(1)
}
2022-09-06 10:58:15 -04:00
fmt.Println("Deleting the index")
fmt.Println(deleteIndexResponse)
2021-08-25 14:05:24 -04:00
}
```