簡體   English   中英

使用NEST批量插入ElasticSearch

[英]Bulk insert to ElasticSearch with NEST

我嘗試向elasticsearch添加100k產品,但是當我嘗試獲取時:{“驗證失敗: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索引數據。 現在根據您在評論中的問題,您可以指定彈性搜索將使用的ID。 見下面的例子。

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

如果你不想指定任何Id到彈性搜索,創建一個虛擬字段dummyId(可空)並將其放在“IdProperty”中。 如果彈性搜索為空,則彈性搜索將自動為其分配值。

編輯:從2.3開始,它

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM