在 C#.Net 中的 DataTable 中获取 Elasticsearch 响应
Get Elasticsearch response in DataTable in C#.Net
我是 Elasticsearch 2.1.1
的新手,我想问你如何使用 Suggest
从 index
获取记录并将其转换为 DataTable
。我已经在我的项目中安装了 Nest NuGet 包。
我有这段代码,我希望它来自 Elasticsearch 的数据表格式
curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
"song-suggest" : {
"text" : "n",
"completion" : {
"field" : "suggest"
}
}
}'
我有一些code
块应该可以满足你的要求。
你说你是Elasticsearch
的新手,所以我先向你展示如何初始化连接。
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var clientElasticSearchNet = new ElasticsearchClient(settings);
然后使用
var ElasticSearchNetQuery = new { music = new { "n", completion = new { field = "name_suggest" } } };
ElasticsearchResponse<string> result = clientElasticSearchNet.Suggest<string>("music", ElasticSearchNetQuery);
JObject json = JObject.Parse(result.Response.ToString());
var hitsCount = ((Newtonsoft.Json.Linq.JContainer)(json["music"].First["options"])).Count;
DataTable dtEsReponnse = new DataTable();
for (int i = 0; i < hitsCount; i++)
{
dtEsReponnse = ConvertJSONToDataTable(json["music"].First["options"][i].ToString());
}
我创建了一个函数,它将 return 数据表。
protected DataTable ConvertJSONToDataTable(string jsonString)
{
string[] jsonParts = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
List<string> dtColumns = new List<string>();
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1);
string v = rowData.Substring(idx + 1);
if (!dtColumns.Contains(n))
{
dtColumns.Add(n.Replace("\"", ""));
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Error Column Name : {0}", rowData));
}
}
break;
}
foreach (string c in dtColumns)
{
if (!dt.Columns.Contains(c.ToString().Replace("\r\n", "").Trim())) {
dt.Columns.Add(c.ToString().Replace("\r\n", "").Trim());
}
}
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dt.NewRow();
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1).Replace("\"", "").Replace("\r\n", "").Trim(); //replaced
string v = rowData.Substring(idx + 1).Replace("\"", "");
nr[n] = v;
}
catch (Exception ex)
{
continue;
}
}
dt.Rows.Add(nr);
}
return dt;
}
这是您要找的代码吗?
我是 Elasticsearch 2.1.1
的新手,我想问你如何使用 Suggest
从 index
获取记录并将其转换为 DataTable
。我已经在我的项目中安装了 Nest NuGet 包。
我有这段代码,我希望它来自 Elasticsearch 的数据表格式
curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
"song-suggest" : {
"text" : "n",
"completion" : {
"field" : "suggest"
}
}
}'
我有一些code
块应该可以满足你的要求。
你说你是Elasticsearch
的新手,所以我先向你展示如何初始化连接。
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var clientElasticSearchNet = new ElasticsearchClient(settings);
然后使用
var ElasticSearchNetQuery = new { music = new { "n", completion = new { field = "name_suggest" } } };
ElasticsearchResponse<string> result = clientElasticSearchNet.Suggest<string>("music", ElasticSearchNetQuery);
JObject json = JObject.Parse(result.Response.ToString());
var hitsCount = ((Newtonsoft.Json.Linq.JContainer)(json["music"].First["options"])).Count;
DataTable dtEsReponnse = new DataTable();
for (int i = 0; i < hitsCount; i++)
{
dtEsReponnse = ConvertJSONToDataTable(json["music"].First["options"][i].ToString());
}
我创建了一个函数,它将 return 数据表。
protected DataTable ConvertJSONToDataTable(string jsonString)
{
string[] jsonParts = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
List<string> dtColumns = new List<string>();
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1);
string v = rowData.Substring(idx + 1);
if (!dtColumns.Contains(n))
{
dtColumns.Add(n.Replace("\"", ""));
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Error Column Name : {0}", rowData));
}
}
break;
}
foreach (string c in dtColumns)
{
if (!dt.Columns.Contains(c.ToString().Replace("\r\n", "").Trim())) {
dt.Columns.Add(c.ToString().Replace("\r\n", "").Trim());
}
}
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dt.NewRow();
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1).Replace("\"", "").Replace("\r\n", "").Trim(); //replaced
string v = rowData.Substring(idx + 1).Replace("\"", "");
nr[n] = v;
}
catch (Exception ex)
{
continue;
}
}
dt.Rows.Add(nr);
}
return dt;
}
这是您要找的代码吗?