【问题标题】:Spring data solr HttpSolrClient does not use core annotation from entitySpring data solr HttpSolrClient 不使用实体的核心注释
【发布时间】:2017-01-18 15:58:49
【问题描述】:

配置如下

@Configuration
@EnableSolrRepositories(basePackages={"com.foo"}, multicoreSupport=true)
public class SolrConfig {

    @Value("${solr.host}") String solrHost;

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient(solrHost);
    }

    @Bean
    public SolrTemplate solrTemplate() {
        return new SolrTemplate(solrClient());
    }
}

我有一个简单的实体:

@SolrDocument(solrCoreName = "core1")
public class MyEntity implements Serializable {

如果使用 SolrTemplate 执行查询,则不使用文档上的 coreName 注解:

Page results = solrTemplate.queryForPage(search, MyEntity.class);

我得到异常:

org.springframework.data.solr.UncategorizedSolrException: Error from server at http://localhost:8983/solr: Expected mime type application/octet-stream but got text/html.
[..]
Problem accessing /solr/select
[...]
<title>Error 404 Not Found</title>

将 SolrTemplate bean 更改为:

@Bean
public SolrTemplate solrTemplate() {
    return new SolrTemplate(solrClient(), "core1");
}

作品

【问题讨论】:

    标签: java spring solr spring-data spring-data-solr


    【解决方案1】:

    spring-data 的人员确认这是预期行为,模板不会从实体注释中读取核心。
    因此,在multicoreSupport=true 环境中,如果您想同时使用存储库和模板,则必须创建 2 个 bean: 对于存储库,基本模板:

        @Bean
        public SolrTemplate solrTemplate() {
            return new SolrTemplate(solrClient());
        }
    

    对于注入,您将拥有另一个:

        @Bean
        public SolrTemplate customTemplate() {
            return new SolrTemplate(solrClient(), "core1");
        }
    

    显然,如果您不需要multicoreSupport=true,则不需要!

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 2018-05-04
      相关资源
      最近更新 更多