使用 spring 保存数据后立即查询 elasticsearch 可能不是最新的?

query elasticsearch right after saving data with spring can be not up to date?

我有以下代码: (线程一次只能访问一个特定对象的 synchronized(someObj)。)

@Async
public void funcA() {
 synchronized (someObj) {
      //query (using spring) for **some_data_in_ES**
      //check if **some_data_in_ES** is not 'nothing'
      //update **some_data_in_ES** to be 'A' using spring 
 }
}

@Async
public void funcB() {
 synchronized (someObj) {
      //query (using spring) for **some_data_in_ES**
      //check if **some_data_in_ES** is not 'nothing'
      //update **some_data_in_ES** to be 'B' using spring
 }
}

如果 funcA 控制了 synchronized(someObj) 并将 some_data_in_ES 更新为 'A' 并且 synchronized(someObj) 上的锁定是免费的, funcB 进入 synchronized(someObj) 并检查 some_data_in_ES 是否不是 'nothing'。 有没有可能 B 会得到 'nothing' 或 elasticsearch 在更新为 'A' 后只会 return some_data_in_ES?

is there a chance that B will get 'nothing' or elasticsearch will only return some_data_in_ES after it updated to be 'A'?

是的,B 可以看到 'nothing' 或 'A'。对 ES 的更新不是原子的。您看到更新的速度取决于 ES 和 Lucene 的配置方式。所以这完全是时间和配置的问题。我过去处理问题的一种方法是使用我在 ES 中索引的数据库中的 ID。这样我就可以确定它是独一无二的,并且用它来编写更新和更新是微不足道的。

检查 update API,例如更新插入可能是合适的。

继续阅读 near real-time search,了解有关文档何时可见的更多信息。特别注意刷新间隔和刷新 API.