当前位置: 首页 > 知识库问答 >
问题:

单个API,多个Elasticsearch实例

姬博瀚
2023-03-14

我们有一个Spring Boot Restful API,它需要从两个不同的Elasticsearch实例(在不同的服务器上)获取数据,一个用于“共享”数据(上面有大约5个不同的索引),一个用于“私有”数据(有大约3个不同的索引)。目前只针对“私有”数据实例运行,一切都很好。但我们现在需要获取“共享”数据。

在我们的Spring Boot应用程序中,我们启用了如下Elasticsearch存储库

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = {
    "com.company.core.repositories",  //<- private repos here...
    "com.company.api.repositories"  //<-- shared repos here...
})
public class Application { //... }

然后我们使用Elasticsearch chRepository访问“私有”数据,例如:

package com.company.core.repositories

public interface DocRepository extends ElasticsearchRepository<Doc, Integer> { ... }

在我们的终点,我们有。。。

@RestController
@CrossOrigin
@RequestMapping("/v2/statuses/")
public class StatusEndpoint {
    @Resource
    private ElasticsearchTemplate template;

    @Autowired
    private DocRepository docRepository;

    @Autowired
    private Validator validator;
    //...
}

现在我们要添加另一个存储库,如:

package com.company.api.repositories

public interface LookupRepository extends ElasticsearchRepository<Lookup, Integer> { ... }

然后在我们的API层中,我们将添加一个自动连接的实例...

@Autowired
private LookupRepository lookupRepo;

我们在想我们可以定义多个具有不同名称的Bean,但是我们如何将每个“elasticsearch chTemboard”bean与需要它们的不同Elasticsearch chRepository实例相关联?此外,我们如何将“私有”bean/配置与注入的实例相关联

@Resource
private ElasticsearchTemplate template;

我们需要在哪里本地使用它?

共有2个答案

端木渝
2023-03-14

可能有多种方法可以做到这一点。这是一种利用@Bean名称和@Resources名称的方法。

@Configuration
public class MyElasticConfig{

    @Bean //this is your private template
    public ElasticsearchTemplate template(){
        //construct your template
        return template;
    }

    @Bean //this is your public template
    public ElasticsearchTemplate publicTemplate(){
        //construct your template
        return template;
    }
}

然后你可以这样得到他们...

@Resource
private ElasticsearchTemplate template;

@Resource
private ElasticsearchTemplate publicTemplate;

@Resource(name="template")
private ElasticsearchTemplate anyName;

@Resource(name="publicTemplate")
private ElasticsearchTemplate anyOtherName;

您还可以直接命名@Bean,而不是依赖@Bean的方法名。

    @Bean (name="template")
    public ElasticsearchTemplate myPrivateTemplate(){
        //construct your template
        return template;
    }

    @Bean (name="publicTemplate")
    public ElasticsearchTemplate myPubTemplate(){
        //construct your template
        return template;
    }

看看这些关于这个话题的精彩资源。

SPRING注入@RESOURCE、@AUTOWIRED和@INJECT
Bean注释类型
AUTOWIRED vs RESOURCE

锺离浩慨
2023-03-14

您可以使用2个唯一的Elasticsearch配置bean和StatusEndpoint控制器中模板注入的@Resources(name="XXX")注释来解决这个问题。

如果根据应使用的Elasticsearch集群将存储库分隔为不同的包,则可以使用@EnableElasticsearchRepositories注释将它们与不同的配置相关联。

例如:

如果您有这些包和类:

com.company.data.repositories.private.YourPrivateRepository
com.company.data.repositories.shared.YourSharedRepository

然后是这些配置:

@Configuration
@EnableElasticsearchRepositories( 
    basePackages = {"com.company.data.repositories.private"},
    elasticsearchTemplateRef = "privateElasticsearchTemplate")
public class PrivateElasticsearchConfiguration {

    @Bean(name="privateElasticsearchTemplate")
    public ElasticsearchTemplate privateTemplate() {
       //code to create connection to private ES cluster
    }
}
@Configuration
@EnableElasticsearchRepositories( 
    basePackages = {"com.company.data.repositories.shared"},
    elasticsearchTemplateRef = "sharedElasticsearchTemplate")
public class SharedElasticsearchConfiguration {

    @Bean(name="sharedElasticsearchTemplate")
    public ElasticsearchTemplate sharedTemplate() {
       //code to create connection to shared ES cluster
    }
}

由于@EnableElasticsearch chRepositories注释中的elasticsearch chTemplateRef参数,实现存储库的JPA代码将使用base Packages列表中存储库的指定模板。

对于StatusEndpoint部分,只需为@Resource注释提供正确的模板bean名称即可。您的StatusEndpoint如下所示:

@RestController
@CrossOrigin
@RequestMapping("/v2/statuses/")
public class StatusEndpoint {

    @Resource(name="privateElasticsearchTemplate")
    private ElasticsearchTemplate template;

    @Autowired
    private DocRepository docRepository;

    @Autowired
    private Validator validator;
    //...
}
 类似资料:
  • 问题内容: 我想从索引中获取所有数据。由于项数太大而无法存储,因此我使用了Scroll(很好的功能): 调用时效果很好: 但是,当我多次调用前一个方法时,会得到相同的多次,因此无法并行滚动多次。 我找到了http://elasticsearch-users.115913.n3.nabble.com/Multiple-scrolls- simultanious-td4024191.html ,其中指

  • 问题内容: 我曾经使用过django,haystack和elasticsearch。 我的search_index.py: 搜索表格: 模板: 看 ` 我得到所有的价值,那里有“老板”和“挡泥板” 当您在搜索框中输入“ boss fender”时,我没有结果。从搜索表单中,我只能得到一个单词的结果,例如“老板”。如何使搜索多个单词的能力? 问题答案: 这个月我陷入了这个问题。 为了执行正确的查询,

  • 我的要求是将数据发送到不同的ES接收器(基于数据)。例如:如果数据包含特定信息,则将其发送到sink1,否则将其发送到sink2等(基本上是根据数据动态发送到任何一个接收器)。我还想分别为ES sink1、ES sink2、ES sink3等设置并行度。 有什么简单的方法可以在flink中实现上述目标吗? 我的解决方案:(但并不满意) 我可以想出一个解决方案,但有中间Kafka主题,我写(topi

  • 问题内容: 我在同一台Ubuntu服务器上有一个Rails 3应用程序的暂存和生产实例(使用tyre gem)。看来这两个实例都共享相同的elasticsearch索引,这显然不是我想要的。 如何使生产和登台实例使用单独的实例? 问题答案: 您需要覆盖索引名称。假设您要绑定ActiveRecord,它将根据相关模型创建索引名称。您可以使用这样的前缀来调整名称; 然后会创建一个名为的索引,以此类推。

  • 问题内容: 假设我有5个电影片名: Sans Soleil Sansa So Is This Sol Goode Sole Survivor 我想使用此预期行为实现自动完成搜索字段: “Sans” > Sans Soleil, Sansa “Sans so” > Sans Soleil “So” > So Is This, Sol Goode, Sole Survivor “So Is” > So

  • 尝试获取与字段ABC的值相匹配的文档。尝试了“必须”或“应该”查询,但未得到预期结果。有人能建议我应该尝试什么样的查询吗?使用HighLevelRestClient。 或 映射 条件工作正常。如果我只是反转条件并忽略字段值,那么我就会得到结果。 X1和Y1是精确的字段值(想想枚举) Still query返回所有文档。这应该已将文档筛选为匹配的值 样本文档