From 7aed3bfbf1a1af94a0c21795bbc2165d9df14c09 Mon Sep 17 00:00:00 2001 From: ashwinkumar12345 Date: Fri, 27 Aug 2021 14:00:26 -0700 Subject: [PATCH] updated code to check error message --- _clients/go.md | 173 ++++++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 80 deletions(-) diff --git a/_clients/go.md b/_clients/go.md index 06b6e533..6db260ac 100644 --- a/_clients/go.md +++ b/_clients/go.md @@ -29,104 +29,117 @@ This sample code creates a client, adds an index with non-default settings, inse ```go package main - import ( - "context" - "crypto/tls" - "fmt" - opensearch "github.com/opensearch-project/opensearch-go" - opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" - "net/http" - "strings" + "os" + "context" + "crypto/tls" + "fmt" + opensearch "github.com/opensearch-project/opensearch-go" + opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi" + "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{ + 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) + } - // 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) - } + // Print OpenSearch version information on console. + fmt.Println(client.Info()) - // Print OpenSearch version information on console. - fmt.Println(client.Info()) - - // Define a mapping. - mapping := strings.NewReader(`{ - 'settings': { - 'index': { + // Define index 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) + // 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" - }`) + // 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) + 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) - // Search for the document. - content := strings.NewReader(`{ - "size": 5, - "query": { - "multi_match": { - "query": "miller", - "fields": ["title^2", "director"] - } - } - }`) + // Search for the document. + content := strings.NewReader(`{ + "size": 5, + "query": { + "multi_match": { + "query": "miller", + "fields": ["title^2", "director"] + } + } + }`) - search := opensearchapi.SearchRequest{ - Body: content, - } + search := opensearchapi.SearchRequest{ + Body: content, + } - searchResponse, err := search.Do(context.Background(), client) - fmt.Println(searchResponse) + 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. - delete := opensearchapi.DeleteRequest{ - Index: IndexName, - DocumentID: docId, - } + // Delete the document. + delete := opensearchapi.DeleteRequest{ + Index: IndexName, + DocumentID: docId, + } - deleteResponse, err := delete.Do(context.Background(), client) - fmt.Println("deleting document") - fmt.Println(deleteResponse) + 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. - deleteIndex := opensearchapi.IndicesDeleteRequest{ - Index: []string{IndexName}, - } + // Delete previously created index. + deleteIndex := opensearchapi.IndicesDeleteRequest{ + Index: []string{IndexName}, + } - deleteIndexResponse, err := deleteIndex.Do(context.Background(), client) - fmt.Println("deleting index") - fmt.Println(deleteIndexResponse) + 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) } ```