How do I get the _id which is the document id using NEST?

Hello dear friends, I have a question. I want to retrieve the _id of a document, how can I do that with using Nest?

The field I want to reach is the following "_id" field :
"_id" : "4912ba5cfb86ea63541c7007b1791ab3a6f0d54a",

"_index" : "people",
        "_type" : "_doc",
        "_id" : "4912ba5cfb86ea63541c7007b1791ab3a6f0d54a",
        "_score" : 1.0,
        "_source" : {
          "country" : "Turkey",
          "surname" : "Bayrak",
          "name" : "Esra",
          "email" : "esrabayrak@gmail.com",
          "age" : "88"
      }

For this I tried things like below. I used the Source plugin but I can't access the _id :
My model doesn't have a prop called id. Elasticsearch created the _id field itself.

string GetDocumentId(string Name) {
                        var searchResponse = elasticClient.Search<MyModelName>(s => s
                        .Index(people)
                        .Query(q => q
                            .Bool(b => b
                               .Must(
                                   m => m.Match(mm => mm.Field(f => f.name).Query("Esra"))
                               )))
                        );
                        string temp = searchResponse.Hits.Select(s => s.Id).ToString();
                        return temp;
                    }
  );

I would be very happy if you help. Thank you from now. :hugs:

The _id field is part of the document metadata and is the unique id for the document in the index. If an id is not provided when a document is indexed, Elasticsearch will generate an id for the document.

If Elasticsearch has generated an id for a given document, the generated id is returned on the index response. If the id is meaningful and will be used to look up a specific document, it's ideal to either

  1. store the generated id somewhere from which it can be retrieved in order to get the document from Elasticsearch

or

  1. pass a known id when indexing a document in Elasticsearch

Without the id, the best that can be done is to search for documents as in your example, and return the id. There's a couple of things to consider with this approach however:

  1. A document only becomes available to search queries once a refresh has occurred. This means a document that was just indexed might not be available to search and not appear in the results. In contrast, the Get API can get a document by id immediately after it is indexed.
  2. A search may return more than one matching document, which might add some complexity in determining which id to choose.

Since indexing documents with ids is common to do when interacting with Elasticsearch from NEST, if your document POCO has an Id property, it will be used to set the id for the document in Elasticsearch. With this convention, it does mean that the document id will be duplicated in the document _source with a field name of "id" (there are ways to avoid this, if this is an issue).

1 Like

Thank you for your reply :hugs:

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