-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
- Loading branch information
Showing
8 changed files
with
1,270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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() | ||
|
||
movies := "movies" | ||
books := "books" | ||
|
||
createResp, err := client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{Index: movies}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Index created: %t\n", createResp.Acknowledged) | ||
|
||
createResp, err = client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{Index: books}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Index created: %t\n", createResp.Acknowledged) | ||
|
||
bulkResp, err := client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader(`{ "index": { "_index": "movies", "_id": 1 } } | ||
{ "title": "Beauty and the Beast", "year": 1991 } | ||
{ "index": { "_index": "movies", "_id": 2 } } | ||
{ "title": "Beauty and the Beast - Live Action", "year": 2017 } | ||
{ "index": { "_index": "books", "_id": 1 } } | ||
{ "title": "The Lion King", "year": 1994 } | ||
`), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
respAsJson, err := json.MarshalIndent(bulkResp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson)) | ||
|
||
bulkResp, err = client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader(`{ "create": { "_index": "movies" } } | ||
{ "title": "Beauty and the Beast 2", "year": 2030 } | ||
{ "create": { "_index": "movies", "_id": 1 } } | ||
{ "title": "Beauty and the Beast 3", "year": 2031 } | ||
{ "create": { "_index": "movies", "_id": 2 } } | ||
{ "title": "Beauty and the Beast 4", "year": 2049 } | ||
{ "create": { "_index": "books" } } | ||
{ "title": "The Lion King 2", "year": 1998 } | ||
`), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
respAsJson, err = json.MarshalIndent(bulkResp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson)) | ||
|
||
bulkResp, err = client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader(`{ "update": { "_index": "movies", "_id": 1 } } | ||
{ "doc": { "year": 1992 } } | ||
{ "update": { "_index": "movies", "_id": 1 } } | ||
{ "doc": { "year": 2018 } } | ||
`), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
respAsJson, err = json.MarshalIndent(bulkResp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson)) | ||
|
||
bulkResp, err = client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader(`{ "delete": { "_index": "movies", "_id": 1 } } | ||
{ "delete": { "_index": "movies", "_id": 2 } } | ||
`), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
respAsJson, err = json.MarshalIndent(bulkResp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson)) | ||
|
||
bulkResp, err = client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader(`{ "create": { "_index": "movies", "_id": 3 } } | ||
{ "title": "Beauty and the Beast 5", "year": 2050 } | ||
{ "create": { "_index": "movies", "_id": 4 } } | ||
{ "title": "Beauty and the Beast 6", "year": 2051 } | ||
{ "update": { "_index": "movies", "_id": 3 } } | ||
{ "doc": { "year": 2052 } } | ||
{ "delete": { "_index": "movies", "_id": 4 } } | ||
`), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
respAsJson, err = json.MarshalIndent(bulkResp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Bulk Resp:\n%s\n", string(respAsJson)) | ||
|
||
bulkResp, err = client.Bulk( | ||
ctx, | ||
opensearchapi.BulkReq{ | ||
Body: strings.NewReader("{\"delete\":{\"_index\":\"movies\",\"_id\":1}}\n"), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
for _, item := range bulkResp.Items { | ||
for operation, resp := range item { | ||
if resp.Status > 299 { | ||
fmt.Printf("Bulk %s Error: %s\n", operation, resp.Result) | ||
} | ||
} | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.