using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CreateMessagesFromES
static void Main(string[] args)
Console.Write("Login User Name:");
string userName = Console.ReadLine();
var password = getPasswordFromConsole("Login User Password:");
Console.Write("Enter ES Topic Name:");
string indexName = Console.ReadLine();
Console.Write("Enter From Date (yyyy-MM-dd):");
string fromDate = Console.ReadLine();
Console.Write("Enter To Date (yyyy-MM-dd):");
string toDate = Console.ReadLine();
Console.WriteLine("Message retrieval started...");
ReconcileBFRealtime(userName, password, indexName, fromDate, toDate);
Console.WriteLine("Done! :).....Press Key to terminate...");
public static void ReconcileBFRealtime(string userName, string password, string indexName, string fromDate, string toDate)
var customQuery = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Query.txt"));
""header.sourceSystemCreationTimestamp"": { ""gte"":""<<fromDate>>T00:00:00.000Z"",""lte"":""<<toDate>>T00:00:00.000Z""}
{ ""header.sourceSystemCreationTimestamp"": { ""order"": ""asc"" } }
if (!string.IsNullOrWhiteSpace(customQuery))
query = query.Replace("<<fromDate>>", fromDate).Replace("<<toDate>>", toDate);
var uris = "https://dp.bns:9200".Split(',').Select(uri => new Uri(uri));
IConnectionPool pool = new StaticConnectionPool(uris);
var conn = new ConnectionConfiguration(pool);
conn.RequestTimeout(TimeSpan.FromSeconds(System.Convert.ToInt32("500")))
.BasicAuthentication(userName, password)
var esClient = new ElasticLowLevelClient(conn);
var searchResponse = esClient.Search<DynamicResponse>(indexName, query);
if (!searchResponse.Success)
throw searchResponse.OriginalException;
var stringResponse = Encoding.UTF8.GetString(searchResponse.ResponseBodyInBytes);
JArray resultArray = new JArray();
var resp = JObject.Parse(stringResponse);
resultArray.Merge((JArray)((JObject)resp?["hits"])?["hits"]);
string path = $@"c:\\Temp\ES_Files_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
Directory.CreateDirectory(path);
Console.WriteLine($"Folder Created at {path}");
foreach (var item in resultArray)
string tradeRefVal = item.SelectToken("$._source.trade.tradeHeader.tradeIdentifiers.tradeId.id").ToString();
string filePath = Path.Combine(path, $"{tradeRefVal}_{System.IO.Path.GetRandomFileName().Split('.')[0]}.json");
File.WriteAllText(filePath, item.SelectToken("$._source").ToString());
DateTime creationTime = Convert.ToDateTime(item.SelectToken("$._source.header.sourceSystemCreationTimestamp").ToString());
File.SetCreationTime(filePath, creationTime);
File.SetLastWriteTime(filePath, creationTime);
Console.WriteLine(Path.GetFileName(filePath));
public static string getPasswordFromConsole(String displayMessage)
SecureString pass = new SecureString();
Console.Write(displayMessage);
key = Console.ReadKey(true);
if (!char.IsControl(key.KeyChar))
pass.AppendChar(key.KeyChar);
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
pass.RemoveAt(pass.Length - 1);
while (key.Key != ConsoleKey.Enter);
return pass.CreateString();