ElasticSearch.Net -更新数组与多个组件
本文关键字:组件 数组 Net 更新 ElasticSearch | 更新日期: 2023-09-27 18:11:48
我已经使用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
对象的列表,而不是字符串列表。
任何帮助都非常感谢!
我以前通过使用匿名对象并向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()
);