问题描述
我试图使用Nest将多个记录插入到我的数据库中。使用IndexMany类进行插入确实有效,但是我也需要通过json字符串插入对象。我看过github,并找到了一些例子来使用RAWclient。下面的代码示例我插入我的json。
> var twitter = _jsonData;
> var result = client.Raw.BulkPost(
> new {twitter}
>,qs => qs
> //.Replication(ReplicationOptions.Async)
&ReReresh(true));
一些附加信息:
jsondata:
tweet tweet1 = new tweet {id =104,name =test104,lastname =test107}; // ect ....
列表< tweet>数据; //添加多个tweet对象
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
var twitter:
pre>
{
twitter:[{'name':'test104','lastname':'test107','id':'104'}, { '名称': 'test105', '名字': 'test108', 'ID': '105'},{ '名称': 'test106', '名字': 'test109', 'ID': '106' }]
}
结果我从数据库中收到:
{error:意外结束输入:OBJECT的预期关闭标记(来源:[B @ 10893e4; line:1,column:0])\\\
at [Source:[B @ 10893e4; line:2,column:3]}
有谁知道问题可能是什么?或者我在我的json /代码中丢失了什么?
你的json对于弹性搜索批量操作是不正确的。请参阅。
在批量请求中,每个数据对象都应该有一个命令,因为单个批量请求可以包含插入,更新或删除,而不仅仅是插入。
所以你的json应该看起来像
{index:{_index:twitter,_type :tweets}} \\\
{'name':'test104','lastname':'test107','id':'104'} \\\
{index :{_index:twitter,_type:tweets}} \\\
{'name':'test105','lastname':'test108','id' } \\\
{index:{_index:twitter,_type:tweets}} \\\
{'name':'test106','lastname' :'test109','id':'106'} \\\
为了减少重复的开销命令可以将一些参数移动到请求uri。那么json可以更短:
{index:{}} \\\
{'name' :'test104','lastname':'test107','id':'104'} \\\
var result = client.Raw.BulkPost(new {twitter} ,twitter,tweets);
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?
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");
这篇关于如何使用NEST弹性搜索来批量插入Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!