我有一些 json 文件需要导入到 elasticsearch。
我使用 curl api。以下是示例,对我来说效果很好。
curl -XPOST http://localhost:9200/index_local/_doc/_bulk -H "Content-Type: application/json" --data-binary @sample.json
我使用HttpWebRequest进行模拟,它对我来说也很好。
public void Post(string fileName)
{
try
{
// get the byte array
var data = File.ReadAllBytes(fileName);
// create HttpRequest
var httpRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:9200/index_local/_doc/_bulk");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
httpRequest.ContentLength = data.Length;
// set the file byte array to the stream
using (var requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(data, 0, data.Length);
}
// get the response
using (var response = httpRequest.GetResponse() as HttpWebResponse)
{
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
// read the result
Console.WriteLine(responseStream.ReadToEnd());
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
但我找不到带有 elasticsearch.net 的导入 json 文件的批量 api。
是否有一些等于HttpWebRequest的函数可以将 json 文件发布到 elasticsearch ?
elasticsearch.net 库 ElasticLowLevelClient 或 ElasticClient 是否支持使用 btye 数组导入 json 文件?