Skip to content

Commit

Permalink
[esutil.Client] Return Elasticsearch update response (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexashley committed Jun 9, 2021
1 parent 0bb1825 commit 7aae484
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go/v1beta1/storage/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (es *ElasticsearchStorage) UpdateOccurrence(ctx context.Context, projectId,
}
fieldmask_utils.StructToStruct(m, o, occurrence)

err = es.client.Update(ctx, &esutil.UpdateRequest{
_, err = es.client.Update(ctx, &esutil.UpdateRequest{
Index: es.occurrencesAlias(projectId),
DocumentId: targetDocumentID,
Message: proto.MessageV2(occurrence),
Expand Down
2 changes: 1 addition & 1 deletion go/v1beta1/storage/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ var _ = Describe("elasticsearch storage", func() {

JustBeforeEach(func() {
client.SearchReturns(expectedSearchResponse, expectedSearchError)
client.UpdateReturns(expectedUpdateError)
client.UpdateReturns(nil, expectedUpdateError)
actualOccurrence, actualErr = elasticsearchStorage.UpdateOccurrence(context.Background(), expectedProjectId, expectedOccurrenceId, occurrencePatchData, fieldMask)
})

Expand Down
14 changes: 7 additions & 7 deletions go/v1beta1/storage/esutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Client interface {
MultiSearch(ctx context.Context, request *MultiSearchRequest) (*EsMultiSearchResponse, error)
Get(ctx context.Context, request *GetRequest) (*EsGetResponse, error)
MultiGet(ctx context.Context, request *MultiGetRequest) (*EsMultiGetResponse, error)
Update(ctx context.Context, request *UpdateRequest) error
Update(ctx context.Context, request *UpdateRequest) (*EsIndexDocResponse, error)
Delete(ctx context.Context, request *DeleteRequest) error
}

Expand Down Expand Up @@ -478,11 +478,11 @@ func (c *client) MultiGet(ctx context.Context, request *MultiGetRequest) (*EsMul
return &response, nil
}

func (c *client) Update(ctx context.Context, request *UpdateRequest) error {
func (c *client) Update(ctx context.Context, request *UpdateRequest) (*EsIndexDocResponse, error) {
log := c.logger.Named("Update")
str, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(request.Message)
if err != nil {
return err
return nil, err
}

if request.Refresh == "" {
Expand All @@ -497,20 +497,20 @@ func (c *client) Update(ctx context.Context, request *UpdateRequest) error {
c.esClient.Index.WithRefresh(request.Refresh),
)
if err != nil {
return err
return nil, err
}
if res.IsError() {
return fmt.Errorf("unexpected response from elasticsearch: %s", res.String())
return nil, fmt.Errorf("unexpected response from elasticsearch: %s", res.String())
}

esResponse := EsIndexDocResponse{}
if err := DecodeResponse(res.Body, &esResponse); err != nil {
return err
return nil, err
}

log.Debug("elasticsearch response", zap.Any("response", esResponse))

return nil
return &esResponse, nil
}

func (c *client) Delete(ctx context.Context, request *DeleteRequest) error {
Expand Down
9 changes: 7 additions & 2 deletions go/v1beta1/storage/esutil/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,8 @@ var _ = Describe("elasticsearch client", func() {

Context("Update", func() {
var (
actualErr error
actualResponse *EsIndexDocResponse
actualErr error

expectedUpdateRequest *UpdateRequest
expectedDocumentId string
Expand Down Expand Up @@ -995,7 +996,7 @@ var _ = Describe("elasticsearch client", func() {
})

JustBeforeEach(func() {
actualErr = client.Update(ctx, expectedUpdateRequest)
actualResponse, actualErr = client.Update(ctx, expectedUpdateRequest)
})

It("should index (update) the document in ES", func() {
Expand All @@ -1014,6 +1015,10 @@ var _ = Describe("elasticsearch client", func() {
Expect(transport.ReceivedHttpRequests[0].URL.Query().Get("refresh")).To(Equal("true"))
})

It("should return the response from Elasticsearch", func() {
Expect(actualResponse.Id).To(Equal(expectedDocumentId))
})

It("should return no error", func() {
Expect(actualErr).ToNot(HaveOccurred())
})
Expand Down
33 changes: 19 additions & 14 deletions go/v1beta1/storage/esutil/esutilfakes/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7aae484

Please sign in to comment.