【发布时间】:2020-09-22 11:34:17
【问题描述】:
我正在使用 spring-boot-starter-data-elasticsearch (2.3.0.RELEASE),它使用 spring-data-elasticsearch (4.0.0.RELEASE)。
官方documentation 显示了 ElasticsearchRestTemplate 和 ElasticsearchRepository 的用法。推荐的文档索引方法是什么?
ElasticsearchRestTemplate.index() or ElasticsearchRepository.save()
一个类似的问题是here,但是已经超过5年了。
【问题讨论】:
-
存储库在后台使用模板来访问 Elasticsearch。通常在 Spring Data 中,当您需要实现存储库不提供的功能时,您会使用存储库并使用模板
-
@P.J.Meisch 是的!我看到存储库也在内部使用相同的模板。那么如果我直接使用存储库就可以了,因为索引是一项基本操作并且存储库支持它。但是有一个问题,在域对象中我已经指定了索引名称,那么为什么在使用 ElasticsearchRestTemplate.index() 时必须再次通过 IndexCoordinates 指定索引名称?模板是否忽略 Document 注释?我看到它仍然尊重字段注释。
-
模板(或更好的操作接口)具有仅采用类的方法重载,而其他方法则采用额外的
IndexCoordinates对象。如果要指定与@Document注释中定义的索引不同的索引,则可以使用后者。 -
@P.J.Meisch,在
ElasticsearchRestTemplate类中,我只能看到一个需要IndexCoordinates的索引方法。ElasticsearchOperations中也没有类似的方法,并且大多数方法都被标记为已弃用。在文档中也找不到:docs.spring.io/spring-data/elasticsearch/docs/current/api/org/…我错过了什么吗? -
你必须查看
DocumentOperations和SearchOperations。那里定义了所有方法。ElasticsearchOperations派生自这两个接口。ElasticsearchRestTemplate是具体的实现,但它派生自AbstractElasticsearchOperations,其中所有的 logi 都是独立于具体的客户端实现的。所以注入一个ElasticsearchOperations并使用它。
标签: spring-boot elasticsearch spring-data-elasticsearch