How do you debug your Nest queries?
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.
I like to take it a step further than bsarkar suggests and eliminate the need for a roundtrip altogether:
var client = new ElasticClient();
var seriesSearch = new SearchDescriptor<Series>();
seriesSearch.Filter(f => f
.Term<Role>(t => t.ReleasableTo.First(), Role.Visitor))
.SortDescending(ser => ser.EndDate)
.Size(1));
string searchJson = Encoding.UTF8.GetString(client.Serializer.Serialize(seriesSearch));
Note that your ElasticClient doesn't need any connection properties, so you have no dependency on an ES node.
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.
NEST
is Baroque of .NET APIs. For 2.1+ on call level:
IElasticClient client = new ElasticClient();
var searchDescriptor = new SearchDescriptor<Series>();
var query = Query<Series>.Term(...);
var pretty = query.ToPrettyString(query);
var json = client.ToRawRequest(searchDescriptor.Query(descriptor => query));
On configuration level:
var settings = new ConnectionSettings()
.PrettyJson().DisableDirectStreaming()
.OnRequestCompleted(details=> Debug.WriteLine(Encoding.UTF8.GetString(details.RequestBodyInBytes)));
On response level look into CallDetails.RequestBodyInBytes
.
Used extensions:
/// <summary>
/// Converts search to raw JSON request for debugging.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="self">The self.</param>
/// <param name="searchDescriptor">The search descriptor.</param>
/// <returns>The string.</returns>
public static string ToRawRequest<T>(this IElasticClient self, SearchDescriptor<T> searchDescriptor) where T : class
{
using (var output = new MemoryStream())
{
self.Serializer.Serialize(searchDescriptor, output);
output.Position = 0;
var rawQuery = new StreamReader(output).ReadToEnd();
return rawQuery;
}
}
/// <summary>
/// Prints query into string.
/// </summary>
/// <param name="self">The self.</param>
/// <returns>The value.</returns>
public static string ToPrettyString(this QueryContainer self)
{
using (var settings = new ConnectionSettings())
{
var visitor = new DslPrettyPrintVisitor(settings);
self.Accept(visitor);
return visitor.PrettyPrint.Replace(Environment.NewLine, string.Empty);
}
}