我有一个脚本,使用一些update_by_query调用Elasticsearch。

在这里,我使用id=299966更新项目,并更改垃圾桶标记trash=0:

_update_by_query
{
  "query": {
    "query": {
      "bool": {
        "must": [
          {
            "terms": {
              "_id": [
                299966
              ]
            }
          }
        ],
        "should": [

        ]
      }
    }
  },
  "script": {
    "inline": "ctx._source.trash=0"
  }
}

然后我将带有id=299966的项目(与上述相同)更改为trash=1:
_update_by_query
{
  "query": {
    "query": {
      "bool": {
        "must": [
          {
            "terms": {
              "_id": [
                299966
              ]
            }
          }
        ],
        "should": [

        ]
      }
    }
  },
  "script": {
    "inline": "ctx._source.trash=1"
  }
}

事情是做完这两个操作之后,如果我使用id=299966搜索该项目,则当它被认为是trash=0时,它会得到trash=1,因为它是最后执行的代码。我总是按顺序进行操作,而我自己的日志显示,首先执行trash=0的命令,然后执行trash=1的命令。
update_by_query逻辑中是否有任何东西可以避免进行两次调用?我是否需要等待几秒钟或其他时间才能进行第二次update_by_query?

PS:神经在代码上输入那些双重query。一切正常。

提前致谢。

最佳答案

我发现的解决方案是在每个_flush或每个_update之后使用_update_by_query

myindex/_update_by_query
{
  "query": {
    "query": {
      "bool": {
        "must": [
          {
            "terms": {
              "_id": [
                299966
              ]
            }
          }
        ],
        "should": [

        ]
      }
    }
  },
  "script": {
    "inline": "ctx._source.trash=0"
  }
}

myindex/_flush

myindex/_update_by_query
{
  "query": {
    "query": {
      "bool": {
        "must": [
          {
            "terms": {
              "_id": [
                299966
              ]
            }
          }
        ],
        "should": [

        ]
      }
    }
  },
  "script": {
    "inline": "ctx._source.trash=1"
  }
}

关于elasticsearch - “update by query”无法直接调用按预期方式工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43343228/

10-12 22:17