從 3.2.x 升級到 4.0.x
本節描述了從版本 3.2.x 到 4.0.x 的重大變更,以及如何透過新引入的功能替換已移除的功能。
移除使用的 Jackson Mapper
版本 4.0.x 中的一項更改是 Spring Data Elasticsearch 不再使用 Jackson Mapper 將實體對映到 Elasticsearch 所需的 JSON 表示(請參閱 Elasticsearch 物件對映)。在 3.2.x 版本中,Jackson Mapper 是預設使用的。可以透過顯式配置(元模型物件對映)切換到基於元模型的轉換器(命名為 ElasticsearchEntityMapper)。
在 4.0.x 版本中,基於元模型的轉換器是唯一可用的,並且不需要顯式配置。如果您有自定義配置,透過提供一個 bean(如下所示)來啟用元模型轉換器
@Bean
@Override
public EntityMapper entityMapper() {
ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
elasticsearchMappingContext(), new DefaultConversionService()
);
entityMapper.setConversions(elasticsearchCustomConversions());
return entityMapper;
}
您現在必須移除此 bean,因為 ElasticsearchEntityMapper 介面已被移除。
一些使用者在實體類上自定義了 Jackson 註解,例如為了定義 Elasticsearch 中對映文件的自定義名稱或配置日期轉換。這些不再被考慮。所需的功能現在由 Spring Data Elasticsearch 的 @Field 註解提供。請參閱 對映註解概述 以獲取詳細資訊。
從查詢物件中移除隱式索引名稱
在 3.2.x 中,不同的查詢類,如 IndexQuery 或 SearchQuery,具有用於操作的索引名稱或索引名稱的屬性。如果這些未設定,則會檢查傳入的實體以檢索 @Document 註解中設定的索引名稱。
在 4.0.x 中,索引名稱現在必須在型別為 IndexCoordinates 的附加引數中提供。透過這種分離,現在可以將一個查詢物件用於不同的索引。
因此,例如以下程式碼
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(person.getId().toString())
.withObject(person)
.build();
String documentId = elasticsearchOperations.index(indexQuery);
必須更改為
IndexCoordinates indexCoordinates = elasticsearchOperations.getIndexCoordinatesFor(person.getClass());
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(person.getId().toString())
.withObject(person)
.build();
String documentId = elasticsearchOperations.index(indexQuery, indexCoordinates);
為了更方便地使用實體並使用實體 @Document 註解中包含的索引名稱,已添加了新方法,例如 DocumentOperations.save(T entity);
新的 Operations 介面
在 3.2 版本中,存在 ElasticsearchOperations 介面,它定義了 ElasticsearchTemplate 類的所有方法。在 4 版本中,這些功能已拆分為不同的介面,使這些介面與 Elasticsearch API 對齊。
-
DocumentOperations是與文件相關的功能,例如儲存或刪除。 -
SearchOperations包含在 Elasticsearch 中搜索的功能。 -
IndexOperations定義了對索引進行操作的功能,例如索引建立或對映建立。
ElasticsearchOperations 現在擴充套件了 DocumentOperations 和 SearchOperations,並具有訪問 IndexOperations 例項的方法。
版本 3.2 中 ElasticsearchOperations 介面中的所有已移至 IndexOperations 介面的功能仍然可用,它們被標記為已棄用,並具有委託給新實現的預設實現。 |
/**
* Create an index for given indexName.
*
* @param indexName the name of the index
* @return {@literal true} if the index was created
* @deprecated since 4.0, use {@link IndexOperations#create()}
*/
@Deprecated
default boolean createIndex(String indexName) {
return indexOps(IndexCoordinates.of(indexName)).create();
}
棄用
方法和類
許多函式和類已被棄用。這些函式仍然有效,但 Javadoc 會顯示它們應該替換為什麼。
/*
* Retrieves an object from an index.
*
* @param query the query defining the id of the object to get
* @param clazz the type of the object to be returned
* @return the found object
* @deprecated since 4.0, use {@link #get(String, Class, IndexCoordinates)}
*/
@Deprecated
@Nullable
<T> T queryForObject(GetQuery query, Class<T> clazz);
移除
-
如前所述,
ElasticsearchEntityMapper介面已被移除。 -
SearchQuery介面已合併到其基介面Query中,因此其出現可以直接替換為Query。 -
方法
org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor<T> resultsExtractor);和org.springframework.data.elasticsearch.core.ResultsExtractor介面已被移除。這些可用於在 Jackson 基於對映器的響應對映不足的情況下解析 Elasticsearch 的結果。自版本 4.0 以來,有新的 搜尋結果型別 可返回 Elasticsearch 響應中的資訊,因此不再需要暴露這種低階功能。 -
低階方法
startScroll、continueScroll和clearScroll已從ElasticsearchOperations介面中移除。對於低階滾動 API 訪問,現在ElasticsearchRestTemplate類上提供了searchScrollStart、searchScrollContinue和searchScrollClear方法。