0

我已经使用 ElasticSearch 对数据进行了索引,但在更新特定字段时遇到了问题。JSON 的一个片段如下:

 {
 "_index": "indexName",
 "_type": "type",
 "_id": "00001",
 "colors": [
     "red",
     "green"
 ]
 "place": "london",
 "person": [
      {
           "name": "john",
           "age": "27",
           "eyes": "blue"
      }
      {
           "name": "mary",
           "age": "19",
           "eyes": "green"
      }

 ]
 }

我需要添加一个新person对象,例如:

{
    "name": "jane",
    "age": "30",
    "eyes": "grey"
}

我的People定义如下:

public class People
{
    public List<string> colors {get; set; }
    public string place {get; set; }
    public List<Person> person {get; set; }
}
public class Person
{
    public string name {get; set; }
    public string age {get; set; }
    public string eyes {get; set; }
}

color通过以下方式更新没有问题:

client.Update<People>(u => u
    .Id(u.Id)
    .Index(u.Index)
    .Type(u.Type)
    .Script("if ctx._source.containsKey(\"color\")) { ctx._source.color += color; } else { ctx._source.color = [color] }")
    .Params(p => p
        .Add("color", "pink"))
);

我不知道如何更新该person字段,因为它是Person对象列表而不是字符串列表。

任何帮助是极大的赞赏!

4

1 回答 1

2

我之前通过使用匿名对象并将部分文档更新发送到 Elasticsearch 以仅更新所需部分来完成此操作。

这是一个应该可以工作的代码片段......

var peopleId = //Get Id of document to be updated.
var persons = new List<Person>(3);
persons.Add(new Person { name = "john", eyes = "blue", age = "27" });
persons.Add(new Person { name = "mary", eyes = "green", age = "19" });
persons.Add(new Person { name = "jane", eyes = "grey", age = "30" });

var response = Client.Update<People, object>(u => u
            .Id(peopleId)
            .Doc(new { person = persons})
            .Refresh()
        );
于 2015-09-09T13:57:49.650 回答