I\'m new to Nest, and I very likely am not creating my query like I think I am. My question is more along the lines of teach a man to fish rather than give me a fish. However,
Using the latest elastic search 5+, I was able to get mine (thanks to Adrian Carr's method) with the following:
var jsonOutput = System.Text.Encoding.UTF8.GetString(
response.ApiCall.RequestBodyInBytes
)
Which gave me the following output:
{
"from":0,
"size":0,
"query":{
"bool":{
...
}
}
}
You can use EnableTrace
or ConnectionStatusHandler
. More details here.
Elasticsearch.Net and NEST: the .NET clients [7.x]
ref: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/debug-information.html`
ref code
Var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool)
.DisableDirectStreaming(true);
var client = new ElasticClient(settings);
2.) var response = client.Search<Project>(s =>....)
3.)
var jsonOutput = System.Text.Encoding.UTF8.GetString(
result.ApiCall.RequestBodyInBytes);
Above can be achieved on a per request basis with following.
var response = client.Search<Project>(s => s
.RequestConfiguration(r => r
.DisableDirectStreaming()
)
.Query(q => q
.MatchAll()
)
);
Really easily. If this is my code that searches:
var results = client.Search<SearchItem>(s => s.AllIndices()
.Query(q =>
q.Term(p => p.LastName, searchItem.LastName)
&& q.Term(p => p.FirstName, searchItem.FirstName)
&& q.Term(p => p.ApplicationCode, searchItem.ApplicationCode)
)
.Size(1000)
);
var list = results.Documents.ToList();
I then set a breakpoint on the line above. Then, in Visual Studio Immediate Window, I enter this:
?results.ConnectionStatus
and it gives me this:
{StatusCode: 200,
Method: POST,
Url: http://localhost:9200/_all/searchitem/_search,
Request: {
"size": 1000,
"query": {
"bool": {
"must": [
{
"term": {
"lastName": {
"value": "carr"
}
}
},
{
"term": {
"firstName": {
"value": "adrian"
}
}
}
]
}
}
}
Hope this helps.
You can get the values of search request URL and JSON request body as under:
var requestURL = response.RequestInformation.RequestUrl;
var jsonBody = Encoding.UTF8.GetString(response.RequestInformation.Request);
You can find other useful properties in RequestInformation
for debugging.
Tested in Nest 6.2.0
You can now just do:
#if DEBUG
connectionSettings.EnableDebugMode(details =>
{
Logger.Debug($"ES Request: {Encoding.UTF8.GetString(details.RequestBodyInBytes ?? new byte[0])}");
Logger.Verbose($"ES Response: {Encoding.UTF8.GetString(details.ResponseBodyInBytes ?? new byte[0])}");
});
#endif
The call EnableDebugMode
will automatically call DisableDirectStreaming
, PrettyJson
, and then OnRequestCompleted
, passing in the function you gave it.
NOTE: There may be a bug somewhere in their response stuff. The response seems to be lacking some closing brackets for some reason.