当我们索引的类型是泛型时如何调用 IElasticClient.Index()

How to call IElasticClient.Index() when the type that we are indexing is generic

当我有一个明确类型化的对象时,我的索引没有问题。例如,

EventObject e;
IElasticClient client;
client.Index(e); // perfectly valid

但是,我有一个 class 类型是通用的。这是我的 class

class FantasticClass<INDEXCLASS>
{
    IElasticClient client;
    public FantasticClass(IElasticClient c)
    {
        client = c;
    }

    public INDEXCLASS Add(INDEXCLASS entity)
    {
        client.Index(entity); // can't compile!
        // or
        client.Index<INDEXCLASS>(entity); // can't compile!
    }
}

当推断 class 类型时,如何使用 Nest 建立索引?

无法编译的问题是由于 class 缺少类型的声明 INDEXCLASS 这应该解决该错误:

class FantasticClass<INDEXCLASS> where INDEXCLASS : class

此外,Add 方法有一个 return 类型的 INDEXCLASS,但是该方法中没有任何内容 returning。将其更改为 void 或 return entity 变量在它被索引后。