How to do an ElasticSearch using REST request body method and get the returned json formatted / pretty?


How to do an ElasticSearch using REST request body method and get the returned json formatted / pretty?



In issuing a search request, via REST Request Body method, such as


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



is there a parameter that can be added anywhere to request the returned response body's json be formatted/pretty ?



The same search using REST Request URI enables to do that, like


REST Request URI


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



How to achieve the same using REST request body ?


REST request body



Using ElasticSearch.NET's low level api, one have no control over the REST call, and can only provide the 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);



One can send a third parameter, a SearchRequestParameters object, I can't find any property there for that.


SearchRequestParameters



enter image description here





I am purposely not using Newtonsoft.Json.
– Veverke
Jul 1 at 14:33




1 Answer
1



You need to add to your request pretty=true
Like that:


pretty=true


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



for more reference check here



I didn't understand you at first, the pretty should be in the header of the request.
Try like that:


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



If you are using elstic.NET and you want too achieve pretty Jason.
You need to configure it in the connection.
Here is the method you should use(it's in the classConnectionConfiguration : ConnectionConfiguration<ConnectionConfiguration>):


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" } });
});



Here you can see the git





You are showing how I do that using the REST Request URI method, I want to know how I do the same via REST Request body (by the way, it is not necessary to assign "true" to pretty, as far as I recall)
– Veverke
Jul 2 at 6:47





I edited the answer, hope it would help. about the true, you are correct - I didn't know it is not necessary to assign true.
– Green
Jul 2 at 18:53





Well, (I believe, not sure) I asked the question with ElasticSearch.NET low level api in mind. using ElasticSearch.net (low level) api. One has no control over the REST call itself. Try to do that with ElasticSearchClient. I edited the question. Sorry for the misunderstanding.
– Veverke
Jul 3 at 9:00






Why don't you work with Nest? there, I know for a fact it exist. (I can update with nest example if you will need)
– Green
Jul 3 at 9:20





Haha, It became personal.
– Green
Jul 3 at 10:35







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?