Example usage for org.apache.solr.client.solrj.embedded EmbeddedSolrServer EmbeddedSolrServer

List of usage examples for org.apache.solr.client.solrj.embedded EmbeddedSolrServer EmbeddedSolrServer

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.embedded EmbeddedSolrServer EmbeddedSolrServer.

Prototype

public EmbeddedSolrServer(CoreContainer coreContainer, String coreName) 

Source Link

Document

Create an EmbeddedSolrServer wrapping a CoreContainer.

Usage

From source file:jp.aegif.nemaki.NemakiCoreAdminHandler.java

License:Open Source License

/**
 * Switch actions on REST API/*from  ww w.ja  v  a2 s .c o  m*/
 *
 * Boolean return value is used as "doPersist" parameter,
 * which relate to the persistence of action results to the core.
 */
@Override
protected void handleCustomAction(SolrQueryRequest req, SolrQueryResponse rsp) {

    SolrParams params = req.getParams();

    // Get Server & Tracker info
    String indexCoreName = params.get(CoreAdminParams.CORE);
    String tokenCoreName = "token";

    SolrServer indexServer = new EmbeddedSolrServer(coreContainer, indexCoreName);
    SolrServer tokenServer = new EmbeddedSolrServer(coreContainer, tokenCoreName);
    SolrCore core = getCoreContainer().getCore(indexCoreName);
    CoreTracker tracker = new CoreTracker(this, core, indexServer, tokenServer);

    // Stop cron when executing action
    try {
        if (scheduler != null)
            scheduler.standby();
    } catch (SchedulerException e) {
        logger.error("Stop cron when executing action error:", e);
    }

    // Action
    doAction(rsp, tracker, params);

    // Restart cron
    try {
        if (scheduler != null)
            scheduler.start();
    } catch (SchedulerException e) {
        logger.error("Restart cron error:", e);
    }

}

From source file:jp.co.tis.gsp.tools.dba.dialect.SolrDialect.java

License:Apache License

private EmbeddedSolrServer createSolrServer(Map<String, String> urlParams) {
    String solrHome = urlParams.get("SOLR_HOME");
    StringBuilder paramStr = new StringBuilder();
    if (solrHome != null) {
        System.setProperty("solr.solr.home", solrHome);
        paramStr.append(";SOLR_HOME=").append(solrHome);
    }//from  w w w . ja  v  a2s.  c  om
    String dataDir = urlParams.get("DATA_DIR");
    if (dataDir != null) {
        System.setProperty("solr.data.dir", dataDir);
        paramStr.append(";DATA_DIR=").append(dataDir);
    }
    try {
        DriverManager.getConnection("jdbc:solr:s" + paramStr.toString());
    } catch (SQLException e) {
        throw new SQLRuntimeException(e);
    }
    CoreContainer.Initializer initializer = new CoreContainer.Initializer();
    CoreContainer coreContainer = initializer.initialize();
    return new EmbeddedSolrServer(coreContainer, coreContainer.getDefaultCoreName());

}

From source file:learning.SolrJTest.java

License:Mozilla Public License

private SolrServer startUpSolrServer() throws Exception {
    File solrConfigXml = new File(solrHomeRelativePath + solrXMLHomeRelativePath);
    String solrHome = solrHomeRelativePath;
    coreContainer = new CoreContainer(solrHome, solrConfigXml);
    SolrServer solrServer = new EmbeddedSolrServer(coreContainer, defaultCollectionName);
    return solrServer;
}

From source file:learning.SolrJTest.java

License:Mozilla Public License

@Test
@Ignore("its taking memory to create multicores and we also dont need that feature")
public void can_query_a_new_core() throws Exception {
    String coreName = UUID.randomUUID().toString();
    SolrCore solrCore = coreContainer.create(new CoreDescriptor(coreContainer, coreName, "."));
    coreContainer.register(solrCore, true);
    solrServer = new EmbeddedSolrServer(coreContainer, coreName);
    solrServer.add(doc1Example());/* w  w w .  ja  v a 2s .  c om*/
    solrServer.commit();
    SolrDocumentList documents = getAllDocsUsingSolr();
    assertThat(documents.size(), is(1));
    solrServer = new EmbeddedSolrServer(coreContainer, defaultCollectionName);
    documents = getAllDocsUsingSolr();
    assertThat(documents.size(), is(0));
}

From source file:lux.solr.LuxSolrTest.java

License:Mozilla Public License

@Test
public void testCreateCore() throws Exception {
    SolrQuery q = new SolrQuery();
    q.setRequestHandler(coreContainer.getAdminPath());
    q.setParam("action", "CREATE");
    q.setParam("name", "core3");
    q.setParam("instanceDir", "core3");
    solr.query(q);/*from  w  w  w  .  j  ava 2  s  .c  o  m*/
    SolrServer core3 = new EmbeddedSolrServer(coreContainer, "core3");
    core3.deleteByQuery("*:*");
    core3.commit();
    assertSolrQueryCount(0, "*:*", core3);
    // main core still works
    assertQueryCount(1, 102, "xs:integer", "102", "count(collection())", solr);
    // new core working too
    assertQueryCount(1, 0, "xs:integer", "0", "count(collection())", core3);
}

From source file:lux.solr.TLogTest.java

License:Mozilla Public License

@Test
@Ignore// ww w . j  a v a  2  s. c  o m
public void testTransactionLog() throws Exception {
    solrCore = null;

    // add some documents
    Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
    BaseSolrTest.addSolrDocFromFile("src/test/resources/conf/schema.xml", docs);
    BaseSolrTest.addSolrDocFromFile("src/test/resources/conf/solrconfig.xml", docs);
    solr.add(docs);

    QueryResponse response = search("lux_uri:src/test/resources/conf/schema.xml");
    assertEquals(0, response.getResults().getNumFound());

    // soft commit -- note must waitSearcher in order to see commit
    // we want it in the tlog, but not saved out to the index yet
    solr.commit(true, true, true);
    response = search("lux_uri:src/test/resources/conf/schema.xml");
    assertEquals(1, response.getResults().getNumFound());
    assertEquals("src/test/resources/conf/schema.xml", response.getResults().get(0).get("lux_uri"));
    List<?> xml = (List<?>) response.getResults().get(0).get("lux_xml");
    schemaXml = new TinyBinary((byte[]) xml.get(0), UTF8);

    // copy contents of solr data folder to temporary area to simulate hard shutdown
    copyDirectory("solr/collection1/data/tlog", "solr/tlog-backup");

    System.out.println("shut down solr");

    // shut down
    coreContainer.getCore("collection1").close();
    //solr.shutdown();

    coreContainer.shutdown();
    solr.shutdown();
    File lock = new File("solr/collection1/data/index/write.lock");
    if (lock.exists()) {
        System.err.println("solr did not shut down cleanly");
        assertTrue(lock.delete());
    }

    // restore contents of data directory to before we shutdown
    removeDirectory("solr/collection1/data/tlog");
    copyDirectory("solr/tlog-backup", "solr/collection1/data/tlog");
    removeDirectory("solr/tlog-backup");

    System.out.println("start solr up again");

    // start up again
    coreContainer = new CoreContainer();
    coreContainer.load();
    solr = new EmbeddedSolrServer(coreContainer, "collection1");

    // retrieve the documents (from the replayed transaction log):
    validateContent();

    // commit
    solr.commit();
    validateContent();
    coreContainer.getCore("collection1").close();
}

From source file:mecha.db.SolrCore.java

License:Apache License

public SolrCore(String solrHomePath, String solrCoreName, boolean createIfNotExist) throws Exception {
    homePath = solrHomePath;//from ww  w .  j av  a 2 s .  c o m
    coreName = solrCoreName;

    /* zzz de-multi-core zzz
    if (createIfNotExist) {
    log.info("calling createCore(" + solrCoreName + ", " + solrCoreName + ")");
    CoreAdminRequest.createCore(
        solrCoreName, 
        solrCoreName, 
        Mecha.getSolrManager().getCore("index").getServer(),
            "./solr/" + solrCoreName + "/conf/solrconfig.xml", 
            "./solr/_p/conf/schema.xml");
    }
    */

    log.info("starting solr core [" + homePath + "] " + coreName);
    File f = new File(new File(solrHomePath), "solr.xml");
    container = new CoreContainer();
    container.load(solrHomePath, f);
    server = new EmbeddedSolrServer(container, solrCoreName);
}

From source file:net.hasor.search.server.rsf.service.AbstractSearchService.java

License:Apache License

protected SolrClient getSolrClient() {
    String coreName = this.rsfRequest.getBindInfo().getBindGroup();
    return new EmbeddedSolrServer(this.container, coreName);
}

From source file:net.yacy.cora.federate.solr.instance.EmbeddedInstance.java

License:Open Source License

public EmbeddedInstance(final File solr_config, final File containerPath, String givenDefaultCoreName,
        String[] initializeCoreNames) throws IOException {
    super();/*w ww.  j  a v  a 2 s. c  o m*/
    // copy the solrconfig.xml to the storage path
    this.containerPath = containerPath;

    // ensure that default core path exists
    File defaultCorePath = new File(containerPath, givenDefaultCoreName);
    if (!defaultCorePath.exists())
        defaultCorePath.mkdirs();

    // migrate old conf directory
    File oldConf = new File(containerPath, "conf");
    File confDir = new File(defaultCorePath, "conf");
    if (oldConf.exists())
        oldConf.renameTo(confDir);

    // migrate old data directory
    File oldData = new File(containerPath, "data");
    File dataDir = new File(defaultCorePath, "data");
    if (oldData.exists())
        oldData.renameTo(dataDir);

    // create index subdirectory in data if it does not exist
    File indexDir = new File(dataDir, "index");
    if (!indexDir.exists())
        indexDir.mkdirs();

    // initialize the cores' configuration
    for (String coreName : initializeCoreNames) {
        initializeCoreConf(solr_config, containerPath, coreName);
    }

    // initialize the coreContainer
    String containerDir = this.containerPath.getAbsolutePath(); // the home directory of all cores
    File configFile = new File(solr_config, "solr.xml"); //  the configuration file for all cores
    this.coreContainer = CoreContainer.createAndLoad(containerDir, configFile); // this may take indefinitely long if solr files are broken
    if (this.coreContainer == null)
        throw new IOException(
                "cannot create core container dir = " + containerDir + ", configFile = " + configFile);

    // get the default core from the coreContainer
    this.defaultCoreName = givenDefaultCoreName;
    ConcurrentLog.info("SolrEmbeddedInstance", "detected default solr core: " + this.defaultCoreName);
    this.defaultCore = this.coreContainer.getCore(this.defaultCoreName);
    assert givenDefaultCoreName.equals(this.defaultCore.getName()) : "givenDefaultCoreName = "
            + givenDefaultCoreName + ", this.defaultCore.getName() = " + this.defaultCore.getName();
    if (this.defaultCore == null) {
        throw new IOException("cannot get the default core; available = " + MemoryControl.available()
                + ", free = " + MemoryControl.free());
    }
    this.defaultCoreServer = new EmbeddedSolrServer(this.coreContainer, this.defaultCoreName);

    // initialize core cache
    this.cores = new ConcurrentHashMap<String, SolrCore>();
    this.cores.put(this.defaultCoreName, this.defaultCore);
    this.server = new ConcurrentHashMap<String, SolrClient>();
    this.server.put(this.defaultCoreName, this.defaultCoreServer);
}

From source file:net.yacy.cora.federate.solr.instance.EmbeddedInstance.java

License:Open Source License

@Override
public SolrClient getServer(String coreName) {
    SolrClient s = this.server.get(coreName);
    if (s != null)
        return s;
    s = new EmbeddedSolrServer(this.coreContainer, coreName);
    this.server.put(coreName, s);
    return s;/*from www .  j a v  a 2 s .  co m*/
}