如何通过HTTP从C#查询Elasticsearch?

Gon*_*dil 5 c# http elasticsearch

我正在尝试用C#编写代码,用于PUT和GET到我的弹性搜索数据.我像这样为PUT键入代码,它似乎有效:

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);
}
Run Code Online (Sandbox Code Playgroud)

但我不能为这个GET编写代码:

GET my_index/my_type/_search
{
   "query" : {
       "match" : {
            "name" : "john"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试过这样的事情:

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));
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码返回了与此GET相同的输出:

GET /my_index/my_type/_search
Run Code Online (Sandbox Code Playgroud)

它没有抛出任何错误,但它绝对忽略了URL末尾的json体.我想在没有任何外部包(如NEST或Elasticsearch.NET)的情况下编写它,只需使用HTTP.

在此先感谢您的帮助!

Gon*_*dil 2

我的问题的最终解决方案在这段代码中

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));
}
Run Code Online (Sandbox Code Playgroud)