简体   繁体   English

ElasticSearch.Net-更新具有多个组件的数组

[英]ElasticSearch.Net - Update array with multiple components

I've got data indexed using ElasticSearch, and I'm having trouble updating a particular field. 我已经使用ElasticSearch索引了数据,但在更新特定字段时遇到了麻烦。 A snippet of the JSON is as follows: 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"
      }

 ]
 }

I need to add in a new person object, something like: 我需要添加一个新的person对象,例如:

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

I've got People defined as follows: 我已经将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; }
}

I updated color with no problems by doing: 通过执行以下操作,我更新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"))
);

I can't figure out how to update the person field though, as it's a list of Person objects rather than a list of strings. 我无法弄清楚如何更新person字段,因为它是Person对象列表而不是字符串列表。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

I have done this previously by using an anonymous object and sending a partial document update to Elasticsearch to only update the needed section. 我以前通过使用匿名对象并将部分文档更新发送给Elasticsearch来仅更新所需的部分来完成此操作。

Here is a code snippet that should work... 这是一个应该起作用的代码片段...

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()
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM