# Best practice for handling \_ids in get and search results

**URL:** <https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500>\
**Category:** Elasticsearch\
**Created:** [November 30, 2021, 1:15am UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500 "2021-11-30T01:15:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davestewart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davestewart/32/97621_2.png) [@davestewart](https://discuss.elastic.co/u/davestewart)\
**Post date:** [November 30, 2021, 1:15am UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/1 "2021-11-30T01:15:17Z")

</div>

Hello,

Frontend engineer here, slowly improving at using Elasticsearch.

Does anyone have any advice on how to best work with document `_id`s, specifically with regards to sending them to the front end after a `_search` or `_get` along with the `_source` data?

The application I've inherited has had various approaches applied; stored copies of the `_id` as `_source.id`, mixing in `_id` as part of the returned results, etc, etc.

Note that we have our own Express back end which calls Elasticsearch, then sends flattened results to the front end like so:

```auto
{ id: hit._id, ...hit._source }

```

Are there any best practices, specifically with regards to a fairly general CRUD app?

Clearly we need the `_id` in order to make updates, and we would like to find a balance between sending back clean data we can pass around the app, and not writing code which would make a more experienced Elastic practitioner wince.

It is clearly not as straightforward as it would have been with SQL.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [November 30, 2021, 1:34am UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/2 "2021-11-30T01:34:01Z")

</div>

I am no expert in this coding at all. but just use python to update few million document and added a field to it. used \_id to update.

1. used elasticsearch\_dsl to retrieve only part of data from elasticsearch.
2. calculated new field.
3. created update document and use elasticsearch bulk update to update document on same index where I am reading from.

for example

from elasticsearch import Elasticsearch, helpers  
from elasticsearch\_dsl import Search

elastic\_output = Elasticsearch([hostnames], http\_auth=('elastic', 'elastic'), port=9200  
client = Elasticsearch([hostnames], http\_auth=('elastic', 'elastic'), port=9200)

##### test using one job, once done please remove .query\*

s = Search(using=client, index=input\_index).query("match", job="4139835950")  
search = s.source(["job", "minutes", "nodes", "exechost"])

this will return four value. for that job#. which is also my \_id

Then I created dictionary record like this  
mylist = [{"\_index": "your\_index", "\_id": "4139835950", "\_op\_type": "update", "\_source": {"my\_new\_field": "9.98654"}}]

and created list of such entry. once list is 5000 count I used bulk helper to write back to index

helpers.bulk(elastic\_output, mylist)

---

<div class="post-metadata">

**Author:** ![davestewart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davestewart/32/97621_2.png) [@davestewart](https://discuss.elastic.co/u/davestewart)\
**Post date:** [November 30, 2021, 2:51pm UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/3 "2021-11-30T14:51:43Z")

</div>

Thanks for the reply @elasticforme.

I'm good with the search and update APIs, the thrust of my question was really how folks structure the data returned from an Elastic `_search` or `_get` to be consumed by the front end application.

Do they:

1 ) Pass back the whole hit and reference `_id` and `_src` separately in the frontend code, i.e. `model._id` and `model._source.name`:

```auto
{
  "_index" : "contacts",
  "_type" : "_doc",
  "_id" : "VBj7bH0Bk8QNffIJSaXC",
  "_score" : 1.0,
  "_source" : {
    "name" : "Some contact",
    "phone" : "123456789"
  }
}

```

2 ) Merge the `_id` into `_src` and return that, , referencing it all as one object i.e. `model._id` and `model.name`:

```auto
{
  "_id" : "VBj7bH0Bk8QNffIJSaXC",
  "name" : "New contact",
  "phone" : "123456789"
}

```

3 ) Something else, for example passing `_id` as `id` ?

```auto
{
  "id" : "VBj7bH0Bk8QNffIJSaXC",
  "name" : "New contact",
  "phone" : "123456789"
}

```

As mentioned, I just don't want to start reinventing the wheel or introducing bad practices.

Admittedly, this kind of thing is always a balance between back and front end constraints and motivations.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [November 30, 2021, 2:56pm UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/4 "2021-11-30T14:56:31Z")

</div>

> [@davestewart](#):
>
> ```auto
> {
> "_index" : "contacts",
> "_type" : "_doc",
> "_id" : "VBj7bH0Bk8QNffIJSaXC",
> "_score" : 1.0,
> "_source" : {
> "name" : "Some contact",
> "phone" : "123456789"
> }
> }
> 
> ```

This is correct method for updating document.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [November 30, 2021, 2:59pm UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/5 "2021-11-30T14:59:08Z")

</div>

I have three index with 100 to 150 Million record in each these are 2018, 2019 and 2020 data and now I needed to add a field in to each record.

just finish 2018 index. took me 7 hour for extraction and update of all document in a index.

same way that you have explain in your first example.

---

<div class="post-metadata">

**Author:** ![davestewart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davestewart/32/97621_2.png) [@davestewart](https://discuss.elastic.co/u/davestewart)\
**Post date:** [November 30, 2021, 4:19pm UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/6 "2021-11-30T16:19:52Z")

</div>

> [@elasticforme](#):
>
> This is correct method for updating document.

I'm talking about the data passed from Elastic to the front end application (perhaps in a `_search` reequest) not the data passed to Elastic via an `_update` request. I shall update the post to disambiguate.

---

<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:** [December 28, 2021, 4:19pm UTC](https://discuss.elastic.co/t/best-practice-for-handling-ids-in-get-and-search-results/290500/7 "2021-12-28T16:19:55Z")

</div>

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