Spring Data Elasticsearch的ElasticsearchTemplate与ElasticsearchRepository [英] Spring Data Elasticsearch's ElasticsearchTemplate vs ElasticsearchRepository

查看:222
本文介绍了Spring Data Elasticsearch的ElasticsearchTemplate与ElasticsearchRepository的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 Spring Data Elasticsearch的

  • org.springframework.data.elasticsearch.repository.ElasticsearchRepository
  • org.springframework.data.elasticsearch.core.ElasticsearchTemplate

似乎它们是两个实现相同目标的不同API,但我不确定这两种类型之间有什么区别,更重要的是何时使用它们.

It seems they are two different APIs that achieve the same goal but I am not sure what the differences are between those two types and more importantly when to use which.

有人可以提供建议和指导吗?

Can someone please provide advice and guidance?

推荐答案

ElasticsearchRepository旨在用作您键入的域类的存储库.它扩展了存储库的Spring接口,因此可以用作其中之一.如果您习惯使用Spring存储库,将会对此感到非常满意.

ElasticsearchRepository is intended to be used as a repository for your domain classes, as it's typed. It extends Spring interfaces for repositories so it can used as one of them. You'll feel very comfortable with it if you are used to Spring repositories.

开始将对象索引到Elasticsearch的所有操作就是将@Document批注添加到它们,并创建一个扩展ElasticsearchRepository的Repository接口.

All you need to start indexing your objects to Elasticsearch is to add the @Document annotation to them and create a Repository interface extending ElasticsearchRepository.

可索引类:

@Document(
    indexName = "customers", 
    type = "customer", 
    shards = 1, 
    replicas = 0, 
    refreshInterval = "-1"
)
public class Customer {
    @Id
    private Long id;
    private String name;

    public Customer() { 
    }

    public Customer(String name) {
        this.name = name;
    }

    //Getters and setters omited
}

恢复原状:

public interface CustomerRepository 
    extends ElasticsearchRepository<Customer, Long>{
}

借助此工具,您可以立即进行CRUD操作,索引,搜索和其他常见操作.

With this you can, out of the box, make CRUD operations, index, search and other common operations.

ElasticsearchTemplate是用于处理索引的Elasticsearch客户端,它没有键入或与您的域类相关.由于您可以执行存储库实现不可用的许多任务,例如删除索引或进行汇总搜索,因此它功能更强大.

ElasticsearchTemplate, by other hand, is an elasticsearch client for working with your indexes, and it's not typed or related to your domain classes. It's more powerful since you can do many tasks not available to the repository implementation, like deleting an index or making aggregated searchs.

这篇关于Spring Data Elasticsearch的ElasticsearchTemplate与ElasticsearchRepository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆