.net - 使用Elasticsearch.net或PlainElastic.net批量插入Elasticsearch

标签 .net elasticsearch bulk

我一直在使用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/

相关文章:

.net - 生成 FlowDocument 第一页的预览缩略图图像的推荐方法是什么

C# 返回类型问题

elasticsearch - ELK Stack-Elasticsearch索引创建(logstash)

elasticsearch - 在Elasticsearch中使用电子邮件 token 生成器

c# - 使用 C# 从 sendgrid 发送计划电子邮件

c# - HttpWebRequest,像 Fiddler 一样保持事件状态?

c# - 将像素转换为点

elasticsearch - 如何检查所有碎片是否已从特定的Elasticsearch节点移走?

endpoint - Instagram API 通过逗号分隔的 ID 获取批量对象

ruby-on-rails - 如何在 Ruby on Rails 中批量上传图像(一次大约 100-200 个图像)?