Compare commits

..

9 Commits

Author SHA1 Message Date
ashwinkumar12345 54a0b8e755 minor change 2021-09-20 13:29:58 -07:00
ashwinkumar12345 6bf8a7d51a minor change 2021-09-20 13:27:13 -07:00
ashwinkumar12345 05ba9198f6 modified description 2021-09-20 13:22:18 -07:00
ashwinkumar12345 7aed3bfbf1 updated code to check error message 2021-08-27 14:00:26 -07:00
ashwinkumar12345 fbc0447bcd added more context 2021-08-27 13:20:59 -07:00
ashwinkumar12345 1c4f81eb53 minor fix 2021-08-26 11:41:09 -07:00
ashwinkumar12345 bfc56f2f7f incorporated feedback 2021-08-26 11:39:32 -07:00
ashwinkumar12345 9a2e6ed028 spacing 2021-08-26 11:33:21 -07:00
ashwinkumar12345 12ce7e5fea rough draft 2021-08-25 11:05:24 -07:00
3 changed files with 147 additions and 10 deletions
+145
View File
@@ -0,0 +1,145 @@
---
layout: default
title: Go client
nav_order: 80
---
# 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:
```go
go mod init
```
To add the client to your project, import it like any other module:
```go
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:
```go
package main
import (
"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)
}
// Print OpenSearch version information on console.
fmt.Println(client.Info())
// 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)
// 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)
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 := opensearchapi.SearchRequest{
Body: content,
}
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,
}
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 previously created index.
deleteIndex := opensearchapi.IndicesDeleteRequest{
Index: []string{IndexName},
}
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)
}
```
+1 -1
View File
@@ -1,7 +1,7 @@
---
layout: default
title: Java high-level REST client
nav_order: 97
nav_order: 60
---
# Java high-level REST client
+1 -9
View File
@@ -54,7 +54,7 @@ body {
code {
@include monospace;
font-size: 0.8rem;
font-size: 0.75rem;
-webkit-font-smoothing: subpixel-antialiased;
-moz-osx-font-smoothing: auto;
}
@@ -231,13 +231,6 @@ p.label {
}
}
.site-footer {
display: none;
@include mq(md) {
display: block;
}
}
.main {
@include mq(xl) {
margin-left: calc((100% - #{$nav-width + $content-width + $toc-width}) / 2 + #{$nav-width});
@@ -467,7 +460,6 @@ main {
}
.main-content {
font-size: 1rem;
h1, h2, h3, h4, h5, h6 {
a {
@include link-alternate;