How to bulk insert Json using NEST elasticsearch?

£可爱£侵袭症+ 提交于 2019-12-08 02:43:41

问题


I'am trying to insert multiple records into my database using Nest. Inserting using IndexMany class does work however I also need to insert objects by json string.

I did look on github, and found some examples how to use the RAWclient. Below a code example I insert my json.

    > var twitter = _jsonData;          
    > var result = client.Raw.BulkPost(
    >               new { twitter }
    >               , qs => qs
    >                   //.Replication(ReplicationOptions.Async)
    >                   .Refresh(true)          );

some additional info:

jsondata:

tweet tweet1 = new tweet { id = "104", name = "test104", lastname = "test107" }; //ect....
List<tweet> data; //multiple tweet objects are added
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);

var twitter:

{
      "twitter": "[{'name':'test104','lastname':'test107','id':'104'},{'name':'test105','lastname':'test108','id':'105'},{'name':'test106','lastname':'test109','id':'106'}]"
}

result i receive from the database:

{"error":"Unexpected end-of-input: expected close marker for OBJECT (from [Source: [B@10893e4; line: 1, column: 0])\n at [Source: [B@10893e4; line: 2, column: 3]"}

Does anyone know what the issue might be? or what I'am missing in my json/code snipped?


回答1:


Your json is not correct for elasticsearch bulk operation. See the documentation.

In a bulk request every data object should be preceded by a command because a single bulk request can contain inserts, updates or deletes, not just inserts. So your json should look like

  { "index" : { "_index" : "twitter", "_type" : "tweets" } }\n
  {'name':'test104','lastname':'test107','id':'104'}\n
  { "index" : { "_index" : "twitter", "_type" : "tweets" } }\n
  {'name':'test105','lastname':'test108','id':'105'}\n
  { "index" : { "_index" : "twitter", "_type" : "tweets" } }\n
  {'name':'test106','lastname':'test109','id':'106'}\n

To reduce overhead from repetitive commands you can move some arguments to the request uri. Then the json can be shorter:

  { "index" : { } }\n
  {'name':'test104','lastname':'test107','id':'104'}\n

In IRawElasticClient that means moving them to BulkPost arguments.

  var result = client.Raw.BulkPost(new { twitter }, "twitter", "tweets");


来源:https://stackoverflow.com/questions/22017858/how-to-bulk-insert-json-using-nest-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!