使用 HttpClient 执行查询搜索 - elasticsearch

Using HttpClient to perform a query search - elasticsearch

我正在尝试使用 httpclient 在 elasticsearch 中执行 GET 查询搜索。但是,每当我这样做时,我都会在点击 HttpResponseMessage response 变量时收到错误消息。它告诉我这是一个 400(错误的请求)。

我检查了我的 jsonQuery 并复制了那个 jsonObject 并将其粘贴到 Kibana 的 CLI 中,实际上我在 CLI 中得到了结果。所以我不明白为什么我会收到 400 错误请求。

如果我在 kibana 的 CLI 中执行 GET /"indexname"/_search,我需要使用 .PostAsJsonAsync() 吗?

public string GetVirtualSupport()
        {

            var query = "{\"size\": 1000,\"query\": {\"bool\": {\"should\":[ {\"match\": { \"level\": \"Information\" } }, {\"match\": { \"level\": \"Error\" } } ], " +
                        "\"filter\": [ { \"range\": { \"@timestamp\": { \"gte\": \"2021-07-26T07:58:45.304-05:00\", \"lt\": \"2021-07-26T08:58:45.305-05:00\" } } } ]," +
                        "\"minimum_should_match\": 1 } } }";


            var jsonQuery = JObject.Parse(query);

            Console.WriteLine(jsonQuery);


            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:9200/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.PostAsJsonAsync("customer-simulation-es-app-logs*/_search", jsonQuery).Result;


            if (response.IsSuccessStatusCode)
                {
                    var contents = response.Content.ReadAsStringAsync().Result;

                    Console.WriteLine(contents);
                    
                    return contents;
                }
                else
                {
                    Console.WriteLine("{0} ({1}) \n {2}", (int)response.StatusCode, response.ReasonPhrase, response.RequestMessage);
                    return response.ToString();
                }

            
            
        }

在 console.exe 我得到:

{
  "size": 1000,
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "level": "Information"
          }
        },
        {
          "match": {
            "level": "Error"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "2021-07-26T07:58:45.304-05:00",
              "lt": "2021-07-26T08:58:45.305-05:00"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

400 (Bad Request)
 Method: POST, RequestUri: 'http://localhost:9200/customer-simulation-es-app-logs*/_search', Version: 1.1, Content: System.Net.Http.Json.JsonContent, Headers:
{
  Accept: application/json
  Transfer-Encoding: chunked
  Content-Type: application/json; charset=utf-8
}

Actual results inside of the CLI when running the query search:

{
  "took" : 53,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 101,
      "relation" : "eq"
    },
    "max_score" : 4.116497,
    "hits" : [
      {
        "_index" : "customer-simulation-es-app-logs-development-2021-07",
        "_type" : "_doc",
        "_id" : "xYAa43oBzNYm7dmwVxvD",
        "_score" : 4.116497,
        "_source" : {
          "@timestamp" : "2021-07-26T08:56:26.6984506-05:00",
          "level" : "Error",
          "messageTemplate" : "Empty textfield!",
          "message" : "Empty textfield!",
          "exceptions" : [
            {
              "Depth" : 0,
              "ClassName" : "System.Exception",
              "Message" : "Looks like you did not type something!",
              "Source" : "CustomerSimulatorApp",
              "StackTraceString" : """   at CustomerSimulatorApp.Controllers.SecondController.SecIndex(TextInput form) in C:\Users\user\App\Controllers\SecondController.cs:line 43""",
              "RemoteStackTraceString" : null,
              "RemoteStackIndex" : 0,
              "HResult" : -2146233088,
              "HelpURL" : null
            }
       
       .........................

会不会是.PostAsJsonAsync()的问题?如果我想检索结果,那是正确的方法吗?不确定是什么导致我收到 400 错误请求。 Size 只要在单词查询之前就可以工作。我什至删除了它并尝试查看它是否有效,但我仍然收到相同的 400 错误请求消息。

您可以使用现有的 json 字符串。试试这个,它可能有效

var contentData = new StringContent(query, Encoding.UTF8, "application/json");
var response =  client.PostAsync("customer-simulation-es-app-logs*/_search", contentData).Result;