【问题标题】:Elasticsearch NEST - System.StackOverflowException when running IndexElasticsearch NEST - 运行索引时出现 System.StackOverflowException
【发布时间】:2021-05-19 10:17:52
【问题描述】:

当我运行这段代码时:

var result = _client.Index<EntityType>(item, i => i.Index(n));

我收到此错误:

发生异常:CLR/System.StackOverflowException 未处理 'System.StackOverflowException' 类型的异常发生在 Elasticsearch.Net.dll

完整方法:

public bool Index<EntityType>(EntityType item, int attempt = 0) where EntityType : class, IDomainEntity<int>
{
    const int maxRetries = 5;
    if (item == null)
    {
        return false;
    }
    var type = item.GetType();
    var attributes = type.CustomAttributes;
    string n = "";

    foreach (var attribute in attributes)
    {
        foreach (var arg in attribute.NamedArguments)
        {
            if (arg.MemberName == "RelationName")
            {
                n = arg.TypedValue.Value.ToString().ToLower();
            }
        }
    }

    var result = _client.Index<EntityType>(item, i => i.Index(n));
    if (!CheckResponse(result) && attempt < maxRetries)
    {
        RefreshClient<EntityType>();
        attempt++;
        return Index(item, attempt);
    }
    RefreshClient<EntityType>();
    return result.IsValid;
}

【问题讨论】:

  • 属性IdIdType是什么?
  • @RussCam int。我也使用泛型来允许字符串 id。
  • 啊抱歉,我错过了(深夜:))你有一个完整的可复制的例子可以分享(在这里或作为要点)?
  • @RussCam 我实际上认为它可能与您的other answer 相同,在意识到您已回答之前,我已将其链接到我的问题中。
  • 这也是我的怀疑

标签: asp.net-core elasticsearch .net-core nest


【解决方案1】:

我将 NEST 中的 [PropertyName("propertyToIgnoreInElasticsearch", Ignore = true)] 添加到我的 POCO 字段中,这导致索引时出现无限循环。它会忽略 Elasticsearch 索引中的字段,因此不会对其进行索引。

例如:

[Serializable]
public abstract class VeganItem<VeganItemEstablishmentType>
{
    [Required]
    public string Name { get; set; }

    [PropertyName("veganItemEstablishments", Ignore = true)]
    public virtual ICollection<VeganItemEstablishmentType> VeganItemEstablishments { get; set; }
}

【讨论】:

  • 很高兴看到你把它整理好了。一般来说,最好创建特定的 POCO 来与 Elasticsearch 交互。复杂的对象,例如与 Entity Framework 和 NHibernate 等 ORM 一起使用的对象,可能包含延迟加载的实体和自引用循环,在序列化为 JSON 以发送到 Elasticsearch 时可能会导致问题
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2016-06-17
  • 2014-11-12
相关资源
最近更新 更多