使用 C# 从弹性检索数据时出现问题
Issue while retrieving the data from elastic using c#
我正在尝试通过使用多种条件从弹性数据库中检索数据,例如 24 小时内的“cu_ticketLastUpdated_date”和属于 COSMOS 的“平台”。我在这些条件下使用以下代码
{
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": [
{"match": {
"platform": "COSMOS"
}}],
"filter": [
{"range": {
"cu_ticketLastUpdated_date": {
"gte": "2022-05-31 00:00:00",
"lt": "2022-05-31 23:59:59"
}}}]}}}
当我在 Postman 中使用上面的代码时,它工作正常。(同时满足“cu_ticketLastUpdated_date”和“平台”条件)
但是如果我试图在 C# 中实现相同的条件,则只有一个条件可以满足,即; “cu_ticketLastUpdated_date”在 24 小时之间休息条件“平台”= COSMOS 不工作....取而代之的是我的 C# 代码在 24 小时
之间检索所有平台数据
下面是我的 C# 代码。在 string json 中,我正在传递这些条件。
public static string COSMOS(string getAPiurl, string ApiUserId, string ApiPassword)
{
string url = getAPiurl;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/JSON";
webrequest.Accept = "application/JSON";
String username = ApiUserId;
String password = ApiPassword;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
webrequest.Headers.Add("Authorization", "Basic " + encoded);
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
string json = "{\"from\": 0,\"size\": 10000,\"query\":{\"bool\":{\"should\":[{\"match\":{\"platform\":{\"query\":\"COSMOS\"}}}],\"filter\":[{\"range\":{\"cu_ticketLastUpdated_date\":{\"gte\":\"2022-05-31 00:00:00\",\"lt\":\"2022-05-31 23:59:59\"}}}]}}}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)webrequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
httpResponse.Close();
//MessageBox.Show(result);
return result;
}
}
我正在尝试多种方法来修复它。但我无法解决这个问题。谁能帮我解决一下。
Tldr;
您在 postman
和 c#
中执行的查询不同。
在邮递员中,您使用 must
子句,而在 c#
中,您使用 should
.
修复
public static string COSMOS(string getAPiurl, string ApiUserId, string ApiPassword)
{
string url = getAPiurl;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/JSON";
webrequest.Accept = "application/JSON";
String username = ApiUserId;
String password = ApiPassword;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
webrequest.Headers.Add("Authorization", "Basic " + encoded);
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
string json = "{\"from\": 0,\"size\": 10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"platform\":{\"query\":\"COSMOS\"}}}],\"filter\":[{\"range\":{\"cu_ticketLastUpdated_date\":{\"gte\":\"2022-05-31 00:00:00\",\"lt\":\"2022-05-31 23:59:59\"}}}]}}}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)webrequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
httpResponse.Close();
//MessageBox.Show(result);
return result;
}
}
我正在尝试通过使用多种条件从弹性数据库中检索数据,例如 24 小时内的“cu_ticketLastUpdated_date”和属于 COSMOS 的“平台”。我在这些条件下使用以下代码
{
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": [
{"match": {
"platform": "COSMOS"
}}],
"filter": [
{"range": {
"cu_ticketLastUpdated_date": {
"gte": "2022-05-31 00:00:00",
"lt": "2022-05-31 23:59:59"
}}}]}}}
当我在 Postman 中使用上面的代码时,它工作正常。(同时满足“cu_ticketLastUpdated_date”和“平台”条件)
但是如果我试图在 C# 中实现相同的条件,则只有一个条件可以满足,即; “cu_ticketLastUpdated_date”在 24 小时之间休息条件“平台”= COSMOS 不工作....取而代之的是我的 C# 代码在 24 小时
之间检索所有平台数据下面是我的 C# 代码。在 string json 中,我正在传递这些条件。
public static string COSMOS(string getAPiurl, string ApiUserId, string ApiPassword)
{
string url = getAPiurl;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/JSON";
webrequest.Accept = "application/JSON";
String username = ApiUserId;
String password = ApiPassword;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
webrequest.Headers.Add("Authorization", "Basic " + encoded);
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
string json = "{\"from\": 0,\"size\": 10000,\"query\":{\"bool\":{\"should\":[{\"match\":{\"platform\":{\"query\":\"COSMOS\"}}}],\"filter\":[{\"range\":{\"cu_ticketLastUpdated_date\":{\"gte\":\"2022-05-31 00:00:00\",\"lt\":\"2022-05-31 23:59:59\"}}}]}}}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)webrequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
httpResponse.Close();
//MessageBox.Show(result);
return result;
}
}
我正在尝试多种方法来修复它。但我无法解决这个问题。谁能帮我解决一下。
Tldr;
您在 postman
和 c#
中执行的查询不同。
在邮递员中,您使用 must
子句,而在 c#
中,您使用 should
.
修复
public static string COSMOS(string getAPiurl, string ApiUserId, string ApiPassword)
{
string url = getAPiurl;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/JSON";
webrequest.Accept = "application/JSON";
String username = ApiUserId;
String password = ApiPassword;
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
webrequest.Headers.Add("Authorization", "Basic " + encoded);
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
string json = "{\"from\": 0,\"size\": 10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"platform\":{\"query\":\"COSMOS\"}}}],\"filter\":[{\"range\":{\"cu_ticketLastUpdated_date\":{\"gte\":\"2022-05-31 00:00:00\",\"lt\":\"2022-05-31 23:59:59\"}}}]}}}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)webrequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
httpResponse.Close();
//MessageBox.Show(result);
return result;
}
}