如何在 Elasticsearch Nest 2.x UpdateRequest 中使用参数

How to use params in Elasticsearch Nest 2.x UpdateRequest

我想在 UpdateRequest 中为脚本使用参数但没有成功。 这是源代码:

        IUpdateRequest<People, People> updateRequest = new UpdateRequest<People, People>( indexName, "people", docId);
        updateRequest.RequestParameters.Script("ctx._source.age+=step");
        updateRequest.Params = new Dictionary<string, object>();
        updateRequest.Params.Add("step", 1);

        IUpdateResponse<People> updateResponse =  client.Update<People>(updateRequest);

这失败了,如果我不使用参数它会像这样成功

        IUpdateRequest<People, People> updateRequest = new UpdateRequest<People, People>( indexName, "people", docId);
        updateRequest.RequestParameters.Script("ctx._source.age+=1");
        IUpdateResponse<People> updateResponse =  client.Update<People>(updateRequest);

看起来参数不是放置脚本参数的正确位置,将参数放入脚本的正确方法是什么?

您的要求不太正确。您应该将 updateRequest 上的 Script 属性 设置为内联脚本

var indexName = "people";
var docId = 1;

var updateRequest = new UpdateRequest<People, People>(indexName, "people", docId)
{
    Script = "ctx._source.age+=step",
    Params = new Dictionary<string, object>
    {
        { "step", 1 }
    }
};

var updateResponse = client.Update<People>(updateRequest);

请求看起来像

POST http://localhost:9200/people/people/1/_update?pretty=true 
{
  "script": "ctx._source.age+=step",
  "params": {
    "step": 1
  }
}