2021-08-25 11:05:24 -07:00
---
layout : default
title : Go client
nav_order : 80
---
# Go client
2021-09-20 13:29:58 -07:00
The OpenSearch Go client lets you connect your Go application with the data in your OpenSearch cluster.
2021-08-26 11:33:21 -07:00
2021-08-25 11:05:24 -07:00
## Setup
2021-08-26 11:39:32 -07:00
If you're creating a new project:
2021-08-25 11:05:24 -07:00
```go
go mod init
2021-08-26 11:39:32 -07:00
```
To add the client to your project, import it like any other module:
```go
2021-08-25 11:05:24 -07:00
go get github . com / opensearch - project / opensearch - go
```
## Sample code
2021-08-27 13:20:59 -07:00
This sample code creates a client, adds an index with non-default settings, inserts a document, searches for the document, deletes the document, and finally deletes the index:
2021-08-25 11:05:24 -07:00
```go
package main
import (
2021-08-27 14:00:26 -07: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 11:05:24 -07:00
)
2021-08-26 11:33:21 -07:00
const IndexName = "go-test-index1"
func main () {
2021-08-27 14:00:26 -07: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 )
}
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Print OpenSearch version information on console.
fmt . Println ( client . Info ())
2021-08-25 11:05:24 -07:00
2021-08-27 14:00:26 -07:00
// Define index mapping.
mapping := strings . NewReader ( `{
'settings': {
'index': {
2021-08-26 11:33:21 -07:00
'number_of_shards': 4
}
}
2021-08-27 14:00:26 -07:00
}` )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Create an index with non-default settings.
res := opensearchapi . CreateRequest {
Index : IndexName ,
Body : mapping ,
}
fmt . Println ( "creating index" , res )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Add a document to the index.
document := strings . NewReader ( `{
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011"
}` )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
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 )
}
fmt . Println ( insertResponse )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Search for the document.
content := strings . NewReader ( `{
"size": 5,
"query": {
"multi_match": {
"query": "miller",
"fields": ["title^2", "director"]
}
}
}` )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
search := opensearchapi . SearchRequest {
Body : content ,
}
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
searchResponse , err := search . Do ( context . Background (), client )
if err != nil {
fmt . Println ( "failed to search document " , err )
os . Exit ( 1 )
}
fmt . Println ( searchResponse )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Delete the document.
delete := opensearchapi . DeleteRequest {
Index : IndexName ,
DocumentID : docId ,
}
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
deleteResponse , err := delete . Do ( context . Background (), client )
if err != nil {
fmt . Println ( "failed to delete document " , err )
os . Exit ( 1 )
}
fmt . Println ( "deleting document" )
fmt . Println ( deleteResponse )
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
// Delete previously created index.
deleteIndex := opensearchapi . IndicesDeleteRequest {
Index : [] string { IndexName },
}
2021-08-26 11:33:21 -07:00
2021-08-27 14:00:26 -07:00
deleteIndexResponse , err := deleteIndex . Do ( context . Background (), client )
if err != nil {
fmt . Println ( "failed to delete index " , err )
os . Exit ( 1 )
}
fmt . Println ( "deleting index" , deleteIndexResponse )
2021-08-25 11:05:24 -07:00
}
```