【问题标题】:Unable to map Hashset<object> while creating elastic index using Nest c#使用 Nest c# 创建弹性索引时无法映射 Hashset<object>
【发布时间】:2019-08-12 16:43:27
【问题描述】:

我在为我的用户模型创建索引时遇到问题。我的 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 时遇到错误。

无法获取 ObjectTypeDescriptor2 mapping: ArgumentException at Nest.PropertiesDescriptor1.SetProperty(IProperty) 的字段名称 类型)在 Nest.ObjectPropertyDescriptorBase4.<>c.<Properties>b__21_0(TInterface a, Func2 v) 在 Nest.Fluent.Assign[TDescriptor,TInterface,TValue](TDescriptor self, TValue 值,Action`2 赋值)

用户模型是一个 AWS DynamoDb 实体,所以我使用 Hashset 代替 List。我的最终目标是在 Reference 类中按 TypeId 搜索用户。我需要帮助来确定HashSet&lt;Reference&gt; 的映射。

【问题讨论】:

    标签: c# nest aws-elasticsearch


    【解决方案1】:

    在这种情况下,我宁愿使用nested 类型而不是object。这应该可以帮助您稍后进行搜索。你可以阅读更多关于它的信息here。 NEST 在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)))
                    )
                )
            )));
    

    在弹性搜索中创建具有以下映射的索引

    {
      "documents": {
        "mappings": {
          "user": {
            "properties": {
              "references": {
                "type": "nested",
                "properties": {
                  "id": {
                    "type": "keyword"
                  },
                  "type": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      相关资源
      最近更新 更多