【问题标题】:Configure Spring Data Solr with multiple cores为 Spring Data Solr 配置多核
【发布时间】:2017-07-21 16:55:30
【问题描述】:

我在将我的 solr 配置从 1.5.4 升级到 3.0.0.M4(以及 Spring Boot 更新到 2.0.0.M2)时遇到问题。

之前的配置文件(spring-data-solr 1.5.4.RELEASE):

@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr", multicoreSupport = true)
public class SolrConfiguration implements EnvironmentAware{

    private RelaxedPropertyResolver propertyResolver;
    private Environment             environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.data.solr.");
    }

    @Bean
    public SolrServer solrServer() {
        String solrHost = propertyResolver.getProperty("host");
        return new HttpSolrServer(solrHost);
    }


    @Bean(name = "core1SolrTemplate")
    public SolrOperations core1SolrTemplate() {
        HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
        return new SolrTemplate(httpSolrServer, "Core1");
    }

    @Bean(name = "core2SolrTemplate")
    public SolrOperations core2SolrTemplate() {
        HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
        return new SolrTemplate(httpSolrServer, "Core2");
    }
    ...
}

然后我们在代码中使用这些solrtemplate作为

@Resource SolrTemplate core1SolrTemplate;
@Resource SolrTemplate core2SolrTemplate;

尝试更新时,我发现 HttpSolrServer 不再可用,我们应该使用 SolrClient,但是随着 multicoreSupport 的移除和声明模板核心的能力,我不知道如何为每个核心拥有一个模板(或一个能够检测到要查询的核心?)

这是我当前的非工作配置:

@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr")
public class SolrConfiguration {

    @Inject
    private Environment             environment;

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder(environment.getProperty("spring.data.solr.host")).build();
    }

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

}

我们的 Pojo 配置为:

@SolrDocument(solrCoreName = "Core1")
public class Foo{
    @Id
    private String                     id;

    ...
}

以及引发错误的调用

core1SolrTemplate.queryForPage(simpleQuery, Foo.class)

“原因:org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException:来自http://192.168.99.100:8983/solr 的服务器错误:预期的 MIME 类型为 application/octet-stream 但得到了 text/html。”

看来 solrtemplate 只是调用了 baseurl 而没有选择任何核心。

我还尝试像以前一样创建多个 SolrTemplate 并将参数传递给 solrClient() 以构造每个核心的特定 url,但未能成功(我在第二个模板显然使用第一个 solrtemplate bean 时出现错误,指向错误的核心)。

我应该如何配置 Solr 以便能够查询多个核心?

当前 (2.1.4) solr 文档: http://docs.spring.io/spring-data/data-solr/docs/current/reference/html/#solr.multicore

3.0.0.M4 文档中没有描述多核: http://docs.spring.io/spring-data/data-solr/docs/3.0.0.M4/reference/html/#solr.multicore

【问题讨论】:

    标签: spring spring-boot spring-data-solr


    【解决方案1】:

    我认为您需要将 @SolrDocument 注释添加到您的 pojo 以使用多核支持。请参阅下面的问题进行讨论。不过,这取决于您的设置方式。

    Spring Data Solr multiple cores and repository

    【讨论】:

    【解决方案2】:

    看来我累了,以下配置似乎运行良好 - 随时改进它

    @Configuration
    @EnableSolrRepositories(basePackages={"com.example.demo.repository"})
    public class SolrContext {
    
        static final String SOLR_HOST = "solr.host";
    
        @Autowired
        private Environment environment;
    
        @Bean
        public SolrClient solrClient() {
            return new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)).build();
        }
    
        @Bean
        public SolrTemplate solrTemplate() {
            return new SolrTemplate(solrClient());
        }
    
        @Bean
        public SolrTemplate core1Template() {
            return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core1").build());
        }
    
        @Bean
        public SolrTemplate core2Template() {
            return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core2").build());
        }
    }
    

    举例: https://github.com/Farael49/spring-solr-config/blob/master/demo/src/main/java/com/example/demo/web/CoreController.java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2012-05-10
      相关资源
      最近更新 更多