7

Since upgrading my Nest client to 2.2.1 I'm unable see the query I'm submitting to my elastic search client (now version 2.3.0). I used to use this line:

string searchJson = Encoding.UTF8.GetString(client.Serializer.Serialize(myQueryHere));  

But this method now returns void instead of the JSON it used to. ConnectionStatus also doesn't exist so I can no longer see the json i'm sending, does anyone know of a way? CallDetails.RequestBodyInBytes is available but that returns null.

zappa
  • 791
  • 8
  • 16

2 Answers2

8

Take a look at the documentation for NEST 2.x on Connecting. CallDetails.RequestBodyInBytes will be null unless you set .DisableDirectStreaming() on ConnectionSettings that is passed to the constructor of ElasticClient

var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));  
var settings = new ConnectionSettings(connectionPool) 
    .DisableDirectStreaming();

var client = new ElasticClient(settings);

now a copy of the request and response bytes will be exposed on the response CallDetails

var response = client.Search<Document>();

var requestJson = Encoding.UTF8.GetString(response.CallDetails.RequestBodyInBytes);
var responseJson = Encoding.UTF8.GetString(response.CallDetails.ResponseBodyInBytes);

Whilst developing it may be useful to log out all requests and responses.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • Excellent, thanks very much. (I tried to upvote but I need a score of 15) – zappa Apr 12 '16 at 07:29
  • No problem. Feel free to upvote and accept the answer :) – Russ Cam Apr 12 '16 at 07:30
  • Unfortunately this relies on my calling the Search method in order to see what's in my request, in my original query I could see what I was ABOUT to send without actually sending it, is there a way to do this? – zappa Apr 20 '16 at 09:57
  • 3
    Yes, pass your request to `client.Serializer.SerializeToString(request);` for json string or `client.Serializer.SerializeToBytes(request)` for a byte array – Russ Cam Apr 20 '16 at 10:00
  • 3
    Note that the `SerializeToString` and `SerializeToBytes` are extension methods and you need a `using Elasticsearch.Net;` – gwenzek Jun 16 '16 at 10:12
1

Serialize method now requires stream, on which it will write raw json query - Working properly For Nest 5.3.0 :

        var stream = new System.IO.MemoryStream();
        nestClient.Serializer.Serialize(query, stream);
        var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());
Jay Shah
  • 3,553
  • 1
  • 27
  • 26