is there a way to deserialize Elasticsearch Nest search query?

Yup. You can use the serializer that is exposed by ElasticClient like this:

var query = Nest.Query<SomeObject>...
var json = Client.Serializer.SerializeToString(query);

You can also use Newtonsoft directly, or any JSON library for that matter to serialize your query object. Using the serializer in ElasticClient though (which essentially wraps Newtonsoft), will give you the exact JSON that NEST will generate.

Checkout how the unit tests are done in NEST for more details, specifically this.


If the above answer doesn't work, or you want a simpler one, I posted another way here on what is probably a duplicate, (but differently worded) question: https://stackoverflow.com/a/31247636/261405


I used SearchDescriptor like this for a complex query:

SearchDescriptor<T> sd = new SearchDescriptor<T>()
.From(0).Size(100)
    .Query(q => q
        .Bool(t => t
            .Must(u => u
                .Bool(v => v
                    .Should(
                        ...
                    )
                )
            )
        )
    );

And got the deserialized JSON like this:

{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              ...
            ]
          }
        }
      ]
    }
  }
}

It was annoying, NEST library should have something that spits out the JSON from request. However this worked for me:

using (MemoryStream mStream = new MemoryStream()) {
    client.Serializer.Serialize(sd, mStream);
    Console.WriteLine(Encoding.ASCII.GetString(mStream.ToArray()));
}

NEST library version: 2.0.0.0. Newer version may have an easier method to get this (Hopefully).