elasticsearch,C#,Http,elasticsearch" /> elasticsearch,C#,Http,elasticsearch" />

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

如何通过HTTP从C#查询Elasticsearch?,c#,http,elasticsearch,C#,Http,elasticsearch,我正在尝试用C语言编写代码,用于输入和获取我的elasticsearch数据。 我像这样输入PUT的代码,它似乎可以工作: string url = "http://localhost:9200/my_index/my_type/"; JsonDocFormat json = new JsonDocFormat() { name = "John" }; string s = JsonConvert.SerializeObject(json); using (var client =

我正在尝试用C语言编写代码,用于输入和获取我的elasticsearch数据。 我像这样输入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);
}
但是我不能为这个GET编写代码:

GET my_index/my_type/_search
{
   "query" : {
       "match" : {
            "name" : "john"
        }
    }
}
GET /my_index/my_type/_search
我试过这样的方法:

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));
}
但此代码返回的输出与此GET相同:

GET my_index/my_type/_search
{
   "query" : {
       "match" : {
            "name" : "john"
        }
    }
}
GET /my_index/my_type/_search
它没有抛出任何错误,但它完全忽略了URL末尾的json主体。我想在没有任何外部包(如NEST或Elasticsearch.NET)的情况下编写这个,只使用HTTP


提前感谢您的帮助

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

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));
}

字符串s1不应该是“{\”查询\“:{\”匹配\“:{\”名称\“:\”约翰\“}}}”?抱歉,只是一个错误。。我试着让它更容易理解。在我的原始文档中,我使用了字段“text”,但在这里我将其重命名为“name”,但我忘了在那段代码中覆盖特定的“text”。但我的问题仍然存在。能否解释一下“漂亮”在你的url请求中指的是什么?@EmnaJaoua谢谢你的回复。我也在尝试使用HttpClient库做同样的事情(因为我需要使用.Net Client 2.2 framework)。我正在通过client.BaseAddress和client.DefaultRequestHeaders.Authorization定义url和凭据,但当我通过response=wait client.GetAsync(path)获得响应时,响应未成功,因此我无法发出请求。我使用的查询格式与您的完全相同(字符串s)。请提供有用的帮助链接?您的回答使用了POST动词。出于安全原因,我想用GET动词找到一个解决方案。GNU/LinuxCurl将允许您使用GET动词发送消息正文,所以我不明白为什么那么多其他WebRequest工具不允许它。从我在其他地方读到的内容来看,发送带有GET请求的请求体是“奇怪的”,但标准并没有明确禁止它。