我正在尝试使用Nest将多条记录插入到我的数据库中。使用IndexMany类插入确实有效,但我还需要通过json string插入对象。
我确实在github上看了一下,并找到了一些如何使用RAWclient的例子。在代码示例下面,我插入了我的json。
> var twitter = _jsonData;
> var result = client.Raw.BulkPost(
> new { twitter }
> , qs => qs
> //.Replication(ReplicationOptions.Async)
> .Refresh(true) );
其他一些信息:
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'}]"
}
我从数据库收到的结果:
{"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]"}
有谁知道问题可能是什么?或者我的json /代码中缺少什么?
答案 0 :(得分:5)
您的json对弹性搜索批量操作不正确。请参阅documentation。
在批量请求中,每个数据对象都应该以命令开头,因为单个批量请求可以包含插入,更新或删除,而不仅仅是插入。 所以你的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
为了减少重复命令的开销,您可以将一些参数移动到请求uri。然后json可以更短:
{ "index" : { } }\n
{'name':'test104','lastname':'test107','id':'104'}\n
在IRawElasticClient中,这意味着将它们移动到BulkPost参数。
var result = client.Raw.BulkPost(new { twitter }, "twitter", "tweets");