C# - NEST 库中的 BulkDescriptor() 方法覆盖数据
C# - BulkDescriptor() method in NEST library overwriting the data
我是 ES 的新手,我正在使用 vesion 7.x。我正在尝试索引 POCO class 对象的列表,但是 bulkdescriptor 只是覆盖数据并且列表中的最后一个对象正在被索引。我正在尝试索引 "myindex" index.
中的所有项目
我试图通过 for 循环一个一个地索引,但这需要时间,我遇到了这个 BulkDescriptor 方法。
这是我用于索引的代码:
ESClient:
public ElasticClient EsClient()
{
ConnectionSettings(connectionPool).DisableDirectStreaming();
Uri EsInstance = new Uri("http://localhost:9200");
ConnectionSettings EsConfiguration = new
ConnectionSettings(EsInstance).DefaultIndex("myindex");
ElasticClient esClient = new ElasticClient(EsConfiguration);
return esClient;
}
索引代码:
var bulkIndexer = new BulkDescriptor();
foreach (var item in items)
{
bulkIndexer.Index<IndexDataItem>(i => i
.Document(item)
.Id(item.Id));
}
var bulkIndexResponse = _connectionToEs.EsClient().Bulk(b => bulkIndexer);
我也曾尝试在 foreach 循环中添加此 var bulkIndexResponse = _connectionToEs.EsClient().Bulk(b => bulkIndexer);
,但结果相同。
这是我的 POCO Class:
public class IndexDataItem
{
public IndexDataItem()
{
DateModified = DateTime.Now;
DateCreated = DateTime.Now;
}
public int Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public string FullText { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public DocumentLevel DocumentLevel { get; set; }
public IndexDataField[] Fields { get; set; }
}
我希望所有列表对象都在 "myindex" 下建立索引。有人可以帮忙吗。
提前致谢!!!
看起来 Rob 可能已经在评论中提出了这种情况下的问题,但我想添加一些额外的信息来帮助您入门。
使用 Bulk()
API 允许您向 Elasticsearch 发送批量操作请求。如果您需要索引 非常 大量文档,您需要发送多个批量请求来执行此操作,处理重试和失败(如果发生)。为此,您可以考虑 using the BulkAll()
API in NEST,这是一个 .NET 客户端抽象,用于从 IEnumerable<T>
文档
发送批量操作
public static IEnumerable<IndexDataItem> GetItems()
{
var count = 0;
while (count < 1_000_000)
{
yield return new IndexDataItem { Id = count + 1 };
count++;
}
}
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex("myindex");
var client = new ElasticClient(settings);
var bulkAllObservable = client.BulkAll(GetItems(), b => b
.BackOffTime("30s")
.BackOffRetries(2)
.RefreshOnCompleted()
.MaxDegreeOfParallelism(Environment.ProcessorCount)
.Size(1000)
)
.Wait(TimeSpan.FromMinutes(60), next =>
{
// do something e.g. write number of pages to console
});
我是 ES 的新手,我正在使用 vesion 7.x。我正在尝试索引 POCO class 对象的列表,但是 bulkdescriptor 只是覆盖数据并且列表中的最后一个对象正在被索引。我正在尝试索引 "myindex" index.
中的所有项目我试图通过 for 循环一个一个地索引,但这需要时间,我遇到了这个 BulkDescriptor 方法。
这是我用于索引的代码:
ESClient:
public ElasticClient EsClient()
{
ConnectionSettings(connectionPool).DisableDirectStreaming();
Uri EsInstance = new Uri("http://localhost:9200");
ConnectionSettings EsConfiguration = new
ConnectionSettings(EsInstance).DefaultIndex("myindex");
ElasticClient esClient = new ElasticClient(EsConfiguration);
return esClient;
}
索引代码:
var bulkIndexer = new BulkDescriptor();
foreach (var item in items)
{
bulkIndexer.Index<IndexDataItem>(i => i
.Document(item)
.Id(item.Id));
}
var bulkIndexResponse = _connectionToEs.EsClient().Bulk(b => bulkIndexer);
我也曾尝试在 foreach 循环中添加此 var bulkIndexResponse = _connectionToEs.EsClient().Bulk(b => bulkIndexer);
,但结果相同。
这是我的 POCO Class:
public class IndexDataItem
{
public IndexDataItem()
{
DateModified = DateTime.Now;
DateCreated = DateTime.Now;
}
public int Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public string FullText { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public DocumentLevel DocumentLevel { get; set; }
public IndexDataField[] Fields { get; set; }
}
我希望所有列表对象都在 "myindex" 下建立索引。有人可以帮忙吗。
提前致谢!!!
看起来 Rob 可能已经在评论中提出了这种情况下的问题,但我想添加一些额外的信息来帮助您入门。
使用 Bulk()
API 允许您向 Elasticsearch 发送批量操作请求。如果您需要索引 非常 大量文档,您需要发送多个批量请求来执行此操作,处理重试和失败(如果发生)。为此,您可以考虑 using the BulkAll()
API in NEST,这是一个 .NET 客户端抽象,用于从 IEnumerable<T>
文档
public static IEnumerable<IndexDataItem> GetItems()
{
var count = 0;
while (count < 1_000_000)
{
yield return new IndexDataItem { Id = count + 1 };
count++;
}
}
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex("myindex");
var client = new ElasticClient(settings);
var bulkAllObservable = client.BulkAll(GetItems(), b => b
.BackOffTime("30s")
.BackOffRetries(2)
.RefreshOnCompleted()
.MaxDegreeOfParallelism(Environment.ProcessorCount)
.Size(1000)
)
.Wait(TimeSpan.FromMinutes(60), next =>
{
// do something e.g. write number of pages to console
});