首页 > 解决方案 > Kibana 帖子搜索 - 应为 [START_OBJECT],但找到了 [VALUE_STRING]

问题描述

请帮我解决这个问题。

我有一个这样的 .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

查询DslKibana 如下:

{"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
 }

请帮忙 :)

标签: c#kibanarest-clientelasticsearch-dsl

解决方案


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

像这样的东西:

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

这应该会产生您需要的 JSON:

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

推荐阅读