1

在发出搜索请求时,通过 REST Request Body 方法,如

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

是否可以在任何地方添加一个参数来请求返回的响应正文的 json 被格式化/漂亮?

使用相同的搜索REST Request URI使能做到这一点,比如

GET /bank/_search?q=*&sort=account_number:asc&pretty

如何实现相同的使用REST request body

使用 ElasticSearch.NET 的底层 api,无法控制 REST 调用,只能提供 POST json。

var esClient = new ElasticLowLevelClient(_connectionSettings);
//postDataJson is the json depicted in the question's body
var postData = PostData.String(postDataJson); 
var response = esClient.Search<StringResponse>("myIndex", postData);

可以发送第三个参数,一个SearchRequestParameters对象,我在那里找不到任何属性。

在此处输入图像描述

4

1 回答 1

1

您需要添加到您的请求pretty=true
中:

GET /bank/_search?q=*&sort=account_number:asc&pretty=true

如需更多参考,请在此处查看

编辑

起初我不明白你,漂亮应该在请求的标题中。
试试这样:

GET /bank/_search?pretty=true
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

编辑 2

如果您使用的是elstic.NET,并且您也想实现漂亮的 Jason。
您需要在连接中配置它。这是您应该使用的方法(它在类中ConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>)

    /// <summary>
    /// Forces all requests to have ?pretty=true querystring parameter appended,
    /// causing Elasticsearch to return formatted JSON.
    /// Also forces the client to send out formatted JSON. Defaults to <c>false</c>
    /// </summary>
    public T PrettyJson(bool b = true) => Assign(a =>
    {
        this._prettyJson = b;
        const string key = "pretty";
        if (!b && this._queryString[key] != null) this._queryString.Remove(key);
        else if (b && this._queryString[key] == null)
            this.GlobalQueryStringParameters(new NameValueCollection { { key, "true" } });
    });

在这里你可以看到git

于 2018-07-01T14:52:33.800 回答