我正在使用Nest 6.5.4。我无法对索引中的特定文档执行脚本更新。
我已经尝试了许多方法,但是却遇到语法错误。
我的查询如下。
var clientProvider = new ElasticClientProvider();
var projectModel = new ProjectModel();
var res = clientProvider.Client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Id(projectId)
.Script(script=>script.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
引发错误“更新描述符没有ID的定义”
在Kibana中尝试使用相同的查询
POST attachment_index/attachments/1/_update
{
"script": {
"source":"ctx._source.fileInfo.fileViewCount += 1"
}
}
我不知道我在哪里出错。
最佳答案
.Id()
上没有UpdateDescriptor<T, TPartial>
方法,因为id是Update API调用的必需参数,因此此约束是通过构造函数强制实现的。.Update<T>(...)
的第一个参数是 DocumentPath<T>
,可以从中派生用于更新API调用的索引,类型和ID。如果ProjectModel
CLR POCO具有带有值的Id
属性,则该属性将用作 call 的ID。例如
public class ProjectModel
{
public int Id { get; set; }
}
var client = new ElasticClient();
var projectModel = new ProjectModel { Id = 1 };
var updateResponse = client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
导致
POST http://localhost:9200/attachment_index/attachments/1/_update
{
"script": {
"source": "ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"
}
}
如果要显式指定ID,则可以传递
DocumentPath<T>
的值var updateResponse = client.Update<ProjectModel>(1, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
关于elasticsearch - 用于.NET的NEST 6.5.4中按脚本的Elastic Search Update API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55712032/