如何在solrj中创建新核心

时间:2017-09-21 07:40:12

标签: java spring-boot solr solrj

我正在尝试使用solrj创建新核心。我需要它为我的应用程序准备测试。我认为这段代码不完整或错误,因为每次我收到错误“没有核心候选人”。

package com.itsystems.talentapp.config;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.CoreAdminResponse;
import org.apache.solr.core.CoreContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.io.IOException;

@Configuration
public class SolrConfig {

    @Autowired
    SolrClient solrClient;


    @Bean
    @Profile("test")

    public EmbeddedSolrServer embeddedSolrServer() throws IOException, SolrServerException {
        String folder = "src/main/resources/solr/";
        String coreName = "candidates";

        CoreAdminResponse e = new CoreAdminRequest().createCore(coreName, folder, solrClient);
        CoreContainer container = new CoreContainer(folder);
        container.load();
        return new EmbeddedSolrServer(container, "candidates");
    }
}

错误:

org.apache.solr.common.SolrException: No such core: candidates

版本:

<dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-core</artifactId>
        <version>6.6.1</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

我错过了几个文件和cfg。 正确的代码:

 @Bean
public SolrClient solrClient() throws IOException, SolrServerException {
    String folder = "src/main/resources/solr/";
    String coreName = "candidates";
    CoreContainer container = new CoreContainer(folder);
    container.load();
    return new EmbeddedSolrServer(container, coreName);
}