2
votes

I'm trying to find the place in the NEST API (or via the underlying ElasticSearch.NET API at the time of using NEST) where I can see the JSON created by a NEST query. This is my problem summed up in a partial code sample -

using NEST;

// create any query 
var elasticResponse = elasticClient.Search<Hotel>(s => s
   .Query(q => // ... build up the rest of my query ... 

elasticResponse.ApiCall.HttpMethod // is "POST" - good! 
elasticResponse.ApiCall.HttpStatusCode // is 200 (OKAY) - good! 
elasticResponse.ApiCall.Success // is true - good! 

// ISSUE: 
// I've tried viewing the generated JSON through these members (in no particular order) without success ... 

elasticResponse.ApiCall.RequestBodyInBytes // is always empty before and after use of the elasticResponse object. 

elasticResponse.ApiCall.DebugInformation // doesn't provide useful info about body. 

elasticResponse.CallDetails.RequestBodyInBytes // is always null.

I'm using Elastic Search 2.3.1 and NEST 2.4.1.

The places in which I expect to see the POST data or generated query don't have it; they are either empty or null, and no error condition is showing because the query was successful.

I have also looked through available members of the query descriptor while building up the query, with the thought I might be able to capture the RESTful call body immediately after I finish building the query, but haven't found anything useful yet.

ConnectionSettings turn up a .OnRequestCompletedcallback but that doesn't seem to reveal the query either.

I've skimmed the official documentation to no avail yet. Looking for some help. Thank you.

1
Closely related is this question that involves Glimpse stackoverflow.com/questions/38105355/… - John K

1 Answers

3
votes

I found one answer through use of configuration settings -

Set ConnectionSettings(instance) Disable Direct Streaming property to true with .DisableDirectStreaming(true); and pass it to your new Elastic NEST client object. This will tell Elastic to buffer request and response and cause both values to become available on the .RequestBodyInBytes and .ResponseBodyInBytes properties respectively.

You can then decode the bytes using .NET e.g.

global::System.Text.UTF8Encoding.Default.GetString(elasticResponse.ApiCall.RequestBodyInBytes);

and see the original query, for example in my case -

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "shortDescription": {
                    "value": "mile"
                  }
                }
              },
              {
                "term": {
                  "shortDescription": {
                    "value": "museum"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "name": {
                    "boost": 1.1,
                    "value": "pacific"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}