如何为多核配置带有存储库的 Spring Data Solr [英] How to configure Spring Data Solr with Repositories for multiple cores

查看:20
本文介绍了如何为多核配置带有存储库的 Spring Data Solr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用多核支持的存储库使用 solr 设置 spring 数据的详细而完整的说明?

Is there a detailed and complete explanation for setting up spring data with solr using repositories with multicore support?

推荐答案

spring-data-solr 唯一需要用到的依赖是

The only dependency that need to be used for spring-data-solr is

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

它下载 solrj 依赖项,这不能被更高版本的 solrj 覆盖.此外,最好使用 HttpSolrServer 而不是 EmbeddedSolrServer,这是我们将要使用的.

It downloads solrj dependency and this must not be overridden with later versions of solrj. Also It is always preferable to use HttpSolrServer over EmbeddedSolrServer which is what we will be working with.

Configuration 类应如下所示:

The Configuration class should look like this:

@Configuration
@EnableSolrRepositories(value = "com.package.",multicoreSupport = true)
public class SolrConfig
{
    @Bean
    public SolrServer solrServer() throws Exception
    {
        HttpSolrServerFactoryBean f = new HttpSolrServerFactoryBean();
        f.setUrl("http://localhost:8983/solr");
        f.afterPropertiesSet();
        return f.getSolrServer();
    }

    @Bean
    public SolrTemplate solrTemplate(SolrServer solrServer) throws Exception
    {
        return new SolrTemplate(solrServer());
    }
}

文档实体应该包含关于它们属于哪个核心的信息

The document entity should contain information about which core they belong to

@SolrDocument(solrCoreName = "core1")
public class Document1
{
    @Id
    @Field
    private String id;

    /**other attributes**/
}

另一个文件应该是

@SolrDocument(solrCoreName = "core2")
public class Document2
{
    @Id
    @Field
    private String id;

    /**other attributes**/
}

现在最好的部分是你已经完成了.简单地以普通的旧方式设置存储库就可以了

Now the best part is you are already done. Simply setting up repository in the plain old way does the trick

public interface SolrCore1Repository extends SolrCrudRepository<Document1,String>
{
    // optional code
}

另一个仓库就像

public interface SolrCore2Repository extends SolrCrudRepository<Document2,String>
{
    // optional code
}

一旦 solr 在指定的 url 上运行并且它具有根据 pojo 的字段,您就完成了.

Once solr is running on the url specified and it has fields according to the pojo, you are done.

这篇关于如何为多核配置带有存储库的 Spring Data Solr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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