Skip to content

Instantly share code, notes, and snippets.

@shurane
Created April 8, 2018 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurane/f769d77b577cf1dbffb8191c9e9bc348 to your computer and use it in GitHub Desktop.
Save shurane/f769d77b577cf1dbffb8191c9e9bc348 to your computer and use it in GitHub Desktop.
Working with a dataset with ElasticSearch and C#
using System;
using System.Net.Http;
using System.Web;
namespace ElasticSearchRequests
{
class Program
{
static void Main(string[] args)
{
var client = new HttpClient();
// for comparison with await/async style https://stackoverflow.com/a/26598059/198348
var result = client.GetAsync("http://localhost:9200/").Result;
var content = result.Content.ReadAsStringAsync().Result;
Console.WriteLine("Checking for connection to ElasticSearch");
Console.WriteLine(content);
Console.WriteLine("==================================================");
while (true)
{
Console.WriteLine("What do you want to search? Press \"/quit\" to quit");
var query = Console.ReadLine();
if (query == "/quit")
break;
var encoded = HttpUtility.UrlEncode(query);
Console.WriteLine($"query: {query}");
Console.WriteLine($"encoded: {encoded}");
Console.WriteLine("==================================================");
Console.WriteLine("Searching...");
// shakespeare data set: https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html
// elasticsearch URI search https://www.elastic.co/guide/en/elasticsearch/reference/6.x/search-uri-request.html
result = client.GetAsync($"http://localhost:9200/shakespeare/_search?pretty&q={encoded}").Result;
content = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(content);
}
Console.WriteLine("Peace <Expletive>!!!!!!!!!!!!!!!!!");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment