我一直在使用Elasticsearch.net(http://nest.azurewebsites.net/)和PlainElastic.Net(https://github.com/Yegoroff/PlainElastic.Net),并且能够插入单个文档进入elasticsearch。我现在试图弄清楚如何执行批量插入。我知道这两个.net库中的每一个都有与此相关的文档,但是我要插入的数据存储在字典中,其中的键是文档的ID,值是文档,我不知道该怎么做。
这是我有的一些代码(使用Elasticsearch.net):
var conn = new Uri("http://localhost:9200");
var config = new ConnectionConfiguration(conn);
var client = new ElasticsearchClient(config);
var myJson = @"{""Col1"" : ""Hello World"", ""col2"" : ""asdfasdf"" }";
var myjson2 = @"{""Col2"" : ""Hello World Again"", ""col2"" : ""zxcvzxcv"" }";
Dictionary<string, string> jsonCollection = new Dictionary<string, string>();
jsonCollection.Add("1", myJson);
jsonCollection.Add("2", myjson2);
我使用PlainElastic.Net,这是RAW数据的外观
POST /_bulk
{ "index" :{ "_index": "myIndex", "_type": "myType", "_id": 1 }}
{ "id": 1, "name": "My category \"ONE\" "}
{ "index" :{ "_index": "myIndex", "_type": "myType", "_id": 2 }}
{ "id": 2, "name": "My second category \t "}
{ "index" :{ "_index": "myIndex", "_type": "myType", "_id": 3 }}
{ "id": 3, "name": "My third category \r\n "}
请记住,新行位于每一行的末尾(即使在最后一行之后也可以)
所以vb.net应该看起来像这样:
Dim bulkData As String = "{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 1 }}" & vbNewLine & _
"{ ""id"": 1, ""name"": ""My category""}" & vbNewLine & _
"{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 2 }}" & vbNewLine & _
"{ ""id"": 2, ""name"": ""My second category""} " & vbNewLine & _
"{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 3 }}" & vbNewLine & _
"{ ""id"": 3, ""name"": ""My third category""} " & vbNewLine
Dim ESConn as New ElasticConnection("localhost", 9200)
Dim response As String = ESConn.Post("/_bulk", bulkData)
我没有测试过的C#版本,但您会明白的
string bulkData = @"{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 1}}
{ ""id"": 1, ""name"": ""My category""}
{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 2}}
{ ""id"": 2, ""name"": ""My second category""}
{ ""index"": { ""_index"": ""myIndex"", ""_type"": ""myType"", ""_id"": 3}}
{ ""id"": 3, ""name"": ""My third category""} \n";
ElasticConnection ESConn = New ElasticConnection("localhost", 9200);
string response = ESConn.Post("/_bulk", bulkData);
您可以手动或使用Newtonsoft.Json创建JSON
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句