如何实现使用反应按钮 onClick 更改布尔字段值的 Elasticsearch update_by_query

How to implement Elasticsearch update_by_query that change the value of Boolean field using react button onClick

我正在尝试使用反应按钮 onClick 来实现 Elasticsearch update_by_query。 当我单击反应按钮时,它应该将 Elasticsearch 文档布尔字段 isCancelRun 设置为 true(索引名称为:运行)

我想将下一个查询添加为反应函数,它将通过按钮 onClick 执行:

curl -X POST "http://elasticsearch_ip_addr:9200/run-*/_update_by_query?pretty" -H "Content-Type: application/json" -d'
{
  "script": {
    "source": "ctx._source.isCancelRun = true;",
    "lang": "painless"
  },
  "query": {
    "term": {
      "runId": "20220419142721270"
    }
  }
}
'

我实现了单击按钮时应调用的下一个函数:

export function cancelRunExecution(runIdToCancel) {
    fetch(`${ElasticSearch_BaseAddress}${"run-*"}/_update_by_query?pretty`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: 
            `{"query":{"bool":{"must":[{"match":{"runId":"${runIdToCancel}"}}]}}},
            script: {
                source: "ctx._source.isCancelRun = true;",
                lang: "painless"
            }`
        })
    .then(response => {
        if (!response.ok) { 
            return Promise.reject(response.statusText);
        }

        return response.json();
    });
}

ElasticSearch_BaseAddress = elasticsearch_ip_addr:9200/

带 onClick 的按钮实现应该执行 cancelRunExecution 函数:

            <div>
                <button id="cancelRun" onClick={() => {
                    cancelRunExecution(props.run.runId);
                    var btn = document.getElementById("cancelRun");
                    btn.innerHTML = 'Run execution - canceled';                    
                }}>
                    Cancel Run Execution
                </button>
            </div>

问题: 它不会将布尔字段 isCancelRun 更改为 true。

您发送的不是 JSON,因为 scriptsourcelang 不是 double-quoted。

改为发送此负载:

    body: 
        `{"query":{"bool":{"must":[{"match":{"runId":"${runIdToCancel}"}}]}}},
        "script": {
            "source": "ctx._source.isCancelRun = true;",
            "lang": "painless"
        }`
    })