簡體   English   中英

Elasticsearch NEST 通過腳本更新

[英]Elasticsearch NEST update by script

我正在嘗試通過腳本更新某些字段。 在郵遞員中,我發送以下請求:

https://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

使用您的索引設置和查詢更新它

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

如果有人在這里尋找 NEST 6x 版本的解決方案,請參見下文

public async Task UpdateId(MetaData request)
{
   try
   {                
      var scriptParams = new Dictionary<string, object>
      {
         { "Id",request.Id}
      };
      var script = $"ctx._source.Id= params.Id;";                   
      var indexResponse = 
         await EsClient.UpdateByQueryAsync<Doc>(
            qd => qd.Index(request.IndexName)                          
                    .Conflicts(Conflicts.Proceed)
                    .Query(
                       rq => rq.Term("_id", request._id))
                               .Script(
                                  s => s.Source(script) 
                                        .Params(scriptParams)
                               )
                    )
         );
      if (!indexResponse.IsValid)
      {
                        
      }
   }
   catch (Exception ex)
   {

   }
}

[ElasticsearchType(Name = "_doc")]
public class Doc
{
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM