我正在嘗試使用多個條件從彈性資料庫中檢索資料,例如 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# 代碼。在字串 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;
}
}
我正在嘗試多種方法來修復它。但我無法解決這個問題。任何人都可以幫助我解決問題。
uj5u.com熱心網友回復:
特長;
您沒有在 inpostman和 in 中執行相同的查詢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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485193.html
