How to convert ISearchResponse to C# Class Object.
I am trying to convert to Class Object where my Class name will be dynamic.
ISearchResponse<dynamic> bResponseNewLoop =
objElastic.Search<dynamic>(s => s
.Index("index1")
.Type("DOCTYPE")
.From(0)
.Size(10)
.Source(sr => sr.Include(RequiredFields)));
From above Response , i want to convert the response object to class object and The class name i am retriving from xml file.
In newer NEST versions we introduced IDocument
which allows you to do lazy deserialization to the proper type.
var response = objElastic.Search<IDocument>(s => s
.Index("index1")
.Type("DOCTYPE")
.From(0).Size(10)
.Source(sr => sr.Include(RequiredFields)
);
Now on response you can loop over all the .Hits
and inspect the hit metadata and use that to deserialize to the type that you want. e.g
.Hits.First().Source.As<MyDocument>()
As<>()
is a method on IDocument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With