從 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 版本中,不同的查詢類別(如 IndexQuerySearchQuery)具有屬性,這些屬性會採用它們所操作的索引名稱或索引名稱。如果未設定這些屬性,則會檢查傳入的實體以檢索在 @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 現在擴展了 DocumentOperationsSearchOperations,並具有取得 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 顯示了應該用什麼來取代它們。

來自 ElasticsearchOperations 的範例
/*
 * 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);

Elasticsearch 棄用

自版本 7 以來,Elasticsearch TransportClient 已被棄用,它將在 Elasticsearch 版本 8 中移除。Spring Data Elasticsearch 棄用了在 4.0 版本中使用 TransportClientElasticsearchTemplate 類別。

映射類型已從 Elasticsearch 7 中移除,它們仍然以棄用值的形式存在於 Spring Data @Document 註解和 IndexCoordinates 類別中,但它們在內部不再使用。

移除

  • 如已描述,ElasticsearchEntityMapper 介面已被移除。

  • SearchQuery 介面已合併到其基本介面 Query 中,因此它的出現可以簡單地用 Query 取代。

  • 方法 org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor<T> resultsExtractor);org.springframework.data.elasticsearch.core.ResultsExtractor 介面已被移除。這些可以用於解析來自 Elasticsearch 的結果,適用於使用基於 Jackson 的映射器完成的回應映射不足的情況。自 4.0 版本以來,有新的搜尋結果類型來從 Elasticsearch 回應中回傳資訊,因此不需要公開此低階功能。

  • 低階方法 startScrollcontinueScrollclearScroll 已從 ElasticsearchOperations 介面中移除。對於低階滾動 API 存取,現在在 ElasticsearchRestTemplate 類別上有 searchScrollStartsearchScrollContinuesearchScrollClear 方法。