我正在使用NEST(.NET 4.8)导入数据,但是在使映射在NEST 7.0.0-alpha1中正常工作时遇到了问题。
我具有以下类结构:
class LinkActor
{
public Actor Actor { get; set; }
}
abstract class Actor
{
public string Description { get; set; }
}
class Person : Actor
{
public string Name { get; set; }
}
我以这种方式连接到Elasticsearch:
var connectionSettings = new ConnectionSettings(new Uri(connection));
connectionSettings.DefaultIndex(indexName);
var client = new ElasticClient(connectionSettings);
实际数据如下:
var personActor = new Person
{
Description = "Description",
Name = "Name"
};
var linkActor = new LinkActor
{
Actor = personActor
};
数据被这样索引:
result = client.IndexDocument(linkActor);
使用NEST 6.6,我在Elasticsearch 6.5.2中获得以下数据:
"actor": {
"name": "Name",
"description": "Description"
}
但是,当使用NEST 7.0.0-alpha1时,我在Elasticsearch 7.0.0中获得了以下数据:
"actor": {
"description": "Description"
}
因此缺少具体类中的数据。我显然缺少/不了解某些新的映射功能,但是我对AutoMap的尝试失败了:
client.Map<(attempt with each of the above classes)>(m => m.AutoMap());
在NEST 7.0.0-alpha1中仍然可以映射具体类中的数据吗?
答案 0 :(得分:1)
我找到了使用NEST.JsonNetSerializer的解决方法(记住要安装它),这使我可以直接传递JObject:
使用池连接到Elasticsearch,以便您可以添加JsonNetSerializer。默认值:
var pool = new SingleNodeConnectionPool(new Uri(connection));
var connectionSettings = new ConnectionSettings(pool, JsonNetSerializer.Default);
connectionSettings.DefaultIndex(indexName);
var client = new ElasticClient(connectionSettings);
从上方将linkActor对象转换为JObject(为清楚起见,省略了JsonSerializerSettings,将它们添加以获取CamelCasing):
var linkActorSerialized = JsonConvert.SerializeObject(linkActor);
var linkActorJObject = JObject.Parse(linkActorSerialized);
result = client.IndexDocument(linkActorJObject);
这给出了预期的结果:
"actor": {
"name": "Name",
"description": "Description"
}
这是一种解决方法,希望有人能够解释问题中的映射。