C# 索引父文档时的Elasticsearch空指针
我正在尝试在Elasticsearch中设置父子关系,并且在尝试索引父对象时,始终从服务器获取空指针异常C# 索引父文档时的Elasticsearch空指针,c#,.net,
elasticsearch,nest,C#,.net,
elasticsearch,Nest,我正在尝试在Elasticsearch中设置父子关系,并且在尝试索引父对象时,始终从服务器获取空指针异常 我在6.2版本中使用Elasticsearch和NEST 我基本上一直遵循文档中的指南: 对象:(ESDocument是基类,ESVendor是父类,ESLocation是子类) 映射+索引: StaticConnectionPool connectionPool = new StaticConnectionPool(_nodes); ConnectionSettings conne
我在6.2版本中使用Elasticsearch和NEST 我基本上一直遵循文档中的指南: 对象:(ESDocument是基类,ESVendor是父类,ESLocation是子类) 映射+索引:
StaticConnectionPool connectionPool = new StaticConnectionPool(_nodes);
ConnectionSettings connectionSettings = new ConnectionSettings(connectionPool, sourceSerializer: SourceSerializer)
.DisableDirectStreaming()
.DefaultMappingFor<ESDocument>(m => m.IndexName("vendors").TypeName("doc"))
.DefaultMappingFor<ESVendor>(m => m.IndexName("vendors").TypeName("doc").RelationName("Vendor_Location"))
.DefaultMappingFor<ESLocation>(m => m.IndexName("vendors").TypeName("doc"));
ElasticClient esClient = new ElasticClient(connectionSettings);
IExistsResponse existsResponse = await esClient.IndexExistsAsync(new IndexExistsRequest(Indices.Parse("vendors")));
if (!existsResponse.Exists)
{
esClient.CreateIndex("vendors", c => c
.Index<ESVendor>()
.Mappings(ms => ms
.Map<ESVendor>(m => m
.RoutingField(r => r.Required())
.AutoMap<ESVendor>()
.AutoMap<ESLocation>()
.Properties(props => props
.Join(j => j
.Name(p => p.MyJoinField)
.Relations(r => r
.Join<ESVendor, ESLocation>()
)
)
)
)
));
}
using (Entities dbContext = new Entities())
{
foreach (Vendor vendor in dbContext.Vendors)
{
if (!(await esClient.DocumentExistsAsync(new DocumentExistsRequest("vendors", typeof (Vendor), vendor.VendorID))).Exists)
{
ESVendor parent = new ESVendor(vendor)
{
MyJoinField = JoinField.Root<ESVendor>()
};
var result = esClient.IndexDocument<ESDocument>(parent);
ESLocation child = new ESLocation(vendor.Location)
{
MyJoinField = JoinField.Link<ESLocation, ESVendor>(parent)
};
result = esClient.IndexDocument<ESDocument>(child);
}
}
}
我已经为此奋斗了好几天,而这本应该是一项30分钟的任务,我已经束手无策了
提前谢谢你,非常感谢你的帮助 问题在于应用于
EsVendor
[ElasticsearchType(Name = "Vendor", IdProperty = "ConsigneeID")]
public class ESVendor: ESDocument
{
// ...
}
[ElasticsearchType(Name = "Vendor")]
public class ESVendor : ESDocument
{
// ...
}
PUT http://localhost:9200/vendors/doc/1?routing=1
{
"id": 1,
"vendorID": 1,
"myJoinField": "Vendor_Location"
}
IdProperty
告诉NEST从delicateID
属性推断EsVendor
类型的id,但是查看提供的模型,没有具有该名称的属性。看起来您不需要在属性中指定此属性,而是可以依赖应用于ESDocument
的ElasticsearchTypeAttribute
中的IdProperty
,这将从Id
属性推断Id
因此,从应用于EsVendor
[ElasticsearchType(Name = "Vendor", IdProperty = "ConsigneeID")]
public class ESVendor: ESDocument
{
// ...
}
[ElasticsearchType(Name = "Vendor")]
public class ESVendor : ESDocument
{
// ...
}
PUT http://localhost:9200/vendors/doc/1?routing=1
{
"id": 1,
"vendorID": 1,
"myJoinField": "Vendor_Location"
}
现在将为EsVendor
[ElasticsearchType(Name = "Vendor", IdProperty = "ConsigneeID")]
public class ESVendor: ESDocument
{
// ...
}
[ElasticsearchType(Name = "Vendor")]
public class ESVendor : ESDocument
{
// ...
}
PUT http://localhost:9200/vendors/doc/1?routing=1
{
"id": 1,
"vendorID": 1,
"myJoinField": "Vendor_Location"
}
对于引用属性的字符串值,我们可能也希望使用编译器,并使用nameof(Id)
事实上,如果需要,我们可以完全删除
IdProperty
,因为使用Id
属性推断文档的Id是默认的嵌套行为。更进一步,因为类型在连接设置
上有一个DefaultMappingFor
,我们可以完全删除ElasticsearchType
属性,因为连接设置上的默认映射将优先 您使用的是哪一版本的NEST软件包,您的目标是哪一版本的Elasticsearch?你能把细节添加到你的问题中吗?我正在使用Elasticsearch和NEST,两者都是6.3。我对6.2也有同样的问题,所以我更新了想法,也许这会有所帮助。最新的NEST版本是6.2.0-您是否将其用于Elasticsearch 6.3.0?很抱歉,它们都是6.2版本,最近从6.1更新而来。我当时不在办公室,从内存中提取了版本。这100%修复了它。IdProperty是我完全错过的重构的残余。非常感谢你的帮助。