ES document's _id is not mapped to C# entity's Id
See original GitHub issueI have simplistic entity with property named “Id” of type String in my C# code. I also have an index with a number of documents uploaded via Bulk API. Document’s schema doesn’t include “id” field. The only thing that I have is a standard Elasticsearch’s “_id” property.
So when it gets to retrieving documents (var response = _esClient.Search<Poco>(s => s.MatchAll())
), I clearly see that Id has correct value in every object in response
’s Hits
array property.
However, when I do var documents = response.Documents.ToList()
, all my documents has Id
= null
.
What I’ve tried is setting IdProperty in ConnectionSettings - still no luck. So I don’t really see a way to map Elasticsearch’s “_id” to my entity’s Id property.
Description of the problem including expected versus actual behavior:
NEST/Elasticsearch.Net version: latest Elasticsearch version: latest
Steps to reproduce:
- Create Elasticsearch index with documents.
- Create simplest entity with Id property.
var documents = _esClient.Search<Poco>(s => s.MatchAll()).Documents.ToList()
ER: documents
is a list of Poco
s where each object has Id
property equal to corresponding document’s “_id” field in Elasticsearch.
AR: Id
= null
in all objects.
ConnectionSettings:
_settings =
new ConnectionSettings(_node)
.DefaultIndex("pocos")
.DefaultTypeName("doc")
.DefaultMappingFor<Poco>(
m => m
.IndexName("pocos")
.TypeName("doc")
.IdProperty(p => p.Id)); // this one doesn't work anyway
_esClient = new ElasticClient(_settings);
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top GitHub Comments
@maxsel It’s intentional behaviour and by design to not map the
_id
in the hit metadata returned for a hit to an Id property on a POCO during deserialization. It’s been discussed quite a few times before, the most recent is https://github.com/elastic/elasticsearch-net/issues/1293. It can be mapped withThoughts on revisiting this @Mpdreamz and @codebrain ? I have some thoughts about Elasticsearch autogenerated Ids that could tie in with this i.e. exposing the ability to configure on
ConnectionSettings
to_id
hit metadata to the POCO (on responses where this data is available).@codebrain I thought I noticed this behavior in a recent version of NEST. I remember having an Id field set automatically somehow, but it’s possible that I handled this with AutoMapper and forgot about it. Is there an equivalent for the Version property to automatically retrieve and store the version information without additional mapping?