首页 > 解决方案 > 如何使用 Elasticsearch NEST 动态索引文档?

问题描述

我想将来自不同客户的文档放到 Elasticsearch 中的不同索引中。文档在结构上有一些共同的部分和一个因客户而异的部分,名为Data

C#

class Entity
{
    public Guid CustomerId { get; set; }
    public IDictionary<string, object> Data { get; set; }
}

JSON

{
    "customerId": "10000000-0000-0000-0000-000000000000",
    "data":  {......}
}

假设我们有 10000 个“客户”,每个客户都有一百万份文档。“客户”可以动态创建和删除。

将来自不同客户的文档放到 Elasticsearch 中的不同索引中是个好主意吗?

是否可以根据插入文档的customerId字段在 Elasticsearch 中动态创建新索引?如何使用.Net客户端来做到这一点?

我正在寻找类似的东西:

var index = entity.CustomerId;
client.CreateDocument<Entity>(entity, index);

PS 我正在使用 Elasticsearch v7.6

标签: elasticsearch

解决方案


嗯,我找到了答案。无论如何,如果有人评论它在战略上有多好,那就太好了。

开始了:

 var indexName = entity.CustomerId.ToString();
 var request = new IndexRequest<Entity>(entity, indexName);
 client.Index<Entity>(entity);

推荐阅读