我是ES新手,正在使用vesion7.x。我正在尝试为POCO类对象的列表建立索引,但是bulkdescriptor仅覆盖数据,并且列表中的最后一个对象得到了索引。我正在尝试索引“myindex”索引中的所有项目。
我试图通过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
});
关于c# - C#-NEST库中的BulkDescriptor()方法覆盖数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773296/