# Unknown error while bulk indexing

**URL:** https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [January 7, 2024, 5:20pm UTC](https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536 "2024-01-07T17:20:19Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mmaccou](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mmaccou](https://discuss.elastic.co/u/mmaccou)
#### Post date: [January 7, 2024, 5:20pm UTC](https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536/1 "2024-01-07T17:20:19Z")

</div>

I'm getting some errors during bulk indexing that I cant seem to extract the reason for. Below is my code. Do you see any issues? I feel like this should be pulling out the reason given the explanation in the docs.

```auto
async def bulk_index_to_elasticsearch(documents, index):
    try:
        async with await get_elasticsearch_client() as es:
            RETRY_ON_TIMEOUT = 3
            total_successes = 0
            errors = []
            failed_docs = []

            for doc in documents:
                action = {
                    "_op_type": "create",
                    "_index": index,
                    "_id": doc["_id"], # Using "_id" for Elasticsearch document ID
                    "_source": {key: value for key, value in doc.items() if key != "_id"}, # Include "id" in the source
                    "pipeline": "ml-inference-master" # Update if you have a different pipeline
                }

                for attempt in range(RETRY_ON_TIMEOUT):
                    try:
                        successes, bulk_errors = await helpers.async_bulk(es, [action], timeout="5m", raise_on_error=False)
                        total_successes += successes
                        if bulk_errors:
                            for error in bulk_errors:
                                error_details = error.get('error', {})
                                error_reason = error_details.get('reason', 'Unknown Error')

                                # Append the error reason to the errors list
                                errors.append(f"Error for document ID {doc['articleUrl']}: {error_reason}")
                                failed_docs.append(doc) # Add the document to the failed_docs list

                        break # Break the retry loop if successful or if an error other than timeout occurred
                    except ConnectionTimeout as e:
                        if attempt < RETRY_ON_TIMEOUT - 1:
                            logging.info(f"Timeout for document {doc['articleUrl']}. Retrying {attempt + 1}/{RETRY_ON_TIMEOUT}...")
                            continue
                        else:
                            errors.append(f"Connection timed out for document {doc['articleUrl']} after {RETRY_ON_TIMEOUT} attempts")
                            break
                    except Exception as e:
                        errors.append(f"Exception for document {doc['articleUrl']}: {str(e)}")
                        failed_docs.append(doc)
                        break
            if failed_docs:
                write_errors_to_csv(failed_docs)

        return total_successes, errors
    except Exception as e:
        logging.error(f"An error occurred in bulk_index_to_elasticsearch: {e}")
        return 0, [str(e)]

```

---

<div class="post-metadata">

### Author: ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)
#### Post date: [January 8, 2024, 6:11pm UTC](https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536/2 "2024-01-08T18:11:50Z")

</div>

Hi @mmaccou,

Which version of Elasticsearch and the language client are you using? Can you also share the error you are receiving?

---

<div class="post-metadata">

### Author: ![mmaccou](https://avatars.discourse-cdn.com/v4/letter/m/cab0a1/32.png) [@mmaccou](https://discuss.elastic.co/u/mmaccou)
#### Post date: [January 8, 2024, 7:04pm UTC](https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536/3 "2024-01-08T19:04:00Z")

</div>

Hi Carly. The version is v8.11.2. There is no error reason or details. My logs show "Unknown Error" which is my default. I cant identify anything strange with the data that is failing so it's kind of a mystery. It's definitely possible my code is wrong? But haven't been able to figure out how.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 5, 2024, 7:04pm UTC](https://discuss.elastic.co/t/unknown-error-while-bulk-indexing/350536/4 "2024-02-05T19:04:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
