Example usage for org.apache.solr.core CoreContainer load

List of usage examples for org.apache.solr.core CoreContainer load

Introduction

In this page you can find the example usage for org.apache.solr.core CoreContainer load.

Prototype

public void load() 

Source Link

Document

Load the cores defined for this CoreContainer

Usage

From source file:com.indoqa.solr.server.factory.EmbeddedSolrServerBuilder.java

License:Apache License

public static SolrServer build(String url, String embeddedSolrConfigurationDir) throws IOException {
    String solrHome = getNormalizedPath(embeddedSolrConfigurationDir);

    InputStream solrXmlInputStream = EmbeddedSolrServerBuilder.class
            .getResourceAsStream("/embedded-core-container/solr.xml");

    SolrResourceLoader loader = new SolrResourceLoader(solrHome);
    ConfigSolr configSolr = ConfigSolr.fromInputStream(loader, solrXmlInputStream);

    solrXmlInputStream.close();//from w  ww .  j  a  v a 2 s.  co  m

    CoreContainer container = new CoreContainer(loader, configSolr);
    container.load();

    String dataDir = getNormalizedPath(getDataDir(url));
    CoreDescriptor coreDescriptor = new CoreDescriptor(container, "Embedded Core", solrHome, CORE_DATADIR,
            dataDir);
    SolrCore core = container.create(coreDescriptor);

    return new EmbeddedSolrServer(container, core.getName());
}

From source file:com.indoqa.solr.spring.client.EmbeddedSolrServerBuilder.java

License:Apache License

public static SolrClient build(String url, String embeddedSolrConfigurationPath) {
    if (new File(embeddedSolrConfigurationPath).exists()) {
        deleteOldCoreProperties(embeddedSolrConfigurationPath);

        SolrResourceLoader loader = new SolrResourceLoader(getNormalizedPath(embeddedSolrConfigurationPath));
        NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
        CoreContainer container = new CoreContainer(nodeConfig);
        container.load();

        Map<String, String> properties = new HashMap<String, String>();
        properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());

        SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
        return new EmbeddedSolrServer(core);
    }/*from w w w.  j  a  v  a2s .  c o m*/

    // use a temporary directory for the resource loader because Solr needs a real directory for this
    SolrResourceLoader loader = new SolrResourceLoader(createTempDirectory());
    NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
    CoreContainer container = new CoreContainer(nodeConfig);
    container.load();

    Map<String, String> properties = new HashMap<String, String>();
    properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());
    properties.put(CoreDescriptor.CORE_CONFIG, embeddedSolrConfigurationPath + "/conf/solrconfig.xml");
    properties.put(CoreDescriptor.CORE_SCHEMA, embeddedSolrConfigurationPath + "/conf/schema.xml");

    SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
    return new EmbeddedSolrServer(core);

}

From source file:com.lucid.solr.sidecar.SidecarIndexReaderFactoryTest.java

License:Apache License

private CoreContainer init() throws Exception {

    if (solrHomeDirectory.exists()) {
        FileUtils.deleteDirectory(solrHomeDirectory);
    }/*from ww  w  . j a  va2s  .co  m*/
    assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs());

    copyMinConf(new File(solrHomeDirectory, "source"));
    File target = new File(solrHomeDirectory, "target");
    copyMinConf(target);
    // modify the target to add SidecarIndexReaderFactory
    File targetSolrConf = new File(target, "conf" + File.separator + "solrconfig.xml");
    String xml = FileUtils.readFileToString(targetSolrConf, "UTF-8");
    xml = xml.replaceFirst("<!-- EDIT_SIDECAR_START", "");
    xml = xml.replaceFirst("EDIT_SIDECAR_END -->", "");
    FileUtils.writeStringToFile(targetSolrConf, xml, "UTF-8");
    File solrXml = new File(solrHomeDirectory, "solr.xml");
    FileUtils.write(solrXml, SOLR_XML, IOUtils.CHARSET_UTF_8.toString());
    final CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath());
    cores.load();

    //cores.setPersistent(false);
    return cores;
}

From source file:com.mycompany.sparkrentals.client.RentalSolrClientTest.java

@Before
public void setUp() throws IOException, SolrServerException {
    File resourceDir = new File("src/test/resources/solr");
    //use a temporary folder for solr home
    FileUtils.copyDirectory(resourceDir, tempSolrHome.getRoot());
    CoreContainer coreContainer = new CoreContainer(tempSolrHome.getRoot().getAbsolutePath());
    coreContainer.load();
    server = new EmbeddedSolrServer(coreContainer, "new_core");

    //populate initial data
    List<Rental> rentals = new ArrayList<>();
    rentals.add(new Rental("A1234", "city1", "province2", "country2", "fjkso", "Villa", true, true, true, true,
            10f, "$", 1, new Date()));
    rentals.add(new Rental("A2234", "city2", "province2", "country9", "dfsdd", "studio", true, false, true,
            true, 11f, "$", 2, new Date()));
    rentals.add(new Rental("A3234", "city3", "province5", "country1", "e9r3e", "studio", false, true, false,
            true, 7.2f, "$", 3, new Date()));
    Date threeDaysAgo = new Date(new Date().getTime() - 3 * 24 * 3600 * 1000);
    rentals.add(new Rental("A4234", "city4", "province7", "country6", "ghree", "Single Floor", true, false,
            true, true, 8f, "$", 2, threeDaysAgo));

    for (Rental rental : rentals) {
        server.addBean(rental);// w  w w.  jav a  2  s.c om
        server.commit();
    }

    client = new RentalSolrClient();
    client.setSolrClient(server);
}

From source file:com.ngdata.hbaseindexer.mr.TestUtils.java

License:Apache License

private static EmbeddedSolrServer createEmbeddedSolrServer(File solrHomeDir, FileSystem fs, Path outputShardDir)
        throws IOException {

    LOG.info("Creating embedded Solr server with solrHomeDir: " + solrHomeDir + ", fs: " + fs
            + ", outputShardDir: " + outputShardDir);

    // copy solrHomeDir to ensure it isn't modified across multiple unit tests or multiple EmbeddedSolrServer instances
    File tmpDir = Files.createTempDir();
    tmpDir.deleteOnExit();/* w w w.  j a  va 2 s  .  c o  m*/
    FileUtils.copyDirectory(solrHomeDir, tmpDir);
    solrHomeDir = tmpDir;

    Path solrDataDir = new Path(outputShardDir, "data");

    String dataDirStr = solrDataDir.toUri().toString();

    SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrHomeDir.toString()), null, null);

    LOG.info(String.format(Locale.ENGLISH,
            "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s",
            solrHomeDir, solrHomeDir.toURI(), loader.getInstancePath(), loader.getConfigDir(), dataDirStr,
            outputShardDir));

    // TODO: This is fragile and should be well documented
    System.setProperty("solr.directoryFactory", HdfsDirectoryFactory.class.getName());
    System.setProperty("solr.lock.type", DirectoryFactory.LOCK_TYPE_HDFS);
    System.setProperty("solr.hdfs.nrtcachingdirectory", "false");
    System.setProperty("solr.hdfs.blockcache.enabled", "false");
    System.setProperty("solr.autoCommit.maxTime", "600000");
    System.setProperty("solr.autoSoftCommit.maxTime", "-1");

    CoreContainer container = new CoreContainer(loader);
    container.load();

    SolrCore core = container.create("core1", Paths.get(solrHomeDir.toString()),
            ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr), false);

    if (!(core.getDirectoryFactory() instanceof HdfsDirectoryFactory)) {
        throw new UnsupportedOperationException(
                "Invalid configuration. Currently, the only DirectoryFactory supported is "
                        + HdfsDirectoryFactory.class.getSimpleName());
    }

    EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "core1");
    return solr;
}

From source file:ddf.catalog.source.solr.SolrServerFactory.java

License:Open Source License

/**
 * Provides an already instantiated {@link SolrServer} object. If an instance has not already
 * been instantiated, then the single instance will be instantiated with the provided
 * configuration file. If an instance already exists, it cannot be overwritten with a new
 * configuration./* www.  j  a v  a2s.  com*/
 * 
 * @param solrConfigXml
 *            the name of the solr configuration filename such as solrconfig.xml
 * @param schemaXml
 *            filename of the schema such as schema.xml
 * @param givenConfigFileProxy
 *            a ConfigurationFileProxy instance. If instance is <code>null</code>, a new
 *            {@link ConfigurationFileProxy} is used instead.
 * @return {@link SolrServer} instance
 */
public static EmbeddedSolrServer getEmbeddedSolrServer(String solrConfigXml, String schemaXml,
        ConfigurationFileProxy givenConfigFileProxy) {

    LOGGER.info("Retrieving embedded solr with the following properties: [" + solrConfigXml + "," + schemaXml
            + "," + givenConfigFileProxy + "]");

    String solrConfigFileName = DEFAULT_SOLRCONFIG_XML;

    String schemaFileName = DEFAULT_SCHEMA_XML;

    if (isNotBlank(solrConfigXml)) {
        solrConfigFileName = solrConfigXml;
    }

    if (isNotBlank(schemaXml)) {
        schemaFileName = schemaXml;
    }

    File solrConfigFile = null;
    File solrConfigHome = null;
    File solrSchemaFile = null;

    ConfigurationFileProxy configProxy = givenConfigFileProxy;

    if (givenConfigFileProxy == null) {
        configProxy = new ConfigurationFileProxy(null, ConfigurationStore.getInstance());
    }

    File configurationDir = new File(ConfigurationFileProxy.DEFAULT_SOLR_CONFIG_PARENT_DIR,
            ConfigurationFileProxy.SOLR_CONFIG_LOCATION_IN_BUNDLE);
    configProxy.writeBundleFilesTo(configurationDir);

    try {
        URL url = configProxy.getResource(solrConfigFileName);

        LOGGER.info("Solr config url: " + url);

        solrConfigFile = new File(new URI(url.toString()).getPath());

        solrConfigHome = new File(solrConfigFile.getParent());
    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    try {
        URL url = configProxy.getResource(schemaFileName);

        LOGGER.info("Solr schema url: " + url);

        solrSchemaFile = new File(new URI(url.toString()).getPath());

    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    SolrConfig solrConfig = null;
    IndexSchema indexSchema = null;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(SolrServerFactory.class.getClassLoader());

        // NamedSPILoader uses the thread context classloader to lookup
        // codecs, posting formats, and analyzers
        solrConfig = new SolrConfig(solrConfigHome.getParent(), solrConfigFileName,
                new InputSource(FileUtils.openInputStream(solrConfigFile)));
        indexSchema = new IndexSchema(solrConfig, schemaFileName,
                new InputSource(FileUtils.openInputStream(solrSchemaFile)));
    } catch (ParserConfigurationException e) {
        LOGGER.warn(e);
    } catch (IOException e) {
        LOGGER.warn(e);
    } catch (SAXException e) {
        LOGGER.warn(e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    CoreContainer container = new CoreContainer(solrConfigHome.getAbsolutePath());
    container.load();
    CoreDescriptor dcore = new CoreDescriptor(container, "core1",
            solrConfig.getResourceLoader().getInstanceDir());

    File dataDir = configProxy.getDataDirectory();
    LOGGER.info("Using data directory [" + dataDir + "]");
    SolrCore core = new SolrCore("core1", dataDir.getAbsolutePath(), solrConfig, indexSchema, dcore);
    container.register("core1", core, false);

    return new EmbeddedSolrServer(container, "core1");
}

From source file:it.seralf.solrbook.client.java.EmbeddedSolrExample.java

License:Apache License

public static void main(String[] args) throws Exception {

    // NOTE: we can override this configuration, passing a valid slr home from command line
    System.setProperty("solr.solr.home", "solr-home");

    CoreContainer container = new CoreContainer();
    container.load();
    EmbeddedSolrServer server = new EmbeddedSolrServer(container, "arts");

    // delete all documents
    server.deleteByQuery("*:*");

    Artist doc = new Artist("http://en.wikipedia.org/wiki/Leonardo_da_Vinci", "Leonardo Da Vinci", "Vinci",
            "Florence");
    server.addBean(doc);//www . j a v  a2s.  co  m

    server.commit();

    QueryResponse rsp = server.query(new SolrQuery("leonardo"), METHOD.GET);

    SolrDocumentList oo = rsp.getResults();
    for (SolrDocument d : oo) {
        for (String field : d.getFieldNames()) {
            System.out.printf("%s = %s\n", field, d.getFieldValue(field));
        }
    }

    server.shutdown();
}

From source file:net.hasor.search.server.installs.SolrInstall.java

License:Apache License

/**
 * ? {@link CoreContainer}/*w ww . j  a  v a  2 s. c  om*/
 * @return a CoreContainer to hold this server's cores
 */
protected CoreContainer createCoreContainer(String searchHome) {
    SolrResourceLoader loader = new SolrResourceLoader(searchHome);
    //
    if (new File(searchHome, ConfigSolr.SOLR_XML_FILE).exists() == false) {
        throw new SolrException(ErrorCode.SERVER_ERROR,
                "Bad solr.solrxml.location set: " + searchHome + " - should be 'SEARCH-HOME'");
    }
    //
    ConfigSolr config = ConfigSolr.fromSolrHome(loader, searchHome);
    CoreContainer cores = new CoreContainer(config);
    cores.load();
    return cores;
}

From source file:org.apache.coheigea.bigdata.solr.SolrTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    CoreContainer container = new CoreContainer("target/test-classes/solr");
    container.load();

    server = new EmbeddedSolrServer(container, "core1");
}

From source file:org.apache.jackrabbit.oak.plugins.index.solr.server.EmbeddedSolrServerProvider.java

License:Apache License

private SolrServer createSolrServer() throws Exception {

    log.info("creating new embedded solr server with config: {}", solrServerConfiguration);

    String solrHomePath = solrServerConfiguration.getSolrHomePath();
    String coreName = solrServerConfiguration.getCoreName();
    EmbeddedSolrServerConfiguration.HttpConfiguration httpConfiguration = solrServerConfiguration
            .getHttpConfiguration();// w w w.  j a  va  2  s .  c  om

    if (solrHomePath != null && coreName != null) {
        checkSolrConfiguration(solrHomePath, coreName);
        if (httpConfiguration != null) {
            if (log.isInfoEnabled()) {
                log.info("starting embedded Solr server with http bindings");
            }
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(JettySolrRunner.class.getClassLoader());

            Integer httpPort = httpConfiguration.getHttpPort();
            String context = httpConfiguration.getContext();
            JettySolrRunner jettySolrRunner = null;
            try {
                jettySolrRunner = new JettySolrRunner(solrHomePath, context, httpPort, "solrconfig.xml",
                        "schema.xml", true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner instantiated");
                }
                jettySolrRunner.start(true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner started");
                }
            } catch (Exception t) {
                if (log.isErrorEnabled()) {
                    log.error("an error has occurred while starting Solr Jetty", t);
                }
            } finally {
                if (jettySolrRunner != null && !jettySolrRunner.isRunning()) {
                    try {
                        jettySolrRunner.stop();
                        if (log.isInfoEnabled()) {
                            log.info("Jetty runner stopped");
                        }
                    } catch (Exception e) {
                        if (log.isErrorEnabled()) {
                            log.error("error while stopping the Jetty runner", e);
                        }
                    }
                }
                Thread.currentThread().setContextClassLoader(classLoader);
            }
            if (log.isInfoEnabled()) {
                log.info("starting HTTP Solr server");
            }
            return new HttpWithJettySolrServer(
                    SolrServerConfigurationDefaults.LOCAL_BASE_URL + ':' + httpPort + context + '/' + coreName,
                    jettySolrRunner);
        } else {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());

            CoreContainer coreContainer = new CoreContainer(solrHomePath);
            try {
                if (!coreContainer.isLoaded(coreName)) {
                    coreContainer.load();
                }
            } catch (Exception e) {
                log.error("cannot load core {}, shutting down embedded Solr..", coreName, e);
                try {
                    coreContainer.shutdown();
                } catch (Exception se) {
                    log.error("could not shutdown embedded Solr", se);
                }
                return null;
            } finally {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, coreName);
            if (server.ping().getStatus() == 0) {
                return server;
            } else {
                throw new IOException("the embedded Solr server is not alive");
            }
        }
    } else {
        throw new Exception("SolrServer configuration proprties not set");
    }
}