在通过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
对象,我在那里找不到任何属性。
您需要pretty=true
像这样添加到您的请求中:
GET /bank/_search?q=*&sort=account_number:asc&pretty=true
有关更多参考,请点击此处
我一开始不了解您,应该在请求的标题中。
像这样尝试:
GET /bank/_search?pretty=true
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
如果您使用的是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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句