question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES document's _id is not mapped to C# entity's Id

See original GitHub issue

I 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:

  1. Create Elasticsearch index with documents.
  2. Create simplest entity with Id property.
  3. var documents = _esClient.Search<Poco>(s => s.MatchAll()).Documents.ToList()

ER: documents is a list of Pocos 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
russcamcommented, Aug 6, 2018

@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 with

var response = client.Search<Poco>(searchDescriptor);

var pocoListWithIds = response.Hits.Select(h =>
                {
                    h.Source.Id = h.Id;
                    return h.Source;
                }).ToList();

Thoughts 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

  1. Allow Elasticsearch to generate Ids for documents
  2. Allow _id hit metadata to the POCO (on responses where this data is available).
0reactions
D9001commented, Jan 31, 2020

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Entity Framework error - Error 11009: Property ' ' is not ...
For example, if a view has a column named "OrderID" and then the column name is changed to "OrderId" then it will give...
Read more >
Fluent API - Configuring and Mapping Properties and Types
Your class defines a property whose name is “ID” or “Id” ... Specifying Not to Map a CLR Entity Type to a Table...
Read more >
[Solved] org.hibernate.AnnotationException: No identifier ...
Recently I was working on a hibernate project and I have added few entity beans, when executed I got below exception stack trace....
Read more >
Unique Entity Identifier (UEI) Fact Sheet
If your organization is currently registered in SAM.gov with either an active or inactive registration, you have already been assigned a UEI.
Read more >
Hibernate ORM 6.0.0.CR1 User Guide
You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found