從 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
介面中移除。對於低階滾動(scroll)API 訪問,現在在ElasticsearchRestTemplate
類上有searchScrollStart
、searchScrollContinue
和searchScrollClear
方法。