如何使用NEST Bulk Api将文档添加到elasticsearch

时间:2014-10-17 13:33:29

标签: elasticsearch nest

我是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:\Users\gmarz\code\elasticsearch-net\src\Nest\ExposedInternals\NestSerializer.cs:line 166
       at Nest.ElasticClient.<Bulk>b__1b6(ElasticsearchPathInfo`1 p, BulkDescriptor d) in c:\Users\gmarz\code\elasticsearch-net\src\Nest\ElasticClient-Bulk.cs:line 31
       at Nest.ElasticClient.Dispatch[D,Q,R](D descriptor, Func`3 dispatch) in c:\Users\gmarz\code\elasticsearch-net\src\Nest\ElasticClient.cs:line 82
       at Nest.ElasticClient.Dispatch[D,Q,R](Func`2 selector, Func`3 dispatch) in c:\Users\gmarz\code\elasticsearch-net\src\Nest\ElasticClient.cs:line 70
       at Nest.ElasticClient.Bulk(Func`2 bulkSelector) in c:\Users\gmarz\code\elasticsearch-net\src\Nest\ElasticClient-Bulk.cs:line 27
       at AttendancePOC.Controllers.AttendanceController.CreateBulk() in D:\GIT Source\ElasticSearch\AttendancePOC\AttendancePOC\Controllers\AttendanceController.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:

请帮助我..我的代码有什么问题吗?。

4 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。 我忘记了为此查询设置索引名称。 我在创建新的ElasticClient时设置了一个设置默认索引的配置,但是配置发生了变化,我的索引为空。

希望有所帮助

答案 1 :(得分:0)

我遇到了这个问题,解决方法是将defaultIndex属性包含在ConnectionSettings here中,例如:

var node = new Uri("http://something:9200");
var settings = new ConnectionSettings(node, "someDefaultIndexValue");
var client = new ElasticClient(settings);

答案 2 :(得分:0)

我这样做:

        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")
        }

答案 3 :(得分:0)

请添加这样的默认索引  connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(&#34; indexname&#34;)