【问题标题】:Bulk insert to ElasticSearch with NEST使用 NEST 批量插入 ElasticSearch
【发布时间】:2015-08-18 23:26:23
【问题描述】:

我尝试将 100k 产品添加到 elasticsearch,但是当我尝试时我得到:{“验证失败:1:没有添加请求;”}

我的代码:

        var Node = new Uri("......");
        var ConnectionPool = new SniffingConnectionPool(new[] { Node });
        var Config = new ConnectionConfiguration(ConnectionPool)
                    .SniffOnConnectionFault(false)
                    .SniffOnStartup(false)
                    .SniffLifeSpan(TimeSpan.FromMinutes(10));
        var Client = new ElasticsearchClient(Config);

        var AllProducts = Product.GetProducts();
        var SPl = AllProducts.Split(100); // Split into 100 collections/requests

        var COll = new List<ElasticsearchResponse<DynamicDictionary>>();

        foreach (var I in SPl)
        {
            var Descriptor = new BulkDescriptor();

            foreach (var Product in I)
            {
                Descriptor.Index<Product>(op => op.Document(Product));
            }

            COll.Add(Client.Bulk(Descriptor));
        }

AllProducts 包含此对象的列表:

public class Product
{
 public int AffiliateNr { get; set; }
 public string AffiliateProductId { get; set; }
 public int BrandNr { get; set; }
 public string Currency { get; set; }
 public string IndexId { get; set; }
 public decimal Price { get; set; }
 public long ProductNr { get; set; }
 public string Title { get; set; }
}

所以,

  1. 在哪里可以设置索引的名称?
  2. 为什么我得到,验证失败:1:没有添加请求;?
  3. IndexId 是我的产品 ID。我如何告诉 Elasticsearch 使用这个 id?还是我必须指定一个 ID?

【问题讨论】:

  • 为什么不使用 IndexMany?
  • 谢谢。我现在有: SPl.Select(I => Client.IndexMany(I, "products")) 它可以工作。但是:IndexId 是我的产品 ID。我如何告诉 Elasticsearch 使用这个 id?还是我必须指定一个 ID?
  • 将 IndexId 字段重命名为 Id,elasticsearch 将按照约定将其用作文档的 id。
  • @mrcode : 查看我的回答,如果有帮助,请标记它

标签: c# elasticsearch nest elasticsearch-net


【解决方案1】:

参考您之前的问题,您可以使用 IndexMany 对数据进行索引。 现在根据您在评论中提出的问题,您可以指定弹性搜索将使用的 id。请参阅下面的示例。

  ElasticType(IdProperty = "<fieldName>")]
    public class ClassName
    {

如果您不想为弹性搜索指定任何 Id,请创建一个虚拟字段 dummyId(nullable) 并将其放入“IdProperty”中。如果它为空,弹性搜索将自动为其分配值。

编辑: 从 2.3 开始,它的

[ElasticsearchType(IdProperty = "<fieldName>")]

【讨论】:

  • 完美。有没有一种简单的方法来标记我不想在弹性搜索中索引的属性? :)
  • @mrcode :是的,请通过答案stackoverflow.com/questions/23063839/…。如果正确,请标记以上答案
猜你喜欢
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多