is there a way to deserialize Elasticsearch Nest search query?
upon building my Elasticsearch query using Nest, i want to be able to see the JSON version of what's being sent to Elasticsearch. is this possible? some sort of deserializer i suppose.
here's the info for my follow up question:
{
"_infer": {
"defaultIndex": "myindex"
},
"acknowledged": false,
"isValid": false,
"connectionStatus": {
"success": false,
"requestMethod": "POST",
"requestUrl": "http://localhost:9200/myindex",
"settings": {},
"request": "ew0KICAic2V0dGluZ3MiOiB7DQogICAgImluZGV4Ijogew0KICAgICAgImFuYWx5c2lzIjogew0KICAgICAgICAiYW5hbHl6ZXIiOiB7DQogICAgICAgICAgImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCIsDQogICAgICAgICAgICAgICJsb3dlcmNhc2UiDQogICAgICAgICAgICBdLA0KICAgICAgICAgICAgInR5cGUiOiAiY3VzdG9tIg0KICAgICAgICAgIH0sDQogICAgICAgICAgImZ1bGxUZXJtIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCINCiAgICAgICAgICAgIF0sDQogICAgICAgICAgICAidHlwZSI6ICJjdXN0b20iDQogICAgICAgICAgfSwNCiAgICAgICAgICAiY2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJsb3dlcmNhc2UiLA0KICAgICAgICAgICAgImZpbHRlciI6IFsNCiAgICAgICAgICAgICAgInN0YW5kYXJkIg0KICAgICAgICAgICAgXSwNCiAgICAgICAgICAgICJ0eXBlIjogImN1c3RvbSINCiAgICAgICAgICB9DQogICAgICAgIH0NCiAgICAgIH0NCiAgICB9DQogIH0sDQogICJtYXBwaW5ncyI6IHsNCiAgICAic2VhcmNoZGF0YSI6IHsNCiAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAidXBjIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgInByb2R1Y3RpZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ0aXRsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdG9yZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAibGFiZWwiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAidmVuZG9yIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImF2YWlsYWJsZWRhdGUiOiB7DQogICAgICAgICAgInR5cGUiOiAiZGF0ZSINCiAgICAgICAgfSwNCiAgICAgICAgInJlbGVhc2VkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJsYXN0cmV0dXJuZGF0ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkYXRlIg0KICAgICAgICB9LA0KICAgICAgICAibm9ucmV0dXJuYWJsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtcGFhIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImVzcmIiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAiZXNyYiI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiY3NucmZsYWciOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiY292ZXIiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAic2l6ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJjb2xvciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdHlsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJxb2giOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImFkdWx0Ijogew0KICAgICAgICAgICJ0eXBlIjogImludGVnZXIiDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc3JhbmsiOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImxpc3RwcmljZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkb3VibGUiDQogICAgICAgIH0sDQogICAgICAgICJzZWxsaW5ncHJpY2UiOiB7DQogICAgICAgICAgInR5cGUiOiAiZG91YmxlIg0KICAgICAgICB9LA0KICAgICAgICAiY29zdHByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogImRvdWJsZSINCiAgICAgICAgfSwNCiAgICAgICAgInNwZWNpYWxvcmRlciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJmbGFncyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc2NhdGVnb3J5Ijogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImFydGlzdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJkaXJlY3RvcnMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJzdGFycmluZyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJvYmplY3QiLA0KICAgICAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAgICAgIm5hbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybSIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgImlkIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgImNyZWRpdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJhZGRpdGlvbmFsdGl0bGVzIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgImFuYWx5emVyIjogImNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgfSwNCiAgICAgICAgImF0dHJpYnV0ZXMiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAic3BlY2lhbHR5Ijogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInNwZWNpYWx0eSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiZ2VucmVzIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgImdlbnJlcyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybUNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgICAgIH0sDQogICAgICAgICAgICAicmF3Ijogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJ0cmFja3MiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAiZGVsZXRlZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ1cGRhdGVkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJwcm9jZXNzZWQiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiZGVjYWRlcyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJkZWNhZGVzIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwZXJpb2QiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAicGVyaW9kIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9DQogICAgICB9DQogICAgfQ0KICB9DQp9",
"numberOfRetries": 0,
"httpStatusCode": 400,
"serializer": {},
"successOrKnownError": true
},
"infer": {
"defaultIndex": "myindex"
}
}
Solution 1:
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.
Solution 2:
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).