我一直在使用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
关于.net - 使用Elasticsearch.net或PlainElastic.net批量插入Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27149351/