Skip to content

Instantly share code, notes, and snippets.

@selcukusta
Last active July 30, 2017 17:11
Show Gist options
  • Select an option

  • Save selcukusta/d8615de18742902240b0889ece2af39f to your computer and use it in GitHub Desktop.

Select an option

Save selcukusta/d8615de18742902240b0889ece2af39f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace netcore_elastic_search_template
{
class Program
{
static void Main(string[] args)
{
var paramObject = JObject.FromObject(new
{
id = "basic_search_template",
@params = new { query_string = "kamu" }
}
);
var records = GetRecords(paramObject);
foreach (var item in records)
{
Console.WriteLine($"{item.Title} ({item.Created_Date})");
}
}
static List<SampleType> GetRecords(JObject parameters)
{
List<SampleType> value = new List<SampleType>();
var elasticConnection = "http://localhost:9200";
using (var client = new HttpClient())
{
var httpContent = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json");
var response = client.PostAsync(new Uri(elasticConnection + "/_search/template"), httpContent).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content.ReadAsStringAsync().Result;
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(responseContent);
foreach (var hit in result["hits"]["hits"])
{
SampleType content = hit["_source"].ToObject<SampleType>();
value.Add(content);
}
}
return value;
}
}
}
class SampleType
{
public string Title { get; set; }
public DateTime Created_Date { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment