# How can I get the document meta data after saving a document in elasticsearch?

**URL:** https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668
**Category:** Elasticsearch
**Created:** [October 9, 2023, 2:11pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668 "2023-10-09T14:11:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Mertozturkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mertozturkk/32/126371_2.png) [@Mertozturkk](https://discuss.elastic.co/u/Mertozturkk)
#### Post date: [October 9, 2023, 2:11pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/1 "2023-10-09T14:11:50Z")

</div>

I am saving documents to an index in Elasticsearch using the bulk API in a Python project. However, what I need is how can I return the created id of the document after this process?

```auto
success, failed = bulk(client, actions, refresh=True)

            result = {
                "success_count": success,
                "failed_count": len(failed),
                "success_data": actions if success == len(actions) else [],
                "failed_data": failed
            }

```

---

<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: [October 10, 2023, 8:42am UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/2 "2023-10-10T08:42:35Z")

</div>

Hi @Mertozturkk,

Wwlcome to the community! Have you checked the response body of the build request?

Looking at the [bulk API documentation there is an items collection](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-api-response-body) in the response where the impacted document id for each action is included.

Can you take a look and see if you can extract that from the response?

---

<div class="post-metadata">

### Author: ![Mertozturkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mertozturkk/32/126371_2.png) [@Mertozturkk](https://discuss.elastic.co/u/Mertozturkk)
#### Post date: [October 10, 2023, 9:10am UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/3 "2023-10-10T09:10:33Z")

</div>

Yes, the information in the documentation is exactly what I need. However, when I run the code on the source code side in the Python library, what I see is that the data I need remains in the code and it only returns me the number of successful operations. If there is nothing else I missed about this, I will create a solution by contributing to the source code.

---

<div class="post-metadata">

### Author: ![Mertozturkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mertozturkk/32/126371_2.png) [@Mertozturkk](https://discuss.elastic.co/u/Mertozturkk)
#### Post date: [October 10, 2023, 7:38pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/4 "2023-10-10T19:38:32Z")

</div>

Actually, the information I want is in the item in the source code, but I cannot solve the problem because it is not returned, how can I proceed? I wanted to open a PR, I can get the information with a simple method, but I think there is a process that takes a little longer in the background.

```auto
success, failed = 0, 0

    # list of errors to be collected is not stats_only
    errors = []

    # make streaming_bulk yield successful results so we can count them
    kwargs["yield_ok"] = True
    for ok, item in streaming_bulk(
        client, actions, ignore_status=ignore_status, *args, **kwargs # type: ignore[misc]
    ):
        # go through request-response pairs and detect failures
        if not ok:
            if not stats_only:
                errors.append(item)
            failed += 1
        else:
            success += 1

    return success, failed if stats_only else errors

```

Elasticsearch version (8.10.0):

elasticsearch-py version (8.10.0):

---

<div class="post-metadata">

### Author: ![iulia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iulia/32/124658_2.png) [@iulia](https://discuss.elastic.co/u/iulia)
#### Post date: [October 11, 2023, 5:51pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/5 "2023-10-11T17:51:07Z")

</div>

You can get the IDs as you bulk index documents:

```auto
for ok, document in streaming_bulk(client, actions=actions, index="test-index"):
    print(document["index"]["_id"])

```

Alternatively, you can specify the IDs you want for the documents yourself so you don't have to retrieve them after the fact. Here's an example of that with a custom iterable function:

> <https://github.com/elastic/elasticsearch-py/blob/main/examples/bulk-ingest/bulk-ingest.py>

---

<div class="post-metadata">

### Author: ![Mertozturkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mertozturkk/32/126371_2.png) [@Mertozturkk](https://discuss.elastic.co/u/Mertozturkk)
#### Post date: [October 11, 2023, 6:09pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/6 "2023-10-11T18:09:34Z")

</div>

Thank you, I missed the values that streaming bulk returns, this will make my job easier.

---

<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: [November 8, 2023, 6:09pm UTC](https://discuss.elastic.co/t/how-can-i-get-the-document-meta-data-after-saving-a-document-in-elasticsearch/344668/7 "2023-11-08T18:09:46Z")

</div>

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