在 elasticsearch 更新後立即能夠查詢

2 mins.

一般的關聯式資料庫下 update 以後,如果沒有下 commit 大部分情況是拿到舊的資料,但是在 elastic search 的環境下,沒有所謂的 commit,在異動資料後馬上執行 search 卻拿不到更新後的結果。

只要你在下指令的時候,加上 refresh 這個條件並且指定為 true,就可以馬上讓 search 看到異動後的資料。

官方不太建議使用,如果真的要用要小心,官方文件

這邊附上 elasticsearchjs 的範例,將符合的 id 都改為 deleted 的狀態。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
elastic.updateByQuery({
index: TABLE_NAME,
refresh: true,
body: {
script: {
lang: 'painless',
source: 'ctx._source["deleted"] = true',
},
query: {
terms: {
id: ids,
},
},
},
});