How do I convert IsearchResponse <dynamic> to a C # class object?

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)));

      

On top of the Response, I want to convert the response object to a class object and class name which I am retrieving from the xml file.

+3


source to share


1 answer


In newer versions of NEST, we have introduced a IDocument

which allows you to do lazy deserialization on the appropriate type.

var response = objElastic.Search<IDocument>(s => s
     .Index("index1")
     .Type("DOCTYPE")
     .From(0).Size(10)
     .Source(sr => sr.Include(RequiredFields)
);

      

Now in response you can iterate over everything .Hits

and check the hit metadata and use that to deserialize for the type you want. eg,



.Hits.First().Source.As<MyDocument>()

      

As<>()

is a method on IDocument

+5


source







All Articles