Hello,
Currently creating a Lambda that calls the ES Search API, however accessing and decoding the body of the response is proving difficult. Here are a couple of different attempts I have made to access the body, the second was generated using the examples here:
Attempt One:
req := esapi.SearchRequest{
Index: []string{"REDACTED*"},
Size: &size,
Body: strings.NewReader(jsonQuery),
}
res, err := req.Do(context.Background(), es)
if err != nil {
fmt.Fprintf(os.Stderr, "\x1b[1;107;41mERROR: %s\x1b[0m\n", err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println(res.StatusCode)
fmt.Println(res.Header)
fmt.Println(res.Body)
fmt.Println("\n\n", res)
Output:
200
map[Content-Type:[application/json] Date:[Wed, 07 Jun 2023 01:31:07 GMT] X-Cloud-Request-Id:[GBZQBbobSkuKaP4_U0huyg] X-Elastic-Product:[Elasticsearch] X-Found-Handling-Cluster:[5586b11dd6a24977b3008de81193ede3] X-Found-Handling-Instance:[instance-0000000013]]
&{[] {0xc000094300} <nil> <nil>}
[200 OK] {"took":1392,"timed_out":false,"_shards":{"total":1853,"successful":1853,"skipped":1822,"failed":0},"hits":{"total":{"value":10000,"relation":"gte"},"max_score":0.0,"hits":REDACTED}}}}]}}
Attempt Two:
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex("REDACTED"),
es.Search.WithBody(strings.NewReader(jsonQuery)),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
fmt.Fprintf(os.Stderr, "\x1b[1;107;41mERROR: %s\x1b[0m\n", err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println(res.StatusCode)
fmt.Println(res.Header)
fmt.Println(res.Body)
fmt.Println("\n", res)
Output:
200
map[Content-Type:[application/json] Date:[Wed, 07 Jun 2023 01:34:40 GMT] X-Cloud-Request-Id:[SaGHppjtTRerD5AhUNtTvw] X-Elastic-Product:[Elasticsearch] X-Found-Handling-Cluster:[5586b11dd6a24977b3008de81193ede3] X-Found-Handling-Instance:[instance-0000000013]]
&{[] {0xc00009c300} }
[200 OK] {
"took" : 1821,
"timed_out" : false,
As you can see when printing the res.Body a pointer to an empty slice is returned, indicating there is no body. However, when printing the entire response there is the expected body. This seems to be breaking any attempt at decoding the body of a response, for example:
// if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
// fmt.Println("Error parsing the response body: %s", err)
// }
Is there something wrong in my initial searchRequest that is causing the body to not be accessible? There must be something I am missing here since the docs provide an example of how to do this..
Thank you