elasticsearch - 用于.NET的NEST 6.5.4中按脚本的Elastic Search Update API

标签 elasticsearch nest elastic-stack

我正在使用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/

相关文章:

elasticsearch - 使用C#NEST增加弹性体的磁场极限

elasticsearch - 使用Elasticsearch NEST Api的按位运算

elasticsearch - 如何在Logstash中处理JSON

elasticsearch - "Fan-out"索引策略

node.js - Node JS Elasticsearch 结果改进

elasticsearch - 在NEST Elasticsearch 查询中,file.filename返回null

elasticsearch - Timelion多次 split

elasticsearch - Elasticsearch自动完成提示

Elasticsearch 获取所有没有 child 的 parent

elasticsearch - Elasticsearch :如何 'OR'两个不同的嵌套查询?