使用NEST在ElasticSearch中更新子对象

时间:2015-07-22 16:39:35

标签: elasticsearch nest

我有一个弹性搜索文档,其结构如下:

{
   "Id": 123,
   "name": "MyName",
   "skill": {
         "skillId": 321,
         "name": "Skill Name",
         "description": "Skill Description"
    } 
}

我还有一个映射到此文档的类:

public class Person {
      public int Id { get; set; }
      public string name { get; set; }
      public Skill skill { get; set; }
}

public class Skill{
      public int skillId { get; set; }
      public string name { get; set; }
      public string description { get; set; }
}

现在我想更新弹性搜索的技能说明:

var person = _client.Get(.......)
var newSkill = new Skill();
newSkill.skillId = person.skill.skillId;
newSkill.description = "This is a new Description"

var result = _client.Update<Person, Skill>(u => u
            .IdFrom(person)
            .Doc(newSkill)
            .RetryOnConflict(3)
            .Refresh()
        );

此代码不会更新此人的现有技能,而是将技能属性添加到人员的根目录。

这里怎么了?

1 个答案:

答案 0 :(得分:0)

您正在尝试使用ElasticSearch,就好像它是SQL一样。

您需要将新Skill分配给Person并更新整个Person对象,如下所示:

var person = _client.Get(.......)
var newSkill = new Skill();
newSkill.name = "New Skill";
newSkill.description = "This is a new Description"

person.skill = newSkill;

var result = _client.Update<Person, Person>(u => u
            .IdFrom(person)
            .Doc(person)
            .RetryOnConflict(3)
            .Refresh()
        );