1

I am using NEST (.NET 4.8) to import my data, and I have a problem getting the mapping to work in NEST 7.0.0-alpha1.

I have the following class structure:

class LinkActor
{
    public Actor Actor { get; set; }
}

abstract class Actor
{
    public string Description { get; set; }
}

class Person : Actor
{
    public string Name { get; set; }
}

I connect to Elasticsearch this way:

var connectionSettings = new ConnectionSettings(new Uri(connection));
connectionSettings.DefaultIndex(indexName);
var client = new ElasticClient(connectionSettings);

The actual data looks like this:

var personActor = new Person
{
    Description = "Description",
    Name = "Name"
};

var linkActor = new LinkActor
{
    Actor = personActor
};

And the data is indexed like this:

result = client.IndexDocument(linkActor);

Using NEST 6.6 I am getting the following data in Elasticsearch 6.5.2:

"actor": {
    "name": "Name",
    "description": "Description"
}

However when using NEST 7.0.0-alpha1 I get the following data in Elasticsearch 7.0.0:

 "actor": {
      "description": "Description"
  }

So the data from the concrete class is missing. I am obviously missing / not understanding some new mapping feature, but my attempts with AutoMap has failed:

client.Map<(attempt with each of the above classes)>(m => m.AutoMap());

Is is still possible to map the data from the concrete class in NEST 7.0.0-alpha1?

2
  • 1
    I suspect this is because the property type on LinkActor is Actor and the serializer uses this as opposed to inspecting the runtime type assigned to the property. Would you mind opening an issue at github.com/elastic/elasticsearch-net/issues for this?
    – Russ Cam
    Apr 25, 2019 at 23:23
  • 1
    Hi @Russ Cam, Sorry for the late reply. I have opened up an issue here and created a repository where I have tried to make it easier to reproduce the problem. The repository is linked in the issue. Apr 29, 2019 at 16:37

1 Answer 1

1

I found a workaround using the NEST.JsonNetSerializer (remember to install this), which allows me to pass a JObject directly:

Connect to Elasticsearch using a pool so you can add the JsonNetSerializer.Default:

var pool = new SingleNodeConnectionPool(new Uri(connection));
var connectionSettings = new ConnectionSettings(pool, JsonNetSerializer.Default);
connectionSettings.DefaultIndex(indexName);
var client = new ElasticClient(connectionSettings);

Convert the linkActor object from above to a JObject (JsonSerializerSettings omitted for clarity, add them to get CamelCasing):

var linkActorSerialized = JsonConvert.SerializeObject(linkActor);
var linkActorJObject = JObject.Parse(linkActorSerialized);
result = client.IndexDocument(linkActorJObject);

This gives the desired result:

"actor": {
    "name": "Name",
    "description": "Description"
}

It is a workaround, hopefully someone will be able to explain the mapping in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.