Skip to content

Commit

Permalink
Adding a guide for making raw json requests using the client (#399)
Browse files Browse the repository at this point in the history
* Adding a guide for making raw json requests using the client

Signed-off-by: Vacha Shah <[email protected]>

* Add Changelog

Signed-off-by: Vacha Shah <[email protected]>

* samples: add sample files

Signed-off-by: Jakob Hahn <[email protected]>

---------

Signed-off-by: Vacha Shah <[email protected]>
Signed-off-by: Jakob Hahn <[email protected]>
Co-authored-by: Jakob Hahn <[email protected]>
  • Loading branch information
VachaShah and Jakob3xD authored Nov 21, 2023
1 parent 81200d8 commit b132d80
Show file tree
Hide file tree
Showing 10 changed files with 1,463 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adds govulncheck to check for go vulnerablities ([#405](https://github.com/opensearch-project/opensearch-go/pull/405))
- Adds opensearchapi with new client and function structure ([#421](https://github.com/opensearch-project/opensearch-go/pull/421))
- Adds integration tests for all opensearchapi functions ([#421](https://github.com/opensearch-project/opensearch-go/pull/421))
- Adds guide on making raw JSON REST requests ([#399](https://github.com/opensearch-project/opensearch-go/pull/399))

### Changed

Expand Down
21 changes: 21 additions & 0 deletions _samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OpenSearch Golang Samples

Most samples can be run using OpenSearch installed locally with docker.

Start container:

```
make cluster.build cluster.start
```

Stop and cleanup:

```
make cluster.stop cluster.clean
```

## Run Samples

```
go run _samples/json.go
```
195 changes: 195 additions & 0 deletions _samples/advanced_index_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
package main

import (
"context"
"fmt"
"os"
"strings"

"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
)

func main() {
if err := example(); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
}
}

func example() error {
client, err := opensearchapi.NewDefaultClient()
if err != nil {
return err
}

ctx := context.Background()
exampleIndex := "movies"

createResp, err := client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{Index: exampleIndex})
if err != nil {
return err
}
fmt.Printf("Index created: %t\n", createResp.Acknowledged)

clearCacheResp, err := client.Indices.ClearCache(ctx, &opensearchapi.IndicesClearCacheReq{Indices: []string{exampleIndex}})
if err != nil {
return err
}
fmt.Printf("Cach cleared for %d shards\n", clearCacheResp.Shards.Total)

clearCacheResp, err = client.Indices.ClearCache(
ctx,
&opensearchapi.IndicesClearCacheReq{
Indices: []string{exampleIndex},
Params: opensearchapi.IndicesClearCacheParams{
Fielddata: opensearchapi.ToPointer(true),
Request: opensearchapi.ToPointer(true),
Query: opensearchapi.ToPointer(true),
},
},
)
if err != nil {
return err
}
fmt.Printf("Cach cleared for %d shards\n", clearCacheResp.Shards.Total)

flushResp, err := client.Indices.Flush(ctx, &opensearchapi.IndicesFlushReq{Indices: []string{exampleIndex}})
if err != nil {
return err
}
fmt.Printf("Flushed shards: %d\n", flushResp.Shards.Total)

refreshResp, err := client.Indices.Refresh(ctx, &opensearchapi.IndicesRefreshReq{Indices: []string{exampleIndex}})
if err != nil {
return err
}
fmt.Printf("Refreshed shards: %d\n", refreshResp.Shards.Total)

closeResp, err := client.Indices.Close(ctx, opensearchapi.IndicesCloseReq{Index: exampleIndex})
if err != nil {
return err
}
fmt.Printf("Index closed: %t\n", closeResp.Acknowledged)

openResp, err := client.Indices.Open(ctx, opensearchapi.IndicesOpenReq{Index: exampleIndex})
if err != nil {
return err
}
fmt.Printf("Index opended: %t\n", openResp.Acknowledged)

mergeResp, err := client.Indices.Forcemerge(
ctx,
&opensearchapi.IndicesForcemergeReq{
Indices: []string{exampleIndex},
Params: opensearchapi.IndicesForcemergeParams{
MaxNumSegments: opensearchapi.ToPointer(1),
},
},
)
if err != nil {
return err
}
fmt.Printf("Forcemerged Shards: %d\n", mergeResp.Shards.Total)

blockResp, err := client.Indices.Block(
ctx,
opensearchapi.IndicesBlockReq{
Indices: []string{exampleIndex},
Block: "write",
},
)
if err != nil {
return err
}
fmt.Printf("Index write blocked: %t\n", blockResp.Acknowledged)

cloneResp, err := client.Indices.Clone(
ctx,
opensearchapi.IndicesCloneReq{
Index: exampleIndex,
Target: "movies_cloned",
},
)
if err != nil {
return err
}
fmt.Printf("Cloned: %t\n", cloneResp.Acknowledged)

settingResp, err := client.Indices.Settings.Put(
ctx,
opensearchapi.SettingsPutReq{
Indices: []string{exampleIndex},
Body: strings.NewReader(`{"index":{"blocks":{"write":null}}}`),
},
)
if err != nil {
return err
}
fmt.Printf("Settings updated: %t\n", settingResp.Acknowledged)

createResp, err = client.Indices.Create(
ctx,
opensearchapi.IndicesCreateReq{
Index: "books",
Body: strings.NewReader(`{
"settings": {
"index": {
"number_of_shards": 5,
"number_of_routing_shards": 30,
"blocks": {
"write": true
}
}
}
}`),
},
)
if err != nil {
return err
}
fmt.Printf("Index created: %t\n", createResp.Acknowledged)

splitResp, err := client.Indices.Split(
ctx,
opensearchapi.IndicesSplitReq{
Index: "books",
Target: "books-large",
Body: strings.NewReader(`{"settings":{"index":{"number_of_shards": 10}}}`),
},
)
if err != nil {
return err
}
fmt.Printf("Index splited: %t\n", splitResp.Acknowledged)

settingResp, err = client.Indices.Settings.Put(
ctx,
opensearchapi.SettingsPutReq{
Indices: []string{"books"},
Body: strings.NewReader(`{"index":{"blocks":{"write":null}}}`),
},
)
if err != nil {
return err
}
fmt.Printf("Settings updated: %t\n", settingResp.Acknowledged)

delResp, err := client.Indices.Delete(
ctx,
opensearchapi.IndicesDeleteReq{
Indices: []string{"movies*", "books*"},
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
},
)
if err != nil {
return err
}
fmt.Printf("Deleted: %t\n", delResp.Acknowledged)

return nil
}
Loading

0 comments on commit b132d80

Please sign in to comment.