How to request Elasticsearch with C # via HTTP?

I am trying to write code in C # for PUT and GET for my elasticsearch data. I typed the code for PUT like this and it seems to work:

string url = "http://localhost:9200/my_index/my_type/";

JsonDocFormat json = new JsonDocFormat()
{
   name = "John"
};

string s = JsonConvert.SerializeObject(json);

using (var client = new WebClient())
{
   client.UploadString(url, "POST", s);
}

But I can not write code for this GET:

GET my_index/my_type/_search
{
   "query" : {
       "match" : {
            "name" : "john"
        }
    }
}

I tried something like this:

string url_req = "http://localhost:9200/my_index/my_type/_search?pretty";

string s1 = "{\"query\": {\"match\": { \"name\" : \"john\" }}}";

string s_req = url_req + s1;

using (var client = new WebClient())
{
    Console.Write(client.DownloadString(s_req));
}

But this code returned the same result as for this GET:

GET /my_index/my_type/_search

It did not return any errors, but completely ignored the json body at the end of the URL. I want to write this without any external package (like NEST or Elasticsearch.NET), just with HTTP.

Thanks in advance for your help!

+4
source share
1 answer

The final solution for my question in this code

string url_req = "http://localhost:9200/my_index/my_type/_search?pretty";

string s = "{\"query\": {\"match\": { \"name\" : \"john\" }}}";

using (var client = new WebClient())
{
    Console.Write(client.UploadString(url_req, "POST", s));
}
0
source

Source: https://habr.com/ru/post/1613552/


All Articles