updated code to check error message

This commit is contained in:
ashwinkumar12345 2021-08-27 14:00:26 -07:00
parent fbc0447bcd
commit 7aed3bfbf1
1 changed files with 93 additions and 80 deletions

View File

@ -29,8 +29,8 @@ This sample code creates a client, adds an index with non-default settings, inse
```go
package main
import (
"os"
"context"
"crypto/tls"
"fmt"
@ -39,11 +39,8 @@ import (
"net/http"
"strings"
)
const IndexName = "go-test-index1"
func main() {
// Initialize the client with SSL/TLS enabled.
client, err := opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
@ -55,12 +52,13 @@ func main() {
})
if err != nil {
fmt.Println("cannot initialize", err)
os.Exit(1)
}
// Print OpenSearch version information on console.
fmt.Println(client.Info())
// Define a mapping.
// Define index mapping.
mapping := strings.NewReader(`{
'settings': {
'index': {
@ -90,6 +88,10 @@ func main() {
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)
// Search for the document.
@ -108,6 +110,10 @@ func main() {
}
searchResponse, err := search.Do(context.Background(), client)
if err != nil {
fmt.Println("failed to search document ", err)
os.Exit(1)
}
fmt.Println(searchResponse)
// Delete the document.
@ -117,16 +123,23 @@ func main() {
}
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)
// Delete the index.
// Delete previously created index.
deleteIndex := opensearchapi.IndicesDeleteRequest{
Index: []string{IndexName},
}
deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
fmt.Println("deleting index")
fmt.Println(deleteIndexResponse)
if err != nil {
fmt.Println("failed to delete index ", err)
os.Exit(1)
}
fmt.Println("deleting index", deleteIndexResponse)
}
```