How to create new core in solrj

2019-08-24 10:49发布

问题:

I am trying to create new core using solrj. I need it to prepare test for my app. I think this code is incomplete or wrong because every time I got a error "no core candidates".

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");
    }
}

Error:

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

Version:

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

回答1:

I missed few files and cfg. Correct code:

 @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);
}