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

时间:2016-05-10 00:40:50

标签: nest elasticsearch-net

我想在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);

这失败了,如果我不使用params就会像这样成功

        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);

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

1 个答案:

答案 0 :(得分:1)

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

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
  }
}