我正在使用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"
}
}
我不知道我在哪里出错。
答案 0 :(得分:1)
There is no .Id()
method on UpdateDescriptor<T, TPartial>
because an id is a required parameter for an Update API call, so this constraint is enforced through the constructors.
The first parameter to .Update<T>(...)
is a DocumentPath<T>
from which an index, type and id can be derived for the update API call. If the ProjectModel
CLR POCO has an Id
property with a value, this will be used for Id of the call. For example
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"))
);
which results in
POST http://localhost:9200/attachment_index/attachments/1/_update
{
"script": {
"source": "ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"
}
}
If you want to explicitly specify the Id, you can pass the value for 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"))
);
答案 1 :(得分:1)
client.UpdateAsync<ProjectModel, object>(
new DocumentPath<ProjectModel>(id),
u => u
.Index(ConfigurationManager.AppSettings.Get("indexname"))
.Type(ConfigurationManager.AppSettings.Get("indextype"))
.Doc(ProjectModel)
.DocAsUpsert()
.Refresh(Elasticsearch.Net.Refresh.True));
这将起作用,如果您仍然遇到任何问题,请告诉我。