# How to get the \_id of a document easily? \[C#\]

**URL:** https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [August 2, 2024, 9:57am UTC](https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260 "2024-08-02T09:57:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Motsols](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/motsols/32/134658_2.png) [@Motsols](https://discuss.elastic.co/u/Motsols)
#### Post date: [August 2, 2024, 9:57am UTC](https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260/1 "2024-08-02T09:57:50Z")

</div>

I'm indexing documents without an ID, letting ES set it by itself. How it is created nor the value is of any interest.

How can I easily get this \_id in the response document?

I'd very much love not to iterate over each hit just to set the ID in my response since it's considered metadata.

```csharp
var esResponse = await _client.SearchAsync<SearchBoundingBoxResult>(s => s
    .Query(q => q
        .GeoBoundingBox(b => b
            .Field(f => f.Geometry)
            .BoundingBox(box)
        )
    )
);

//return esResponse.Documents; // ID is null in the document

List<SearchBoundingBoxResult> searchResults = new();
foreach (var hit in esResponse.Hits)
{
    var doc = hit.Source!;
    doc.Id = hit.Id!;
    searchResults.Add(doc);
}

return searchResults;

```

And the response object looks like this (shortened for simplicity)

```csharp
public class SearchBoundingBoxResult
{
    public string Id { get; set; }
    public required string Name { get; set; }
}

```

Example of the raw json document in ES

```auto
{
  "_index": "paths",
  "_id": "4b5OEpEBiSFFgOLpCwAZ",
  "_version": 1,
  "_score": 0,
  "_source": {
    "name": "Bergslagsleden Stage 1: Kloten - Gillersklack",
    "geometry": {
      "type": "LineString",
      "coordinates": [
        [
          15.288221000000002,
          59.89521199999999
        ],
		[...]
      ]
    }
  },
  "fields": {
    "name": [
      "Bergslagsleden Stage 1: Kloten - Gillersklack"
    ],
    "geometry": [
      {
        "coordinates": [
          [
            15.288221000000002,
            59.89521199999999
          ],
		  [...]
        ],
        "type": "LineString"
      }
    ],
  }
}

```

Is there a simple solution to get the ID in my response document or is the only real option to create a GUID when creating the document?

---

<div class="post-metadata">

### Author: ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)
#### Post date: [August 2, 2024, 10:22am UTC](https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260/2 "2024-08-02T10:22:12Z")

</div>

Hi @Motsols,

I don't think there is a better solution for this. As you can see in the JSON response, `_id` is stored outside of the `_source` object, which is just the way ES works.

If you use `Elastic.Clients.Elasticsearch` for ingestion, you should indeed set the `Id` property of your object to something unique, like a GUID.

The client automatically checks, if the document has a property which is called `Id` (as well considers some variations) and uses this as the `_id` meta-data during ingestion, update operations, etc.

This way, the id is effectively stored twice: One time as the `_id` meta-data and another time inside the `id` field of the `_source` object.

Another solution could be to set-up an ingest pipeline that automatically populates the `id` property of your document with the value from the `_id` meta-data.

---

<div class="post-metadata">

### Author: ![Motsols](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/motsols/32/134658_2.png) [@Motsols](https://discuss.elastic.co/u/Motsols)
#### Post date: [August 2, 2024, 2:39pm UTC](https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260/3 "2024-08-02T14:39:02Z")

</div>

Thanks, good to know that I haven't missed anything.  
Then I'll go with the GUID approach as mentioned.

---

<div class="post-metadata">

### Author: ![Cular](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cular/32/85972_2.png) [@Cular](https://discuss.elastic.co/u/Cular)
#### Post date: [June 2, 2025, 6:02pm UTC](https://discuss.elastic.co/t/how-to-get-the-id-of-a-document-easily-c/364260/4 "2025-06-02T18:02:41Z")

</div>

Just in case, if someone will be here. There is solution for `Elastic.Clients.Elasticsearch` client.  
Use `searchResponse.Hits` instead of `searchResponse.Documents`:

```auto
if (searchResponse.Documents.Any())
{
    var documentHit = searchResponse.Hits.First();
    string id = documentHit.Id;
    Console.WriteLine($"Document _id: {id}");
}

```
