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
- store the generated id somewhere from which it can be retrieved in order to get the document from Elasticsearch
or
- 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:
- 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.
- 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).