Spring?Data?Elasticsearch?5.0.x修改數(shù)據(jù)后無法立即刷新解決方法示例
背景
在項(xiàng)目中用到了Spring Data ElasticSearch,最近更新到了最新版本5.0.x,在數(shù)據(jù)插入、修改、刪除后,緊接著進(jìn)行查詢發(fā)現(xiàn)數(shù)據(jù)并未更新;
解決方法
研究后發(fā)現(xiàn)是新版本的spring data es默認(rèn)的刷新策略是null,代碼如下:
@Bean
@ConditionalOnMissingBean(value = ElasticsearchOperations.class, name = "elasticsearchTemplate")
@ConditionalOnBean(ElasticsearchClient.class)
ElasticsearchTemplate elasticsearchTemplate(ElasticsearchClient client, ElasticsearchConverter converter) {
return new ElasticsearchTemplate(client, converter);
}
這里沒有給ElasticsearchTemplate設(shè)置refreshPolicy屬性,點(diǎn)進(jìn)AbstractElasticsearchTemplate可以看到默認(rèn)為null,而以前默認(rèn)是立即刷新IMMEDIATE。
查看Spring官網(wǎng)發(fā)現(xiàn)
Refresh configuration
When configuring Spring Data Elasticsearch like described in Elasticsearch Clients by using ElasticsearchConfigurationSupport, AbstractElasticsearchConfiguration or AbstractReactiveElasticsearchConfiguration the refresh policy will be initialized to null. Previously the reactive code initialized this to IMMEDIATE, now reactive and non-reactive code show the same behaviour.
也就是說,對(duì)es數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)變更操作時(shí),默認(rèn)是按照索引自帶的刷新策略(一般情況下是1s以后才會(huì)更新索引)
{
"settings": {},
"defaults": {
"index": {
"refresh_interval": "1s"
}
}
}如果想要在Spring data es中更新數(shù)據(jù)后立即刷新,那么需要如下配置覆蓋掉Springboot的Auto Configuration:
@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfig extends ElasticsearchConfiguration {
private final ElasticsearchProperties properties;
public ElasticsearchConfig(ElasticsearchProperties properties) {
this.properties = properties;
}
@NotNull
@Override
public ClientConfiguration clientConfiguration() {
List<String> uris = properties.getUris();
String[] uri = uris.toArray(new String[0]);
return ClientConfiguration.builder()
.connectedTo(uri)
.withSocketTimeout(properties.getSocketTimeout())
.withConnectTimeout(properties.getConnectionTimeout())
.build();
}
@NotNull
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
@Override
public ElasticsearchOperations elasticsearchOperations(@NotNull ElasticsearchConverter elasticsearchConverter,
@NotNull ElasticsearchClient elasticsearchClient) {
ElasticsearchTemplate template = new ElasticsearchTemplate(elasticsearchClient, elasticsearchConverter);
template.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
return template;
}
}以上就是Spring Data Elasticsearch 5.0.x修改數(shù)據(jù)后無法立即刷新解決方法示例的詳細(xì)內(nèi)容,更多關(guān)于Spring Data Elasticsearch無法刷新的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于spring項(xiàng)目中無法加載resources下文件問題及解決方法
在學(xué)習(xí)Spring過程中,TestContext框架試圖檢測(cè)一個(gè)默認(rèn)的XML資源位置,再resources下創(chuàng)建了一個(gè)com.example的文件夾,執(zhí)行時(shí),報(bào)錯(cuò),本文給大家介紹spring項(xiàng)目中無法加載resources下文件,感興趣的朋友跟隨小編一起看看吧2023-10-10
使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)詳解
在現(xiàn)代的Web應(yīng)用中,緩存是提高系統(tǒng)性能和響應(yīng)速度的重要手段之一,Spring框架提供了強(qiáng)大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實(shí)現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)2025-01-01
springboot?集成activemq項(xiàng)目配置方法
這篇文章主要介紹了springboot?集成activemq項(xiàng)目配置方法,e-car項(xiàng)目配置通過引入activemq依賴,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-04-04
如何用java計(jì)算兩個(gè)時(shí)間相差多少小時(shí)
最近工作中遇到需要計(jì)算時(shí)間差,下面這篇文章主要給大家介紹了關(guān)于如何用java計(jì)算兩個(gè)時(shí)間相差多少小時(shí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
SpringBoot后端接收參數(shù)優(yōu)化代碼示例(統(tǒng)一處理前端參數(shù))
使用Spring Boot開發(fā)API的時(shí)候,讀取請(qǐng)求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,下面這篇文章主要給大家介紹了關(guān)于SpringBoot后端接收參數(shù)優(yōu)化(統(tǒng)一處理前端參數(shù))的相關(guān)資料,需要的朋友可以參考下2024-07-07
Java運(yùn)行時(shí)數(shù)據(jù)區(qū)概述詳解
這篇文章主要介紹了Java運(yùn)行時(shí)數(shù)據(jù)區(qū)概述,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
吊打Java面試官!整理了一周的Spring面試大全(附答案)
這篇文章主要介紹了Spring面試資料(附答案)建議收藏留存,學(xué)Java的小伙伴都知道Spring是面試的必問環(huán)節(jié),看完了一天就可掌握數(shù)據(jù)結(jié)構(gòu)和算法的面試題,快來看看吧2021-08-08

