从 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 接口已被移除。这些可用于在 Jackson 基于映射器的响应映射不足的情况下解析 Elasticsearch 的结果。自版本 4.0 以来,有新的 搜索结果类型 可返回 Elasticsearch 响应中的信息,因此不再需要暴露这种低级功能。

  • 低级方法 startScrollcontinueScrollclearScroll 已从 ElasticsearchOperations 接口中移除。对于低级滚动 API 访问,现在 ElasticsearchRestTemplate 类上提供了 searchScrollStartsearchScrollContinuesearchScrollClear 方法。

© . This site is unofficial and not affiliated with VMware.