From 19949ccd5276e484835d5148eb416f48d41d1b2e Mon Sep 17 00:00:00 2001 From: Alex Ashley Date: Fri, 18 Jun 2021 10:58:53 -0400 Subject: [PATCH] [esutil.Client] Expose generic multiget (#91) --- go/v1beta1/storage/esutil/client.go | 11 +++++++++-- go/v1beta1/storage/esutil/client_test.go | 11 +++++++++++ go/v1beta1/storage/esutil/types.go | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/go/v1beta1/storage/esutil/client.go b/go/v1beta1/storage/esutil/client.go index a6a08c8..08cf14f 100644 --- a/go/v1beta1/storage/esutil/client.go +++ b/go/v1beta1/storage/esutil/client.go @@ -464,10 +464,17 @@ func (c *client) MultiGet(ctx context.Context, request *MultiGetRequest) (*EsMul }) log = log.With(zap.String("request", requestJson)) + mgetOpts := []func(mgetRequest *esapi.MgetRequest){ + c.esClient.Mget.WithContext(ctx), + } + + if request.Index != "" { + mgetOpts = append(mgetOpts, c.esClient.Mget.WithIndex(request.Index)) + } + res, err := c.esClient.Mget( encodedBody, - c.esClient.Mget.WithContext(ctx), - c.esClient.Mget.WithIndex(request.Index), + mgetOpts..., ) if err != nil { return nil, err diff --git a/go/v1beta1/storage/esutil/client_test.go b/go/v1beta1/storage/esutil/client_test.go index 784029d..8f956cd 100644 --- a/go/v1beta1/storage/esutil/client_test.go +++ b/go/v1beta1/storage/esutil/client_test.go @@ -1000,6 +1000,17 @@ var _ = Describe("elasticsearch client", func() { }) }) + When("no index is specified", func() { + BeforeEach(func() { + expectedMultiGetRequest.Index = "" + }) + + It("should use the generic mget path", func() { + Expect(transport.ReceivedHttpRequests[0].Method).To(Equal(http.MethodGet)) + Expect(transport.ReceivedHttpRequests[0].URL.Path).To(Equal("/_mget")) + }) + }) + When("the multiget operation fails", func() { BeforeEach(func() { transport.PreparedHttpResponses = []*http.Response{ diff --git a/go/v1beta1/storage/esutil/types.go b/go/v1beta1/storage/esutil/types.go index 6b7c31d..472228e 100644 --- a/go/v1beta1/storage/esutil/types.go +++ b/go/v1beta1/storage/esutil/types.go @@ -161,6 +161,7 @@ type ESPitResponse struct { type EsMultiGetItem struct { Id string `json:"_id"` + Index string `json:"_index,omitempty"` Routing string `json:"routing,omitempty"` }