【发布时间】:2016-03-18 16:47:11
【问题描述】:
这可能是一个愚蠢的问题,但我是弹性搜索和嵌套的新手。
我有课
Person
{
public string Id {get; set;}
public string Name {get; set;}
public IList<string> PhoneNumbers {get; set;}
}
我想做的是通过添加来更新电话号码。
现在我用 2 个查询来做,但我想知道是否有办法用 1 来管理它。
// See if the Person already exists
var result = NestClient.Search<Person>(s => s
.Index(_indexName)
.Take(1)
.Query(q => q
.Term(p => p.Name, person.Name)
&& q.Term(p => p.Id, person.Id)));
if (result.ServerError != null)
{
throw result.OriginalException;
}
if (result.Documents.FirstOrDefault() == null)
{
var response = NestClient.Index<Person>(person);
if (response.ServerError != null)
{
throw response.OriginalException;
}
}
else
{
// If it does exist update and overwrite
var savedPerson = result.Documents.First();
IList<string> oldNums = SavedPerson.PhoneNumbers;
IList<string> newNums = newPerson.PhoneNumbers;
var combinedNums = oldNums.Concat(newNums);
newPerson.PhoneNumbers = combinedNums.ToList<string>();
var response = NestClient.Update(DocumentPath<Person>
.Id(newPerson.Id),
u => u.Doc(newPerson).DocAsUpsert(true));
if (response.ServerError != null)
{
throw response.OriginalException;
}
}
基本上我希望我的 upsert 添加到现有的电话号码列表(如果存在)。
【问题讨论】:
-
您打算保留重复的电话号码吗?如果不使用
Union而不是Concat。
标签: c# elasticsearch nest