Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Expecting error in bulk upserts #662

Closed
bentcoder opened this issue Feb 6, 2025 · 2 comments
Closed

[BUG] Expecting error in bulk upserts #662

bentcoder opened this issue Feb 6, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@bentcoder
Copy link

What is the bug?

To be fair I am not sure if this is a bug or an intentional design choice.

When I insert many docs in one go using Bulk Request API, there is no way of knowing how many docs have failed due to bad data they contain. For instance, if you insert a single doc (I know it is a bad doc) using non-bulk operation, you will get 400 error telling you what the problem was. However, bulk API carries on without an error.

How can one reproduce the bug?

// github.com/opensearch-project/opensearch-go/v4/opensearchapi
// Client: *opensearchapi.Client
func (o Opensearch) UpsertOffers(ctx context.Context, offers []*document.Offer) error {
	var body strings.Builder

	for _, offer := range offers {
		body.WriteString(fmt.Sprintf(`{"update":{"_id":"%s","_index":"%s"}}`, offer.Path, "my_index"))
		body.WriteString("\n")

		doc, err := json.Marshal(map[string]any{"doc": offer, "doc_as_upsert": true})
		if err != nil {
			return err
		}

		body.Write(doc)
		body.WriteString("\n")
	}

	req := opensearchapi.BulkReq{
		Body: strings.NewReader(body.String()),
	}

	res, err := o.Client.Client.Do(ctx, req, nil)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.IsError() {
		return fmt.Errorf("unexpected response: %s - %s", res.Status(), res.String())
	}

	return nil
}

What is the expected behavior?

At least res.IsError() or something could list failed docs.

@bentcoder bentcoder added bug Something isn't working untriaged labels Feb 6, 2025
@dblock
Copy link
Member

dblock commented Feb 6, 2025

Does the server side API return the numbers and info?

@dblock dblock removed the untriaged label Feb 6, 2025
@bentcoder
Copy link
Author

Actually the response body reports if there is an error but looks like I have to iterate objects one by one in order to extract errors which will probably be very costly.

{
  "took": 2,
  "errors": false,
  "items": [
        {
            "update": {
                "_index": "offers_20250122",
                "_id": "BV",
                "status": 400,
                "error": {
                    "type": "mapper_parsing_exception",
                    "reason": "failed to parse field [data.value] of type [long] in document with id 'BV'. Preview of field's value: '5,690'",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "For input string: \"5,690\""
                    }
                }
            }
        },
        {
            "update": {
                "_index": "offers_20250122",
                "_id": "OA3",
                "_version": 13,
                "result": "noop",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 226007,
                "_primary_term": 1,
                "status": 200
            }
        }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants