使用 Nest c# 创建弹性索引时无法映射 Hashset<object>
Unable to map Hashset<object> while creating elastic index using Nest c#
我在为我的用户模型创建索引时遇到问题。我的 C# 模型是-
public sealed class User
{
public long Id { get; set; }
public string FullName { get; set; }
public HashSet<Reference> References { get; set; }
}
public sealed class Reference
{
public string Type { get; set; }
public string Id { get; set; }
}
我正在使用 NEST nuget 创建索引。
var newIndexResponse = await _elasticClient.CreateIndexAsync(aliasData.NewIndex, i => i
.Mappings(mappingsDescriptor => mappingsDescriptor
.Map<User>(m => m
.Properties(ps => ps
.Text(p => p
.Name(u => u.References)
.Analyzer(ElasticConstants.TwoLetterAnalyzerName)
.SearchAnalyzer(ElasticConstants.NameSearchAnalyzerName))
.Object<HashSet<Reference>>(p => p // This throws error
.Name(up => up.References)
.Properties(up => up.Object<Reference>(sp => sp
.Properties(so => so
.Keyword(eri => eri
.Name(ei => ei.Id)))
.Properties(so => so
.Keyword(ert => ert.Name(t => t.Type)))
))
)
)
)));
当我尝试 运行 这段代码时,我收到映射错误 Hashset
。
Could not get field name for ObjectTypeDescriptor2 mapping:
ArgumentException at Nest.PropertiesDescriptor
1.SetProperty(IProperty
type) at
Nest.ObjectPropertyDescriptorBase4.<>c.<Properties>b__21_0(TInterface
a, Func
2 v) at
Nest.Fluent.Assign[TDescriptor,TInterface,TValue](TDescriptor self,
TValue value, Action`2 assign)
用户模型是一个 AWS DynamoDb 实体,所以我使用 Hashset
代替 List
。我的最终目标是通过 Type 和 Id 在 Reference class 中搜索用户。我需要帮助来找出 HashSet<Reference>
的映射。
对于这种情况,我宁愿使用 nested
类型而不是 object
。这应该可以帮助您稍后进行搜索。您可以阅读更多相关内容 here. NEST has a section in docs 关于配置映射您也可以查看。
这是一个工作示例(使用 elasticsearch 6.2.4 和 NEST 6.8.1 测试)
var newIndexResponse = await elasticClient.CreateIndexAsync("documents", i => i
.Mappings(mappingsDescriptor => mappingsDescriptor
.Map<User>(m => m
.Properties(ps => ps
.Text(p => p
.Name(u => u.References))
.Nested<Reference>(p => p
.Name(up => up.References)
.Properties(sp => sp.Keyword(k => k.Name(n => n.Id)))
.Properties(sp => sp.Keyword(k => k.Name(n => n.Type)))
)
)
)));
它在 elasticsearch
中使用以下映射创建索引
{
"documents": {
"mappings": {
"user": {
"properties": {
"references": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
}
}
}
}
}
}
希望对您有所帮助。
我在为我的用户模型创建索引时遇到问题。我的 C# 模型是-
public sealed class User
{
public long Id { get; set; }
public string FullName { get; set; }
public HashSet<Reference> References { get; set; }
}
public sealed class Reference
{
public string Type { get; set; }
public string Id { get; set; }
}
我正在使用 NEST nuget 创建索引。
var newIndexResponse = await _elasticClient.CreateIndexAsync(aliasData.NewIndex, i => i
.Mappings(mappingsDescriptor => mappingsDescriptor
.Map<User>(m => m
.Properties(ps => ps
.Text(p => p
.Name(u => u.References)
.Analyzer(ElasticConstants.TwoLetterAnalyzerName)
.SearchAnalyzer(ElasticConstants.NameSearchAnalyzerName))
.Object<HashSet<Reference>>(p => p // This throws error
.Name(up => up.References)
.Properties(up => up.Object<Reference>(sp => sp
.Properties(so => so
.Keyword(eri => eri
.Name(ei => ei.Id)))
.Properties(so => so
.Keyword(ert => ert.Name(t => t.Type)))
))
)
)
)));
当我尝试 运行 这段代码时,我收到映射错误 Hashset
。
Could not get field name for ObjectTypeDescriptor
2 mapping: ArgumentException at Nest.PropertiesDescriptor
1.SetProperty(IProperty type) at Nest.ObjectPropertyDescriptorBase4.<>c.<Properties>b__21_0(TInterface a, Func
2 v) at Nest.Fluent.Assign[TDescriptor,TInterface,TValue](TDescriptor self, TValue value, Action`2 assign)
用户模型是一个 AWS DynamoDb 实体,所以我使用 Hashset
代替 List
。我的最终目标是通过 Type 和 Id 在 Reference class 中搜索用户。我需要帮助来找出 HashSet<Reference>
的映射。
对于这种情况,我宁愿使用 nested
类型而不是 object
。这应该可以帮助您稍后进行搜索。您可以阅读更多相关内容 here. NEST has a section in docs 关于配置映射您也可以查看。
这是一个工作示例(使用 elasticsearch 6.2.4 和 NEST 6.8.1 测试)
var newIndexResponse = await elasticClient.CreateIndexAsync("documents", i => i
.Mappings(mappingsDescriptor => mappingsDescriptor
.Map<User>(m => m
.Properties(ps => ps
.Text(p => p
.Name(u => u.References))
.Nested<Reference>(p => p
.Name(up => up.References)
.Properties(sp => sp.Keyword(k => k.Name(n => n.Id)))
.Properties(sp => sp.Keyword(k => k.Name(n => n.Type)))
)
)
)));
它在 elasticsearch
中使用以下映射创建索引{
"documents": {
"mappings": {
"user": {
"properties": {
"references": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
}
}
}
}
}
}
希望对您有所帮助。