2017-08-29 159 views
0

嗨有一個基本的一對多結構..設置父爲子對象ElasticSearch使用ElasticSearch NEST .NET庫

public class Person { 
     public int PersonId { get; set; } 
     public string Name { get; set; } 
     public List<Skill> Skills { get; set; } 
} 

public class Skill{ 
     public int PersonId { get; set; } 
     public int SkillId { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
} 

安裝ElasticSearch NEST 5.x的使用.NET Framework 4.5 ..

探索從最後2天的網絡,但無法找到設置人士爲技能父母的方式..

我假設NEST將自動映射的父子關係,所以我嘗試以下

private ElasticLowLevelClient client = new ElasticLowLevelClient();  

public void CreatePerson(Person person) 
     { 
var parentResponse = client.Index(person, i => i.Index("myindex").Type("person").Id(person.PersonId)); 
    foreach (var skill in person.Skills) 
    { 
     var skillResponse = client.Index(skill, i => i.Index("myindex").Type("personskills").Parent(person.PersonId.ToString()).Id(skill.SkillId)); //here I am getting error 
    } 
} 

文檔創建者沒有任何問題,但在personskill DOC我得到這個錯誤的時間:

如果沒有父領域已配置

在探索我不能指定父發現可能的文章說,我需要設置父類型的映射中的孩子..但如何..什麼是程序自定義映射索引以及如何和在哪裏我應該做的..沒有得到任何提示..請指導

回答

0

問題是我沒有得到勒到親子文件正確映射。我們可以通過預定義的映射創建索引映射他們或者我們可以更新映射

創建索引

private ElasticLowLevelClient client = new ElasticLowLevelClient();  
private CreateIndexDescriptor descriptor = new CreateIndexDescriptor("myindex") 
       .Mappings(ms => ms 
       .Map<Person>("person", m => m.AutoMap()) 
       .Map<Skill>("personskills", m => m.AutoMap().Parent("person")) 
     );   

public void CreatePerson(Person person) 
{ 
    client.CreateIndex(descriptor); //I am creating it here but one can create it in the class where we will create ElasticClient 
    var parentResponse = client.Index(person, i => i.Index("myindex").Type("person").Id(person.PersonId)); 
    foreach (var skill in person.Skills) 
    { 
     var skillResponse = client.Index(skill, i => i.Index("myindex").Type("personskills").Parent(person.PersonId.ToString()).Id(skill.SkillId)); //here I am getting error 
    } 
} 

更新映射

public void CreatePerson(Person person) 
     { 
client.Map<Skill>(m => m 
           .Parent("person").Index("myindex")); //this will put the default mapping of default index 
var parentResponse = client.Index(person, i => i.Index("myindex").Type("person").Id(person.PersonId)); 
    foreach (var skill in person.Skills) 
    { 
     var skillResponse = client.Index(skill, i => i.Index("myindex").Type("skill").Parent(person.PersonId.ToString()).Id(skill.SkillId)); //here I am getting error 
    } 
} 

在這裏,我已經將子文檔類型更改爲默認值..但可以將其設置爲映射。希望這可以幫助其他人。