【问题标题】:Elastic Search: Is there any way to ignore the script if can't find the document?弹性搜索:如果找不到文档,有什么方法可以忽略脚本?
【发布时间】:2021-07-19 15:14:36
【问题描述】:

在下面的代码中,我有多个脚本,其中一些文档不存在,所以目前只需更新现有的脚本并为不存在的脚本返回错误。 无论如何只是更新现有的并忽略其余部分并返回有效状态?

var script = "new InlineScript($@"if (ctx._source.containsKey('[RemovedField]'))" +
                                              $"{{ ctx._source.remove(\"{RemovedField}\")}}");"

foreach(var update in updates) 
    var operation = new BulkUpdateOperation<IFieldOperation, object>(update.Id)
                {
                    Script = script,
                    ScriptedUpsert = false,
                    //Upsert = don't want upsert if doc not exist 
                    Index = [Index Name]
                };
     operations.Add(operation);

var response = await _client.BulkAsync(new BulkRequest {
                                            Operations = new BulkOperationsCollection<IBulkOperation>(operations)
                                        })

// response.IsValid is equal false which I want to find the way to return as true

【问题讨论】:

  • 你能澄清你的问题并从你的文档和脚本中举出一些例子吗?
  • 已经更新了上面的代码,希望能帮助你理解我的问题

标签: elasticsearch


【解决方案1】:

如果文档不存在,您可以使用noop 操作

scripted_upsert 应设置为 true,这样无论文档是否存在,您的脚本都会运行

然后将此行添加到脚本中

if (ctx._source.isEmpty()) {ctx.op = 'noop'; return}
...<rest of your script follows>

如果您尝试更新不存在的文档,ctx._source 将为空,那么您可以告诉 elastic 不要做任何事情(noop)并提前返回

附言。如果noop不行,可以试试none,看我认为的es版本

【讨论】:

  • 感谢 Ron,我尝试了 noopnone 但仍然获得 404 状态,因为没有退出 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
相关资源
最近更新 更多