[英]How to bulk insert Json using NEST elasticsearch?
I'am trying to insert multiple records into my database using Nest. 我正在尝试使用Nest将多个记录插入数据库。 Inserting using IndexMany class does work however I also need to insert objects by json string.
使用IndexMany类插入确实可以,但是我还需要通过json字符串插入对象。
I did look on github, and found some examples how to use the RAWclient. 我确实看过github,并找到了一些如何使用RAWclient的示例。 Below a code example I insert my json.
在代码示例下面,我插入json。
> var twitter = _jsonData;
> var result = client.Raw.BulkPost(
> new { twitter }
> , qs => qs
> //.Replication(ReplicationOptions.Async)
> .Refresh(true) );
some additional info: 一些其他信息:
jsondata: 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: 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?
还是我在json /代码中丢失了什么?
Your json is not correct for elasticsearch bulk operation. 您的json不适合Elasticsearch批量操作。 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
所以你的json应该看起来像
{ "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. 为了减少重复命令的开销,您可以将一些参数移至请求uri。 Then the json can be shorter:
然后json可以更短:
{ "index" : { } }\n
{'name':'test104','lastname':'test107','id':'104'}\n
In IRawElasticClient that means moving them to BulkPost arguments. 在IRawElasticClient中,这意味着将它们移至BulkPost参数。
var result = client.Raw.BulkPost(new { twitter }, "twitter", "tweets");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.