如何使用NEST Bulk Api将文档添加到elasticsearch
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用NEST Bulk Api将文档添加到elasticsearch相关的知识,希望对你有一定的参考价值。
我是Elasticsearch的新手。我需要使用NEST api使用批量选项插入文档。
我必须从表中索引5000个文档。下面是我用于批量索引的代码。
public ActionResult CreateBulk()
{
var descriptor = new BulkDescriptor();
foreach (var test in db.Attendance.Take(5000).ToList())
{
descriptor.Index<Attendance>(op => op.Document(new Attendance
{
AttendanceId = test.AttendanceId,
AttendanceDate = test.AttendanceDate,
Estate = test.Estate,
Division = test.Division,
FieldNo = test.FieldNo,
Employee = test.Employee,
Activity = test.Activity,
Quantity = test.Quantity
}));
}
var bulkresult = ElasticClient.Bulk(descriptor);
return RedirectToAction("Index");
}
但是当我运行代码时,我收到以下错误:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Nest
StackTrace:
at Nest.NestSerializer.SerializeBulkDescriptor(IBulkRequest bulkRequest) in c:Usersgmarzcodeelasticsearch-netsrcNestExposedInternalsNestSerializer.cs:line 166
at Nest.ElasticClient.<Bulk>b__1b6(ElasticsearchPathInfo`1 p, BulkDescriptor d) in c:Usersgmarzcodeelasticsearch-netsrcNestElasticClient-Bulk.cs:line 31
at Nest.ElasticClient.Dispatch[D,Q,R](D descriptor, Func`3 dispatch) in c:Usersgmarzcodeelasticsearch-netsrcNestElasticClient.cs:line 82
at Nest.ElasticClient.Dispatch[D,Q,R](Func`2 selector, Func`3 dispatch) in c:Usersgmarzcodeelasticsearch-netsrcNestElasticClient.cs:line 70
at Nest.ElasticClient.Bulk(Func`2 bulkSelector) in c:Usersgmarzcodeelasticsearch-netsrcNestElasticClient-Bulk.cs:line 27
at AttendancePOC.Controllers.AttendanceController.CreateBulk() in D:GIT SourceElasticSearchAttendancePOCAttendancePOCControllersAttendanceController.cs:line 114
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException:
请帮帮我..我的代码有什么问题吗?
答案
我有同样的问题。我忘记了为此查询设置索引名称。我已经设置了一个配置,它在创建新的ElasticClient期间设置了一个默认索引,但配置已更改,我的索引为null。
希望有所帮助
另一答案
请添加默认索引,例如此connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(“indexname”)
另一答案
我做这样的事情:
var node = new Uri(elasticSearchURI);
var connectionPool = new SniffingConnectionPool(new[] { node });
var config = new ConnectionSettings(connectionPool)
.SniffOnConnectionFault(false)
.SniffOnStartup(false)
.SetTimeout(600000)
.DisablePing();
var EsClient = new ElasticClient(config);
然后我创建索引,然后我使用这样的东西:
List<Categories> categList = new List<Categories>();
if (categoriesData != null)
{
Parallel.ForEach(categoriesData, element =>
{
Categories inf = new Categories();
inf.Code = element.Code;
inf.Level = element.Level;
lock(categList)
{
categList.Add(inf);
}
});
EsClient.IndexMany<Categories>(categList,"index_name","type_name")
}
另一答案
我有这个问题,解决方案是在defaultIndex
中包含ConnectionSettings
属性,如here所述,例如:
var node = new Uri("http://something:9200");
var settings = new ConnectionSettings(node, "someDefaultIndexValue");
var client = new ElasticClient(settings);
以上是关于如何使用NEST Bulk Api将文档添加到elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章
NEST (ElasticSearch) 将 Highlights 匹配到文档
Elastcisearch.Nest 7.x 系列`伪`官方翻译:通过 NEST 来快捷试用 Elasticsearch
如何在 NEST2 中更新 Elasticsearch 文档