更新后的弹性搜索查询返回旧的非更新文档

时间:2016-11-03 10:50:34

标签: javascript node.js elasticsearch

我正在更新索引中的文档,我将其状态从0更改为1.当我进行更新并再次查询状态为0的任何文档时,它再次返回相同的文档,所以我最终做了同样的事情。该操作重复两次或三次。弹性搜索客户端的部分似乎有些滞后,因为如果我使用settimeout执行相同的操作,则不会重复。当我更新5000个文件时,这个问题会被放大,平均运行大约5500次。

function getData(){
    client.search({
        index: 'es_dummy',
        type: 'log',
        size: 1,
        body: {
            "query" : {
                "match" : {
                    "status" : "0"
                }
            }
        }
    },
    function(err, resp){
        console.log(resp.hits.hits.length);
        if(resp.hits.hits.length)
            updateIndexData(resp.hits.hits, 0, resp.hits.hits.length);
        else
            console.log("done");
    });
}
getData();
function updateIndexData(resp, index, length){
    client.update({
        index: 'es_dummy',
        type: 'log',
        id: resp[index]._id,
        script: 'ctx._source.status = 1'
    },
    function(err1, resp1){
        if(!err1 && resp1){
            console.log("updated" + " " + update);
            var milliseconds = (new Date).getTime();
            console.log("time " + milliseconds);
            update++;
            getData();
        }
        else
            console.log(err1);
    })

}

1 个答案:

答案 0 :(得分:0)

更新文档后,您应该等待索引刷新。索引刷新后,您的更新文档将可供搜索。

根据您的情况,您可以在更新请求https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html后强制索引刷新。或者,如果您使用elasticsearch 5.0+,则可以使用 wait_for 参数“阻止”请求,直到索引刷新计划(默认为1秒)。

相关问题