你能在一个应用程序中创建多个 ElasticsearchCrudRepositories 吗?

Can you create multiple ElasticsearchCrudRepositories in one app?

我有两个不同的 ElasticsearchCrudRepository 接口,例如:

public interface SomeRepository extends ElasticsearchCrudRepository<SomeDao, String> {}

public interface AnotherRepository extends ElasticsearchCrudRepository<AnotherDao, String> {}

在我的配置中,我有这样的东西:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.something")
@EnableElasticsearchRepositories(basePackages = "com.something.repos")
public class MyConfig {

    @Bean
    public Client client() {
        InetSocketTransportAddress address = new InetSocketTransportAddress(
            new InetSocketAddress('1.2.3.4',9300)
        );
        return TransportClient.builder().build().addTransportAddress(address);
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
       return new ElasticsearchTemplate(client());
    }
}

我想要做的是让 SomeRepository 接口连接到服务器 1.2.3.4,而 AnotherRepository 接口连接到不同的服务器,比如 5.6.7.8。这可能吗?

我知道我可以创建第二个 elasticsearchTemplate() 方法(当然有一个唯一的名称)并创建一个 5.6.7.8 的客户端,但是我必须将 ElasticsearchTemplate 注入某些服务 class 并使用它而不是使用 AnotherRepository。如果必须的话,我可以这样做,但我更喜欢使用存储库。

更新:

仍然找不到任何关于此的信息。不幸的是缺少 Spring Data ES 的文档。我似乎只能为最基本和最琐碎的案例找到答案,这不一定适用于现实世界。

任何人都可以指出一个如何使用 SimpleElasticsearchRepository 的工作示例吗?

这似乎不是一种方法。必须创建两个不同的 类,它们都扩展了 ElasticsearchTemplate,然后在 Java 配置中传入适当的客户端。