Reputation: 41
I am updating a document in my index where I am changing its status from 0 to 1. When I do an update and again query for any document having status 0, it again returns the same document, so I end up doing the same thing again. This operation gets repeated twice or thrice randomly. There seems to be some lag on part of elastic search client because if I do the same operation with a settimeout, it doesn't repeat. This problem gets magnified when I update a say 5000 documents, which runs around 5500 times on an average.
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);
})
}
Upvotes: 1
Views: 2617
Reputation: 282
After updating documents you should wait for index refresh. After index refreshes, your updated documents will be available for search.
For your situation you can force index to refresh after update request https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html. Or if you using elasticsearch 5.0+ you can "block" request using wait_for parameter until index refreshes with schedule (1s default).
Upvotes: 3