Example usage for org.apache.solr.client.solrj.request CoreAdminRequest.Create setCoreName

List of usage examples for org.apache.solr.client.solrj.request CoreAdminRequest.Create setCoreName

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request CoreAdminRequest.Create setCoreName.

Prototype

public void setCoreName(String coreName) 

Source Link

Usage

From source file:com.datasalt.utils.viewbuilder.SolrAdminCoreUtils.java

License:Apache License

public static NamedList<Object> createNewCore(SolrServer adminServer, String coreName, String instanceDir)
        throws SolrServerException, IOException {
    CoreAdminRequest.Create req = new CoreAdminRequest.Create();
    req.setCoreName(coreName);
    req.setInstanceDir(instanceDir);/*from   w ww  .ja  va2s.co  m*/

    return adminServer.request(req);
}

From source file:com.datasalt.utils.viewbuilder.SolrAdminCoreUtils.java

License:Apache License

public static NamedList<Object> createNewCore(SolrServer adminServer, String coreName, String instanceDir,
        String dataDir, String schemaName, String configName) throws SolrServerException, IOException {
    CoreAdminRequest.Create req = new CoreAdminRequest.Create();
    req.setConfigName(configName);/*w w w. j  ava 2s.c  o m*/
    req.setSchemaName(schemaName);
    req.setDataDir(dataDir);
    req.setCoreName(coreName);
    req.setInstanceDir(instanceDir);
    return adminServer.request(req);
}

From source file:com.hurence.logisland.service.solr.api.SolrClientService.java

License:Apache License

protected void createCore(String name) throws IOException, SolrServerException {
    CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(name);
    createRequest.setConfigSet("basic_configs");

    createRequest.process(getClient());/*w w  w. ja  v  a2s. c  o m*/
}

From source file:com.stratio.decision.service.SolrOperationsService.java

License:Apache License

public void createCore(StratioStreamingMessage message)
        throws IOException, URISyntaxException, SolrServerException, ParserConfigurationException, SAXException,
        TransformerException, InterruptedException {
    String core = message.getStreamName();
    String dataPath = this.dataDir + '/' + core + "/data";
    String confPath = this.dataDir + '/' + core + "/conf";
    createDirs(dataPath, confPath);//w  w w  .  ja  v  a  2  s . c o m
    createSolrConfig(confPath);
    createSolrSchema(message.getColumns(), confPath);
    SolrClient solrClient = getSolrclient(core);
    CoreAdminRequest.Create createCore = new CoreAdminRequest.Create();
    createCore.setDataDir(dataPath);
    createCore.setInstanceDir(dataDir + '/' + core);
    createCore.setCoreName(core);
    createCore.setSchemaName("schema.xml");
    createCore.setConfigName("solrconfig.xml");
    if (solrClient instanceof CloudSolrClient) {
        ((CloudSolrClient) solrClient).uploadConfig(Paths.get(confPath), core);
    }
    solrClient.request(createCore);
}

From source file:com.tripod.solr.util.EmbeddedSolrServerFactory.java

License:Apache License

/**
 * @param solrHome the Solr home directory to use
 * @param configSetHome the directory containing config sets
 * @param coreName the name of the core, must have a matching directory in configHome
 * @param cleanSolrHome if true the directory for solrHome will be deleted and re-created if it already exists
 * @return an EmbeddedSolrServer with a core created for the given coreName
 *///  w w  w .j ava 2 s  . c o  m
public static SolrClient create(final String solrHome, final String configSetHome, final String coreName,
        final boolean cleanSolrHome) throws IOException, SolrServerException {

    final File solrHomeDir = new File(solrHome);
    if (solrHomeDir.exists()) {
        FileUtils.deleteDirectory(solrHomeDir);
        solrHomeDir.mkdirs();
    } else {
        solrHomeDir.mkdirs();
    }

    final SolrResourceLoader loader = new SolrResourceLoader(solrHomeDir.toPath());
    final Path configSetPath = Paths.get(configSetHome).toAbsolutePath();

    final NodeConfig config = new NodeConfig.NodeConfigBuilder("embeddedSolrServerNode", loader)
            .setConfigSetBaseDirectory(configSetPath.toString()).build();

    final EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(config, coreName);

    final CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(coreName);
    createRequest.setConfigSet(coreName);
    embeddedSolrServer.request(createRequest);

    return embeddedSolrServer;
}

From source file:org.craftercms.search.service.impl.SolrAdminService.java

License:Open Source License

@Override
public void createIndex(String id) throws SearchException {
    CoreAdminRequest.Create request = new CoreAdminRequest.Create();
    request.setCoreName(id);

    if (StringUtils.isNotEmpty(defaultInstanceDir)) {
        request.setInstanceDir(defaultInstanceDir);
    }/*  w  ww  .  ja va  2 s  .co m*/
    if (StringUtils.isNotEmpty(defaultConfigName)) {
        request.setConfigName(defaultConfigName);
    }
    if (StringUtils.isNotEmpty(defaultSchemaName)) {
        request.setSchemaName(defaultSchemaName);
    }
    if (StringUtils.isNotEmpty(defaultDataDir)) {
        request.setDataDir(defaultDataDir);
    }
    if (StringUtils.isNotEmpty(defaultConfigSet)) {
        request.setConfigSet(defaultConfigSet);
    }

    logger.info("Creating Solr core = " + id + ", instanceDir = " + defaultInstanceDir + ", configName = "
            + defaultConfigName + ", schemaName = " + defaultSchemaName + ", dataDir = " + defaultDataDir
            + ", configSet = " + defaultConfigSet);

    try {
        request.process(solrClient);
    } catch (SolrServerException | IOException e) {
        throw new SearchException(id, "Failed to create core", e);
    }
}

From source file:org.dspace.statistics.SolrLogger.java

License:BSD License

private static HttpSolrServer createCore(HttpSolrServer solr, String coreName)
        throws IOException, SolrServerException {
    String solrDir = ConfigurationManager.getProperty("dspace.dir") + File.separator + "solr" + File.separator;
    String baseSolrUrl = solr.getBaseURL().replace("statistics", "");
    CoreAdminRequest.Create create = new CoreAdminRequest.Create();
    create.setCoreName(coreName);
    create.setInstanceDir("statistics");
    create.setDataDir(solrDir + coreName + File.separator + "data");
    HttpSolrServer solrServer = new HttpSolrServer(baseSolrUrl);
    create.process(solrServer);/*from w  ww .j  a  v a2 s.  c o m*/
    log.info("Created core with name: " + coreName);
    return new HttpSolrServer(baseSolrUrl + "/" + coreName);
}

From source file:org.dspace.statistics.SolrLoggerServiceImpl.java

License:BSD License

protected HttpSolrServer createCore(HttpSolrServer solr, String coreName)
        throws IOException, SolrServerException {
    String solrDir = configurationService.getProperty("dspace.dir") + File.separator + "solr" + File.separator;
    String baseSolrUrl = solr.getBaseURL().replace("statistics", "");
    CoreAdminRequest.Create create = new CoreAdminRequest.Create();
    create.setCoreName(coreName);
    create.setInstanceDir("statistics");
    create.setDataDir(solrDir + coreName + File.separator + "data");
    HttpSolrServer solrServer = new HttpSolrServer(baseSolrUrl);
    create.process(solrServer);/*  ww  w.j  a v a  2  s  .  c  o  m*/
    log.info("Created core with name: " + coreName);
    return new HttpSolrServer(baseSolrUrl + "/" + coreName);
}

From source file:org.dspace.util.SolrImportExport.java

License:BSD License

/**
 * Reindexes the specified core/*from   w w  w  .j  av  a2  s .  c o  m*/
 *
 * @param indexName the name of the core to reindex
 * @param exportDirName the name of the directory to use for export. If this directory doesn't exist, it will be created.
 * @param keepExport whether to keep the contents of the exportDir after the reindex. If keepExport is false and the
 *                      export directory was created by this method, the export directory will be deleted at the end of the reimport.
 */
private static void reindex(String indexName, String exportDirName, boolean keepExport)
        throws IOException, SolrServerException, SolrImportExportException {
    String tempIndexName = indexName + "-temp";

    String origSolrUrl = makeSolrUrl(indexName);
    String baseSolrUrl = StringUtils.substringBeforeLast(origSolrUrl, "/"); // need to get non-core solr URL
    String tempSolrUrl = baseSolrUrl + "/" + tempIndexName;

    String solrInstanceDir = ConfigurationManager.getProperty("dspace.dir") + File.separator + "solr"
            + File.separator + indexName;
    // the [dspace]/solr/[indexName]/conf directory needs to be available on the local machine for this to work
    // -- we need access to the schema.xml and solrconfig.xml file, plus files referenced from there
    // if this directory can't be found, output an error message and skip this index
    File solrInstance = new File(solrInstanceDir);
    if (!solrInstance.exists() || !solrInstance.canRead() || !solrInstance.isDirectory()) {
        throw new SolrImportExportException("Directory " + solrInstanceDir
                + "/conf/ doesn't exist or isn't readable."
                + " The reindexing process requires the Solr configuration directory for this index to be present on the local machine"
                + " even if Solr is running on a different host. Not reindexing index " + indexName);
    }

    String timeField = makeTimeField(indexName);

    // Ensure the export directory exists and is writable
    File exportDir = new File(exportDirName);
    boolean createdExportDir = exportDir.mkdirs();
    if (!createdExportDir && !exportDir.exists()) {
        throw new SolrImportExportException("Could not create export directory " + exportDirName);
    }
    if (!exportDir.canWrite()) {
        throw new SolrImportExportException("Can't write to export directory " + exportDirName);
    }

    try {
        HttpSolrServer adminSolr = new HttpSolrServer(baseSolrUrl);

        // try to find out size of core and compare with free space in export directory
        CoreAdminResponse status = CoreAdminRequest.getStatus(indexName, adminSolr);
        Object coreSizeObj = status.getCoreStatus(indexName).get("sizeInBytes");
        long coreSize = coreSizeObj != null ? Long.valueOf(coreSizeObj.toString()) : -1;
        long usableExportSpace = exportDir.getUsableSpace();
        if (coreSize >= 0 && usableExportSpace < coreSize) {
            System.err.println("Not enough space in export directory " + exportDirName
                    + "; need at least as much space as the index ("
                    + FileUtils.byteCountToDisplaySize(coreSize)
                    + ") but usable space in export directory is only "
                    + FileUtils.byteCountToDisplaySize(usableExportSpace)
                    + ". Not continuing with reindex, please use the " + DIRECTORY_OPTION
                    + " option to specify an alternative export directy with sufficient space.");
            return;
        }

        // Create a temp directory to store temporary core data
        File tempDataDir = new File(ConfigurationManager.getProperty("dspace.dir") + File.separator + "temp"
                + File.separator + "solr-data");
        boolean createdTempDataDir = tempDataDir.mkdirs();
        if (!createdTempDataDir && !tempDataDir.exists()) {
            throw new SolrImportExportException(
                    "Could not create temporary data directory " + tempDataDir.getCanonicalPath());
        }
        if (!tempDataDir.canWrite()) {
            throw new SolrImportExportException(
                    "Can't write to temporary data directory " + tempDataDir.getCanonicalPath());
        }

        try {
            // create a temporary core to hold documents coming in during the reindex
            CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
            createRequest.setInstanceDir(solrInstanceDir);
            createRequest.setDataDir(tempDataDir.getCanonicalPath());
            createRequest.setCoreName(tempIndexName);

            createRequest.process(adminSolr).getStatus();
        } catch (SolrServerException e) {
            // try to continue -- it may just be that the core already existed from a previous, failed attempt
            System.err.println("Caught exception when trying to create temporary core: " + e.getMessage()
                    + "; trying to recover.");
            e.printStackTrace(System.err);
        }

        // swap actual core with temporary one
        CoreAdminRequest swapRequest = new CoreAdminRequest();
        swapRequest.setCoreName(indexName);
        swapRequest.setOtherCoreName(tempIndexName);
        swapRequest.setAction(CoreAdminParams.CoreAdminAction.SWAP);
        swapRequest.process(adminSolr);

        try {
            // export from the actual core (from temp core name, actual data dir)
            exportIndex(indexName, exportDir, tempSolrUrl, timeField);

            // clear actual core (temp core name, clearing actual data dir) & import
            importIndex(indexName, exportDir, tempSolrUrl, true, true);
        } catch (Exception e) {
            // we ran into some problems with the export/import -- keep going to try and restore the solr cores
            System.err.println("Encountered problem during reindex: " + e.getMessage()
                    + ", will attempt to restore Solr cores");
            e.printStackTrace(System.err);
        }

        // commit changes
        HttpSolrServer origSolr = new HttpSolrServer(origSolrUrl);
        origSolr.commit();

        // swap back (statistics now going to actual core name in actual data dir)
        swapRequest = new CoreAdminRequest();
        swapRequest.setCoreName(tempIndexName);
        swapRequest.setOtherCoreName(indexName);
        swapRequest.setAction(CoreAdminParams.CoreAdminAction.SWAP);
        swapRequest.process(adminSolr);

        // export all docs from now-temp core into export directory -- this won't cause name collisions with the actual export
        // because the core name for the temporary export has -temp in it while the actual core doesn't
        exportIndex(tempIndexName, exportDir, tempSolrUrl, timeField);
        // ...and import them into the now-again-actual core *without* clearing
        importIndex(tempIndexName, exportDir, origSolrUrl, false, true);

        // commit changes
        origSolr.commit();

        // unload now-temp core (temp core name)
        CoreAdminRequest.unloadCore(tempIndexName, false, false, adminSolr);

        // clean up temporary data dir if this method created it
        if (createdTempDataDir && tempDataDir.exists()) {
            FileUtils.deleteDirectory(tempDataDir);
        }
    } finally {
        // clean up export dir if appropriate
        if (!keepExport && createdExportDir && exportDir.exists()) {
            FileUtils.deleteDirectory(exportDir);
        }
    }
}

From source file:org.opencb.commons.datastore.solr.SolrManager.java

License:Apache License

/**
 * Create a Solr core from a configuration set directory. By default, the configuration set directory is located
 * inside the folder server/solr/configsets.
 *
 * @param coreName  Core name//from  w  w w  . j av a 2s  .c o  m
 * @param configSet Configuration set name
 * @throws SolrException Exception
 */
public void createCore(String coreName, String configSet) throws SolrException {
    try {
        logger.debug("Creating core: host={}, core={}, configSet={}", host, coreName, configSet);
        CoreAdminRequest.Create request = new CoreAdminRequest.Create();
        request.setCoreName(coreName);
        request.setConfigSet(configSet);
        request.process(solrClient);
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.CONFLICT, e);
    }
}