Kibana帖子搜索-预期[START_OBJECT],但发现[VALUE_STRING]

时间:2018-07-31 15:11:26

标签: c# kibana rest-client elasticsearch-dsl

请帮助我解决这个问题。

我有一个.net核心客户端,如下所示:

var client = new RestClient();
        client.BaseUrl = new Uri(Host);
        client.AddDefaultHeader("Content-Type", "application/json");

        var request = new RestRequest();
        request.Resource = "_search";           
        request.AddJsonBody(queryDslKibana);
        request.Method = Method.POST;
        request.AddHeader("Content-Type", "application/json");
        request.RequestFormat = DataFormat.Json;

Uri:    http://URL:PORT/_search

queryDslKibana如下:

{"query":{"match":{"message":".Txt"}}} 



It runs on postman gracefully but the response on .net is: 

 {
    "error": {
        "root_cause": [{
            "type": "parsing_exception",
            "reason": "Expected [START_OBJECT] but found [VALUE_STRING]",
            "line": 1,
            "col": 1
        }],
        "type": "parsing_exception",
        "reason": "Expected [START_OBJECT] but found [VALUE_STRING]",
        "line": 1,
        "col": 1
    },
    "status": 400
 }

请帮助:)

2 个答案:

答案 0 :(得分:0)

在我看来,变量“ queryDslKibana”没有合适的JSON格式,当使用方法“ AddJsonBody ()”时,对象具有合适的格式很重要。 “ AddJsonBody ()”方法会序列化您发送的对象,因此您应该首先尝试使用匿名对象。

类似的东西:

var requestObject = new {query = new {match = new {message = ".txt"}}};

这将产生您需要的JSON:

{"query": {"match": {"message": ". Txt"}}}

答案 1 :(得分:0)

感谢@michael。

最终代码是:

将kibana api连接到_search端点。

问题是.net RestClient,因为我必须按照您所说的发送对象(匿名对象或强类型对象)...

代码的答案是:

        var client = new RestClient();
        client.BaseUrl = new Uri(Host);
        client.AddDefaultHeader("Content-Type", "application/json");

        var request = new RestRequest();
        request.Resource = "_search";
        //{"query":{"match":{"message":"SEPP"}}}
        request.AddJsonBody(new { query = new { match = new { message = "SEPP" } } });
        request.Method = Method.POST;
        request.AddHeader("Content-Type", "application/json");
        request.RequestFormat = DataFormat.Json;

        IRestResponse response = client.ExecutePostTaskAsync(request).Result;