我正在搞乱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);
答案 0 :(得分:2)
我使用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#version我没有测试,但你会明白
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);
您可以手动创建JSON或使用Newtonsoft.Json
创建答案 1 :(得分:0)
我使用下面的类来加载批量索引。这个例子不使用Json文档。
public class esclient
{
const string uri = "http://localhost:9200";
const string index = "INDEX_NAME";
static ElasticClient _current;
public esclient()
{
if (_current == null)
{
var node = new Uri(uri);
var _settings = new ConnectionSettings(node)
.DefaultIndex(index)
.MaximumRetries(2)
.MaxRetryTimeout(TimeSpan.FromSeconds(150));
_current = new ElasticClient(_settings);
}
}
public void bulkIndexCreate(IEnumerable<esentity> items)
{
var descriptor = new BulkDescriptor();
foreach (var item in items)
{
descriptor.Index<esentity>(op => op.Document(item));
}
var result = _current.Bulk(descriptor);
}
}