2021-08-25 14:05:24 -04:00
|
|
|
---
|
|
|
|
layout: default
|
|
|
|
title: Go client
|
|
|
|
nav_order: 80
|
|
|
|
---
|
|
|
|
|
|
|
|
# Go client
|
|
|
|
|
2021-08-26 14:33:21 -04:00
|
|
|
The OpenSearch Go client lets you programmatically interact with data in your OpenSearch cluster as part of your Go application.
|
|
|
|
|
2021-08-25 14:05:24 -04:00
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
2021-08-26 14:39:32 -04:00
|
|
|
If you're creating a new project:
|
2021-08-25 14:05:24 -04:00
|
|
|
|
|
|
|
```go
|
|
|
|
go mod init
|
2021-08-26 14:39:32 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
To add the client to your project, import it like any other module:
|
|
|
|
|
|
|
|
```go
|
2021-08-25 14:05:24 -04:00
|
|
|
go get github.com/opensearch-project/opensearch-go
|
|
|
|
```
|
|
|
|
|
|
|
|
## Sample code
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-26 14:33:21 -04:00
|
|
|
"context"
|
2021-08-25 14:05:24 -04:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2021-08-26 14:33:21 -04:00
|
|
|
opensearch "github.com/opensearch-project/opensearch-go"
|
|
|
|
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
|
2021-08-25 14:05:24 -04:00
|
|
|
"net/http"
|
2021-08-26 14:33:21 -04:00
|
|
|
"strings"
|
2021-08-25 14:05:24 -04:00
|
|
|
)
|
|
|
|
|
2021-08-26 14:33:21 -04:00
|
|
|
const IndexName = "go-test-index1"
|
2021-08-25 14:05:24 -04:00
|
|
|
|
2021-08-26 14:33:21 -04:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Initialize the client with SSL/TLS enabled.
|
2021-08-25 14:05:24 -04:00
|
|
|
client, err := opensearch.NewClient(opensearch.Config{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
Addresses: []string{"https://localhost:9200"},
|
2021-08-26 14:33:21 -04:00
|
|
|
Username: "admin", // For testing only. Don't store credentials in code.
|
|
|
|
Password: "admin",
|
2021-08-25 14:05:24 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("cannot initialize", err)
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:41:09 -04:00
|
|
|
// Print OpenSearch version information on console.
|
|
|
|
fmt.Println(client.Info())
|
2021-08-25 14:05:24 -04:00
|
|
|
|
2021-08-26 14:33:21 -04:00
|
|
|
// Define a mapping.
|
|
|
|
mapping := strings.NewReader(`{
|
|
|
|
'settings': {
|
|
|
|
'index': {
|
|
|
|
'number_of_shards': 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
// Create an index with non-default settings.
|
|
|
|
res := opensearchapi.CreateRequest{
|
|
|
|
Index: IndexName,
|
|
|
|
Body: mapping,
|
|
|
|
}
|
|
|
|
fmt.Println("creating index", res)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
fmt.Println(insertResponse)
|
|
|
|
|
|
|
|
// Search for the document.
|
|
|
|
content := strings.NewReader(`{
|
|
|
|
"size": 5,
|
|
|
|
"query": {
|
|
|
|
"multi_match": {
|
|
|
|
"query": "miller",
|
|
|
|
"fields": ["title^2", "director"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
search := opensearchapi.SearchRequest{
|
|
|
|
Body: content,
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResponse, err := search.Do(context.Background(), client)
|
|
|
|
fmt.Println(searchResponse)
|
|
|
|
|
|
|
|
// Delete the document.
|
|
|
|
delete := opensearchapi.DeleteRequest{
|
|
|
|
Index: IndexName,
|
|
|
|
DocumentID: docId,
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteResponse, err := delete.Do(context.Background(), client)
|
|
|
|
fmt.Println("deleting document")
|
|
|
|
fmt.Println(deleteResponse)
|
|
|
|
|
|
|
|
// Delete the index.
|
|
|
|
deleteIndex := opensearchapi.IndicesDeleteRequest{
|
|
|
|
Index: []string{IndexName},
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
|
|
|
|
fmt.Println("deleting index")
|
|
|
|
fmt.Println(deleteIndexResponse)
|
2021-08-25 14:05:24 -04:00
|
|
|
}
|
|
|
|
```
|