Created
September 3, 2018 10:08
-
-
Save jrgcubano/513bf9c91606b872eb1701b09c07037c to your computer and use it in GitHub Desktop.
Get ElasticSearch Response Directly from API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var counters = getCarparkCounters("1aae8f78-42df-4b72-916e-76e5ab2aa6ac").Hits.HitsHits.Select(c => c.Source); | |
Save(counters, "carpark-counters"); | |
} | |
static ElasticSearchResponse getCarparkCounters(string carparkId) | |
{ | |
var client = new System.Net.Http.HttpClient(); | |
var response = client.PostAsync("http://10.0.0.30:9200/analytics-qa-counters-carparks-minute/elasticcarparkcounterdata/_search", new System.Net.Http.StringContent(@"{ | |
""size"": 5, | |
""query"": { | |
""bool"": { | |
""must"": [ | |
{ | |
""term"": { | |
""carparkId"": """ + carparkId + @""" | |
} | |
} | |
] | |
} | |
} | |
}")).Result.Content.ReadAsStringAsync().Result; | |
try | |
{ | |
return JsonConvert.DeserializeObject<ElasticSearchResponse>(response); | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
static void Save<T>(IEnumerable<T> obj, string name) | |
{ | |
Console.WriteLine(name + ": " + obj.Count()); | |
var text = JsonConvert.SerializeObject(obj); | |
System.IO.File.WriteAllText($@"C:\source\Example\{name}.json", text); | |
} | |
} | |
public partial class ElasticSearchResponse | |
{ | |
[JsonProperty("took")] | |
public long Took { get; set; } | |
[JsonProperty("timed_out")] | |
public bool TimedOut { get; set; } | |
[JsonProperty("_shards")] | |
public Shards Shards { get; set; } | |
[JsonProperty("hits")] | |
public Hits Hits { get; set; } | |
} | |
public partial class Hits | |
{ | |
[JsonProperty("total")] | |
public long Total { get; set; } | |
[JsonProperty("max_score")] | |
public double MaxScore { get; set; } | |
[JsonProperty("hits")] | |
public Hit[] HitsHits { get; set; } | |
} | |
public partial class Hit | |
{ | |
[JsonProperty("_index")] | |
public string Index { get; set; } | |
[JsonProperty("_type")] | |
public string Type { get; set; } | |
[JsonProperty("_id")] | |
public string Id { get; set; } | |
[JsonProperty("_score")] | |
public double Score { get; set; } | |
[JsonProperty("_source")] | |
public Source Source { get; set; } | |
} | |
public partial class Source | |
{ | |
[JsonProperty("id")] | |
public string Id { get; set; } | |
[JsonProperty("counterId")] | |
public string CounterId { get; set; } | |
[JsonProperty("carparkId")] | |
public Guid CarparkId { get; set; } | |
[JsonProperty("counterType")] | |
public string CounterType { get; set; } | |
[JsonProperty("assetId")] | |
public Guid AssetId { get; set; } | |
[JsonProperty("assetType")] | |
public string AssetType { get; set; } | |
[JsonProperty("minimum")] | |
public long Minimum { get; set; } | |
[JsonProperty("average")] | |
public long Average { get; set; } | |
[JsonProperty("maximum")] | |
public long Maximum { get; set; } | |
[JsonProperty("dateTimeUtc")] | |
public DateTimeOffset DateTimeUtc { get; set; } | |
[JsonProperty("hourOfDay")] | |
public long HourOfDay { get; set; } | |
[JsonProperty("dayOfWeek")] | |
public long DayOfWeek { get; set; } | |
[JsonProperty("totalMinutes")] | |
public long TotalMinutes { get; set; } | |
} | |
public partial class Shards | |
{ | |
[JsonProperty("total")] | |
public long Total { get; set; } | |
[JsonProperty("successful")] | |
public long Successful { get; set; } | |
[JsonProperty("failed")] | |
public long Failed { get; set; } | |
} | |
public partial class ElasticSearchResponse | |
{ | |
public static ElasticSearchResponse FromJson(string json) => JsonConvert.DeserializeObject<ElasticSearchResponse>(json, Converter.Settings); | |
// public static string ToJson(this ElasticSearchResponse self) => JsonConvert.SerializeObject(self, Converter.Settings); | |
} | |
internal static class Converter | |
{ | |
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings | |
{ | |
MetadataPropertyHandling = MetadataPropertyHandling.Ignore, | |
DateParseHandling = DateParseHandling.None, | |
Converters = { | |
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment