Created
June 2, 2024 08:39
-
-
Save mexicantexan/299dcdc97dd433334a70bd2a5ef0d74e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go-v2/aws" | |
"github.com/aws/aws-sdk-go-v2/config" | |
"github.com/opensearch-project/opensearch-go/opensearchapi" | |
opensearch "github.com/opensearch-project/opensearch-go/v2" | |
requestsigner "github.com/opensearch-project/opensearch-go/v2/signer/awsv2" | |
) | |
func main() { | |
lambda.Start(HandleRequest) | |
} | |
func HandleRequest(ctx context.Context) (string, error) { | |
//ctx := context.Background() | |
endpoint := os.Getenv("OPENSEARCH_URL") | |
indexName := "go-test-index1" | |
awsCfg, err := config.LoadDefaultConfig(ctx, | |
config.WithRegion("us-west-2"), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Create an AWS request Signer and load AWS configuration using default config folder or env vars. | |
signer, err := requestsigner.NewSignerWithService(awsCfg, "aoss") | |
if err != nil { | |
log.Fatal(err) // Do not log.fatal in a production ready app. | |
} | |
// Create an opensearch client and use the request-signer | |
client, err := opensearch.NewClient(opensearch.Config{ | |
Addresses: []string{endpoint}, | |
Signer: signer, | |
}) | |
if err != nil { | |
log.Fatal("client creation err", err) | |
} | |
settings := strings.NewReader(`{ | |
'settings': { | |
'index': { | |
'number_of_shards': 1, | |
'number_of_replicas': 0 | |
} | |
} | |
}`) | |
fmt.Println(client.Info()) | |
res := opensearchapi.IndicesCreateRequest{ | |
Index: indexName, | |
Body: settings, | |
} | |
fmt.Println("Creating index") | |
fmt.Println(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("Inserting a document") | |
fmt.Println(insertResponse) | |
defer insertResponse.Body.Close() | |
// Perform bulk operations. | |
blk, err := client.Bulk( | |
strings.NewReader(` | |
{ "index" : { "_index" : "go-test-index1", "_id" : "2" } } | |
{ "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"} | |
{ "create" : { "_index" : "go-test-index1", "_id" : "3" } } | |
{ "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"} | |
{ "update" : {"_id" : "3", "_index" : "go-test-index1" } } | |
{ "doc" : {"year" : "2016"} } | |
`), | |
) | |
if err != nil { | |
fmt.Println("failed to perform bulk operations", err) | |
os.Exit(1) | |
} | |
fmt.Println("Performing bulk operations") | |
fmt.Println(blk) | |
// Search for the document. | |
content := strings.NewReader(`{ | |
"size": 5, | |
"query": { | |
"multi_match": { | |
"query": "miller", | |
"fields": ["title^2", "director"] | |
} | |
} | |
}`) | |
search := opensearchapi.SearchRequest{ | |
Index: []string{indexName}, | |
Body: content, | |
} | |
searchResponse, err := search.Do(context.Background(), client) | |
if err != nil { | |
fmt.Println("failed to search document ", err) | |
os.Exit(1) | |
} | |
fmt.Println("Searching for a document") | |
fmt.Println(searchResponse) | |
defer searchResponse.Body.Close() | |
// 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 a document") | |
fmt.Println(deleteResponse) | |
defer deleteResponse.Body.Close() | |
// Delete the 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 the index") | |
fmt.Println(deleteIndexResponse) | |
defer deleteIndexResponse.Body.Close() | |
return "Success", nil | |
} | |
func getCredentialProvider(accessKey, secretAccessKey, token string) aws.CredentialsProviderFunc { | |
return func(ctx context.Context) (aws.Credentials, error) { | |
c := &aws.Credentials{ | |
AccessKeyID: accessKey, | |
SecretAccessKey: secretAccessKey, | |
SessionToken: token, | |
} | |
return *c, nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment