NEST: Is it possible / could it be made possible to load json file into an ISearchRespone object?

I currently am running queries using NEST (ElasticClient.Search), but I don't do anything with the response objects, I just store the returned JSON.

The reason for this is that just storing statistics is much smaller then keeping around all the data in the ElasticSearch indexes.

At some point later, someone can come along and query the statistics I've stored to disks, but ideally the server would take the JSON file and process it first before displaying it in the UI.

Is there some way to simply load into the ISearchResult object just from the JSON I've stored to disk?

Thank you in advance.

Yes. If you know the type T that is represented in the json documents then you can use

SearchResponse<T> searchResponse = null;

using (var stream = File.OpenRead("path-to-json.json"))
    searchResponse = client.Serializer.Deserialize<SearchResponse<T>>(stream);

if you don't know the type, then you can use dynamic, which will deserialize each document to a Json.Net JObject

SearchResponse<dynamic> searchResponse = null;

using (var stream = File.OpenRead("path-to-json.json"))
    searchResponse = client.Serializer.Deserialize<SearchResponse<dynamic>>(stream);
1 Like

Thank you!

Random asides:

  • I did check that the serialized response object and the deserizalized and re-serialized response object from the response json on disk were the same. Check!
  • In my cases, the serialized response object is about four times the size of the response json.

Super helpful thanks again!