2016-04-12 24 views
1

Kullanıcıların kalıcı olarak saklanması için NEST 2.X ve Elastic Search kullanmaya çalışıyorum. Her Kullanıcı, platformumuzdaki izinlerini tanımlayan bir Rol listesi içerir. Ben ElasticSearch verileri depolamak herhangi bir sorun yokNEST ve ElasticSearch ile Arabirim Türetilmiş Sınıflarını Kullanma

public class MediaDetectionUser 
{ 
    public string Username { get; set; } 
    public ICollection<IMediaDetectionRole> Roles { get; set; } 

    public MediaDetectionUser() 
    { 
     Roles = new List<IMediaDetectionRole>(); 
    } 
} 

public interface IMediaDetectionRole 
{ 
    string Name { get; } 
    string GetDescription(); 
    string GetRoleType {get;} 
} 

[ElasticsearchType(Name="MediaDetectionAdminRole")] 
public class MediaDetectionAdminRole : IMediaDetectionRole 
{   
    public string Name { get { return "Admin"; } } 
    public string GetDescription() { return "Admin users can create other users within the account"; } 
    public string GetRoleType { get { return this.GetType().Name; } } 
} 

[ElasticsearchType(Name = "MediaDetectionManagerRole")] 
public class MediaDetectionManagerRole : IMediaDetectionRole 
{ 
    public string Name { get { return "Manager"; } } 
    public string GetDescription() { return "Managers can modify account-level properties"; } 
    public string GetRoleType { get { return this.GetType().Name; } } 
} 

[ElasticsearchType(Name = "MediaDetectionCreatorRole")] 
public class MediaDetectionCreatorRole : IMediaDetectionRole 
{ 
    public string Name { get { return "Creator"; } } 
    public string GetDescription() { return "Creators can create new Media Detection Profiles"; } 
    public string GetRoleType { get { return this.GetType().Name; } } 
} 

ama veriler, veri ANDAE sorgulamak giderken can: Orada her bir IMediaDetectionRole arayüzünden kaynaklanan Rollerin birkaç farklı türleri vardır

Bu rollerin türünü anlamaya. Ben olsun:

Could not create an instance of type IMediaDetectionRole. 
Type is an interface or abstract class and cannot be instantiated. 
Path 'hits.hits[0]._source.roles[0].name', line 1, position 343. 

geri doğru sınıf türüne yuvalanmış nesne harita verilerinde doğru yolu nedir?

Çok teşekkürler!

-Z

+1

Bu tam kullanım durumu belgelerinde açıklanmıştır. Https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/covariant-search-results.html :) –

+0

Teşekkürler Martijn, I Bunun en üst düzey bir nesne için nasıl işe yaradığını görün. Ancak bu örnekler MediaDetectionUser nesnesindeki bir listenin parçasıdır. İç içe bir nesnenin türlerini çözmek için .Types() veya ConcreteTypeSelector'ı nasıl kullanacağım açık değil. – zorlack

+1

DTO içindeki koleksiyonunuz için [JsonPropery (ItemTypeHandling = TypeHandling.Objects] kullanabilirsiniz, bu seri hale getirilmiş sonuca $ türünü ekleyecektir ve json.net bunu doğru sınıfa geri gönderebilecektir –

cevap

1

Tamam, bu yüzden bu bir JSON.NET sorununun NEST/ES sorununun daha az ve daha çok sanırım. Görünüşe göre bu problemin çözümü JSON.NET'e listenin her bir üyesi için tip ipuçları sağlamasını anlatmayı içerir.

Her MartijnLaarman'ın önerisi, Roles özelliğine bir [JsonProperty] özniteliği ekledim. Aşağıya bakın:

Roles: [ 
    { 
    $type: "MediaDetectionFrontend.ServiceModel.Types.MediaDetectionAdminRole, MediaDetectionFrontend.ServiceModel", 
    name: "Admin", 
    getRoleType: "MediaDetectionAdminRole" 
    }, 
    { 
    $type: "MediaDetectionFrontend.ServiceModel.Types.MediaDetectionCreatorRole, MediaDetectionFrontend.ServiceModel", 
    name: "Creator", 
    getRoleType: "MediaDetectionCreatorRole" 
    }, 
    { 
    $type: "MediaDetectionFrontend.ServiceModel.Types.MediaDetectionEditorRole, MediaDetectionFrontend.ServiceModel", 
    name: "Editor", 
    getRoleType: "MediaDetectionEditorRole" 
    } 
] 

Sen $ türü özelliği artık her elemanın tam tip tanımlayıcısı sağladığını görebilirsiniz: o ElasticSearch içindeki _source göründüğü gibi İşte

public class MediaDetectionUser 
{ 
    public string Username { get; set; } 

    //This JsonProperty helps reference the types during deserialization 
    [JsonProperty("Roles", ItemTypeNameHandling = TypeNameHandling.All)] 
    public ICollection<IMediaDetectionRole> Roles { get; set; } 

    public MediaDetectionUser() 
    { 
     Roles = new List<IMediaDetectionRole>(); 
    } 
} 

JSON bir örnek liste.

@MartijnLaarman'a teşekkürler. Bu konu, ElasticSearch ve NEST ile hiçbir ilgisi olmamasına rağmen, bu sorunu çözmemde bana yardımcı oldu.

İlgili konular