I am trying to retrieve the data from elastic DB by using multiple conditions like, "cu_ticketLastUpdated_date" in between 24hrs and "platform" which belongs to COSMOS. I am using the below code for these conditions
{
"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"
}}}]}}}
The above code is working fine when I have used it in Postman.(satisfying both "cu_ticketLastUpdated_date" and "platform" conditions)
But if I am trying to implement the same conditions in C# only one condition is satisfying i.e.; "cu_ticketLastUpdated_date" in between 24hrs rest condition "platform" = COSMOS is not working.... Instead of that my C# code is retrieving all Platforms data in between 24hrs
Below is my c# code. In string json I am passing these conditions.
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;
}
}
I am trying multiple ways to fix it. But I am unable to fix this issue. Can any one help me with the solution.
CodePudding user response:
Tldr;
You are not doing the same query in postman
and in c#
.
In postman you use a must
clause while in the c#
you use should
.
To Fix
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;
}
}