Search code examples
c#elasticsearchnest

How to get in ElasticSearch derived class NEST


I create index my type Product. Product inside has Guide named object. Guide is base class and inherit class that example Guide1 Guide2 Guide3 vs..

I create Product object and index elastic after again create product class but This time Guide1 class in Product.

I am getting by id product nest not cast Guide1 to product.

public class Product{
   public string Name{get;set;}

   public Guide Guide{get;set;}
}

public class Guide{
    public int ID{get;set;}

    public string Title{get;set;}
}

public class Guide1 :  Guide{
    public double Price{get;set;}
}


Product p1 elasticContext.Get<Product>(1, idx => idx.Index("products"));

Guide 1 cast to Guide and I am not access Guide1 property


Solution

  • When you call the record type in the nest connection setting, the full type returns

    new ConnectionSettings(node, sourceSerializer: (b, s) => new JsonNetSerializer(b, s, () => new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }));