Skip to content

Commit

Permalink
Updated opensearch to accomodate for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Apr 2, 2024
1 parent fb5e827 commit 826ade4
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions opensearch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand All @@ -28,9 +27,10 @@ func opensearchHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, opensearchConnector(opensearchConnectionStr))
}

func cleanOpensearchOutput(sr *opensearchapi.Response) string {
func cleanOpensearchOutput(sr *opensearchapi.SearchResp) string {
var mp map[string]interface{}
err := json.NewDecoder(sr.Body).Decode(&mp)
response := sr.Inspect().Response
err := json.NewDecoder(response.Body).Decode(&mp)
if err != nil {
fmt.Print(err)
}
Expand All @@ -55,7 +55,7 @@ func cleanOpensearchOutput(sr *opensearchapi.Response) string {
return opensearchOutput
}

func createOpensearchIndexDocument(client *opensearch.Client) {
func createOpensearchIndexDocument(client *opensearchapi.Client) {
settings := strings.NewReader(`{
'settings': {
'index': {
Expand All @@ -65,9 +65,15 @@ func createOpensearchIndexDocument(client *opensearch.Client) {
}
}`)

_ = opensearchapi.IndicesCreateRequest{
Index: "opensearch-test",
Body: settings,
_, err := client.Indices.Create(
ctx,
opensearchapi.IndicesCreateReq{
Index: "opensearch-test",
Body: settings,
},
)
if err != nil {
log.Println(err)
}

m := make(map[string]string)
Expand All @@ -81,24 +87,27 @@ func createOpensearchIndexDocument(client *opensearch.Client) {
reqDoc := strings.NewReader(string(jsonD))

docId := "1"
req := opensearchapi.IndexRequest{
Index: "opensearch-test",
DocumentID: docId,
Body: reqDoc,
}
_, err := req.Do(context.Background(), client)
_, err = client.Index(
ctx,
opensearchapi.IndexReq{
Index: "opensearch-test",
DocumentID: docId,
Body: reqDoc,
})
if err != nil {
log.Println(err)
}
time.Sleep(1 * time.Second)
}

func opensearchConnector(connectionString string) string {
client, _ := opensearch.NewClient(opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
client, _ := opensearchapi.NewClient(opensearchapi.Config{
Client: opensearch.Config{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Addresses: []string{connectionString},
},
Addresses: []string{connectionString},
})

createOpensearchIndexDocument(client)
Expand All @@ -110,12 +119,15 @@ func opensearchConnector(connectionString string) string {
}
}`)

searchReq := &opensearchapi.SearchRequest{
Index: []string{"opensearch-test"},
Body: content,
searchResponse, err := client.Search(
ctx, &opensearchapi.SearchReq{
Indices: []string{"opensearch-test"},
Body: content,
})
if err != nil {
log.Println(err)
}

searchResponse, _ := searchReq.Do(context.Background(), client)
openSearchResults := cleanOpensearchOutput(searchResponse)
return openSearchResults
}

0 comments on commit 826ade4

Please sign in to comment.