簡體   English   中英

更新后的彈性搜索查詢返回舊的非更新文檔

[英]Elastic search query after update returning old non updated document

我正在更新我的索引中的文檔,我將其狀態從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);
    })

}

更新文檔后,您應該等待索引刷新。 索引刷新后,您的更新文檔將可供搜索。

根據您的情況,您可以在更新請求https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html之后強制索引刷新。 或者,如果您使用elasticsearch 5.0+,則可以使用wait_for參數“阻止”請求,直到索引刷新並使用schedule(默認為1s)。

暫無
暫無

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

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