elasticsearch - 如何使用 REST 请求正文方法执行 ElasticSearch 并获取返回的 json 格式/漂亮?
问题描述
在发出搜索请求时,通过 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" }
]
}
编辑 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
推荐阅读
- python - AttributeError:模块“urllib.response”没有属性“status_code”
- cmake - CMake 不会将 /usr/include 添加到 include_directories 列表中
- python - 带有 Tkinter 的 Python 代码简单计算器我是 Python 中的菜鸟。有可能吗?
- java - 如何执行 maven 目标但忽略潜在的本地 pom.xml?
- python - 如何强制 celery 应用动态时间链任务?
- reactjs - 如何从 useSelector 初始化 useState
- python - 应该如何调用使用 super() 检索的可调用对象?
- haskell - 使用一些“Coercible”用例避免“unsafeCoerce”
- flutter - Flutter,我无法在 TabView 中增加图标的大小
- javascript - 递归函数返回'未定义'