[英]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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.