Elasticsearch NEST按脚本更新

时间:2018-07-05 22:33:08

标签: c# elasticsearch nest elasticsearch-5

我尝试通过脚本更新某些字段。在邮递员中,我发送请求以发送类似以下内容的网址:

http://search.test.com/items_example/item/en_01_2/_update

具有此主体:

{
  "script": {
    "inline": "ctx._source.Title = params.Title; ctx._source.Desc = params.Desc",
    "params": {
        "Title": "New Title",
        "Desc": "New Desc"
    }  
  }
}

但是我不知道如何使用NEST发送此请求。谁能帮我? Elasticsearch 5.4.1版本,NEST 5.6.1

2 个答案:

答案 0 :(得分:3)

使用您的索引设置和查询进行更新

var elasticClient = new ElasticClient(settings);
var scriptParams = new Dictionary<string, object>
{
    {"Title", "New Title"},
    {"Desc", "New Desc"}
};

var response = elasticClient
    .UpdateByQuery<dynamic>(q => q.Query(rq => rq.Term(....))
    .Script(script =>
        script.Inline(
            $"ctx._source.Title = params.Title;" +
            $"ctx._source.Desc  = params.Desc ;"
        )
    .Params(scriptParams));

编辑:如果您只是在寻找更新,只需将语法更改为

var response = elasticClient.Update<dynamic>(
    "items_example/item/en_01_2" //This is your document path
    , request => request.Script(
        script =>
            script.Inline(
                    $"ctx._source.Title = params.Title;" +
                    $"ctx._source.Desc  = params.Desc ;"
                )
                .Params(scriptParams)));

答案 1 :(得分:1)

如果有人在寻找NEST 6x版本的解决方案,请参阅下文

display: inline-flex