Get raw query from NEST client

Mark Walsh picture Mark Walsh · Mar 9, 2015 · Viewed 27.9k times · Source

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.

Answer

Todd Menier picture Todd Menier · Jun 20, 2018

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.