spacing
This commit is contained in:
parent
12ce7e5fea
commit
9a2e6ed028
128
_clients/go.md
128
_clients/go.md
|
@ -6,7 +6,8 @@ nav_order: 80
|
|||
|
||||
# Go client
|
||||
|
||||
<>
|
||||
The OpenSearch Go client lets you programmatically interact with data in your OpenSearch cluster as part of your Go application.
|
||||
|
||||
|
||||
## Setup
|
||||
|
||||
|
@ -17,32 +18,26 @@ go mod init
|
|||
go get github.com/opensearch-project/opensearch-go
|
||||
```
|
||||
|
||||
### Sample response
|
||||
|
||||
```go
|
||||
go: downloading github.com/opensearch-project/opensearch-go v0.0.0-20210823214927-3567f157dece
|
||||
go get: added github.com/opensearch-project/opensearch-go v0.0.0-20210823214927-3567f157dece
|
||||
```
|
||||
|
||||
## Sample code
|
||||
|
||||
Create a new file called main.go:
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/opensearch-project/opensearch-go"
|
||||
"log"
|
||||
opensearch "github.com/opensearch-project/opensearch-go"
|
||||
opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main () {
|
||||
// Instantiate a new OpenSearch client object instance
|
||||
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},
|
||||
|
@ -51,43 +46,80 @@ func main () {
|
|||
Username: "admin", // For testing only. Don't store credentials in code.
|
||||
Password: "admin",
|
||||
})
|
||||
|
||||
// ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA. Optional client certificates if you don't want to use HTTP basic authentication.
|
||||
// client_cert_path = '/full/path/to/client.pem'
|
||||
// client_key_path = '/full/path/to/client-key.pem'
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("cannot initialize", err)
|
||||
}
|
||||
|
||||
// Have the client instance return a response
|
||||
|
||||
// Print OpenSearch version information.
|
||||
log.Println(client.Info())
|
||||
|
||||
// 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)
|
||||
}
|
||||
```
|
||||
|
||||
```go
|
||||
$ go run main.go
|
||||
|
||||
2021/08/24 23:56:05 [200 OK] {
|
||||
"name" : "c825aab1d9cc",
|
||||
"cluster_name" : "docker-cluster",
|
||||
"cluster_uuid" : "jsIDR0FTR5qXZr4XkZ-GlA",
|
||||
"version" : {
|
||||
"distribution" : "opensearch",
|
||||
"number" : "1.0.0",
|
||||
"build_type" : "tar",
|
||||
"build_hash" : "34550c5b17124ddc59458ef774f6b43a086522e3",
|
||||
"build_date" : "2021-07-02T23:22:21.383695Z",
|
||||
"build_snapshot" : false,
|
||||
"lucene_version" : "8.8.2",
|
||||
"minimum_wire_compatibility_version" : "6.8.0",
|
||||
"minimum_index_compatibility_version" : "6.0.0-beta1"
|
||||
},
|
||||
"tagline" : "The OpenSearch Project: https://opensearch.org/"
|
||||
}
|
||||
<nil>
|
||||
```
|
||||
|
||||
For all APIs -
|
||||
https://github.com/opensearch-project/opensearch-go/tree/main/opensearchapi
|
||||
|
|
Loading…
Reference in New Issue