Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get raw query from NEST client

Is it possible to get the raw search query from the NEST client?

var result = client.Search<SomeType>(s => s
                .AllIndices()
                .Type("SomeIndex")
                .Query(query => query
                    .Bool(boolQuery => BooleanQuery(searchRequest, mustMatchQueries)))
                );

I'd really like to debug why I am getting certain results.

like image 309
Mark Walsh Avatar asked Mar 09 '15 09:03

Mark Walsh


4 Answers

The methods to do this seem to change with each major version, hence the confusing number of answers. If you want this to work in NEST 6.x, AND you want to see the deserialized request BEFORE it's actually sent, it's fairly easy:

var json = elasticClient.RequestResponseSerializer.SerializeToString(request);

If you're debugging in Visual Studio, it's handy to put a breakpoint right after this line, and when you hit it, hover over the json variable above and hit the magnifying glass thingy. You'll get a nice formatted view of the JSON.

like image 131
Todd Menier Avatar answered Nov 17 '22 23:11

Todd Menier


You can get raw query json from RequestInformation:

var rawQuery = Encoding.UTF8.GetString(result.RequestInformation.Request);

Or enable trace on your ConnectionSettings object, so NEST will print every request to trace output

var connectionSettings = new ConnectionSettings(new Uri(elasticsearchUrl));
connectionSettings.EnableTrace(true);
var client = new ElasticClient(connectionSettings); 

NEST 7.x

Enable debug mode when creating settings for a client:

var settings = new ConnectionSettings(connectionPool)
    .DefaultIndex("index_name")
    .EnableDebugMode()
var client = new ElasticClient(settings); 

then your response.DebugInformation will contain information about request sent to elasticsearch and response from elasticsearch. Docs.

like image 39
Rob Avatar answered Nov 17 '22 23:11

Rob


For NEST / Elasticsearch.NET v6.0.2, use the ApiCall property of the IResponse object. You can write a handy extension method like this:

public static string ToJson(this IResponse response)
{
    return Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
}

Or, if you want to log all requests made to Elastic, you can intercept responses with the connection object:

var node = new Uri("https://localhost:9200");
var pool = new SingleNodeConnectionPool(node);
var connectionSettings = new ConnectionSettings(pool, new HttpConnection());
connectionSettings.OnRequestCompleted(call =>
{
    Debug.Write(Encoding.UTF8.GetString(call.RequestBodyInBytes));
});
like image 30
Paul Taylor Avatar answered Nov 17 '22 23:11

Paul Taylor


In ElasticSearch 5.x, the RequestInformation.Request property does not exist in ISearchResponse<T>, but similar to the answer provided here you can generate the raw query JSON using the Elastic Client Serializer and a SearchDescriptor. For example, for the given NEST search query:

var results = elasticClient.Search<User>(s => s
    .Index("user")
    .Query(q => q                    
        .Exists(e => e
            .Field("location")
        )
    )            
);

You can get the raw query JSON as follows:

SearchDescriptor<User> debugQuery = new SearchDescriptor<User>()
    .Index("user")
    .Query(q => q                    
        .Exists(e => e
            .Field("location")
        )
    )
;

using (MemoryStream mStream = new MemoryStream())
{
    elasticClient.Serializer.Serialize(debugQuery, mStream);
    string rawQueryText = Encoding.ASCII.GetString(mStream.ToArray());
}
like image 21
Henry C Avatar answered Nov 17 '22 22:11

Henry C