Go client

The OpenSearch Go client lets you connect your Go application with the data in your OpenSearch cluster.

Setup

If you’re creating a new project:

  1. go mod init

To add the client to your project, import it like any other module:

  1. go get github.com/opensearch-project/opensearch-go

Sample code

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:

  1. package main
  2. import (
  3. "os"
  4. "context"
  5. "crypto/tls"
  6. "fmt"
  7. opensearch "github.com/opensearch-project/opensearch-go"
  8. opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
  9. "net/http"
  10. "strings"
  11. )
  12. const IndexName = "go-test-index1"
  13. func main() {
  14. // Initialize the client with SSL/TLS enabled.
  15. client, err := opensearch.NewClient(opensearch.Config{
  16. Transport: &http.Transport{
  17. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  18. },
  19. Addresses: []string{"https://localhost:9200"},
  20. Username: "admin", // For testing only. Don't store credentials in code.
  21. Password: "admin",
  22. })
  23. if err != nil {
  24. fmt.Println("cannot initialize", err)
  25. os.Exit(1)
  26. }
  27. // Print OpenSearch version information on console.
  28. fmt.Println(client.Info())
  29. // Define index mapping.
  30. mapping := strings.NewReader(`{
  31. 'settings': {
  32. 'index': {
  33. 'number_of_shards': 4
  34. }
  35. }
  36. }`)
  37. // Create an index with non-default settings.
  38. res := opensearchapi.CreateRequest{
  39. Index: IndexName,
  40. Body: mapping,
  41. }
  42. fmt.Println("creating index", res)
  43. // Add a document to the index.
  44. document := strings.NewReader(`{
  45. "title": "Moneyball",
  46. "director": "Bennett Miller",
  47. "year": "2011"
  48. }`)
  49. docId := "1"
  50. req := opensearchapi.IndexRequest{
  51. Index: IndexName,
  52. DocumentID: docId,
  53. Body: document,
  54. }
  55. insertResponse, err := req.Do(context.Background(), client)
  56. if err != nil {
  57. fmt.Println("failed to insert document ", err)
  58. os.Exit(1)
  59. }
  60. fmt.Println(insertResponse)
  61. // Search for the document.
  62. content := strings.NewReader(`{
  63. "size": 5,
  64. "query": {
  65. "multi_match": {
  66. "query": "miller",
  67. "fields": ["title^2", "director"]
  68. }
  69. }
  70. }`)
  71. search := opensearchapi.SearchRequest{
  72. Body: content,
  73. }
  74. searchResponse, err := search.Do(context.Background(), client)
  75. if err != nil {
  76. fmt.Println("failed to search document ", err)
  77. os.Exit(1)
  78. }
  79. fmt.Println(searchResponse)
  80. // Delete the document.
  81. delete := opensearchapi.DeleteRequest{
  82. Index: IndexName,
  83. DocumentID: docId,
  84. }
  85. deleteResponse, err := delete.Do(context.Background(), client)
  86. if err != nil {
  87. fmt.Println("failed to delete document ", err)
  88. os.Exit(1)
  89. }
  90. fmt.Println("deleting document")
  91. fmt.Println(deleteResponse)
  92. // Delete previously created index.
  93. deleteIndex := opensearchapi.IndicesDeleteRequest{
  94. Index: []string{IndexName},
  95. }
  96. deleteIndexResponse, err := deleteIndex.Do(context.Background(), client)
  97. if err != nil {
  98. fmt.Println("failed to delete index ", err)
  99. os.Exit(1)
  100. }
  101. fmt.Println("deleting index", deleteIndexResponse)
  102. }