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

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

Introduction

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

Prototype

public CoreContainer() 

Source Link

Document

Create a new CoreContainer using system properties to detect the solr home directory.

Usage

From source file:de.cosmocode.solr.SolrJServerTest.java

License:Apache License

/**
 * Starts the Solr server before all tests run.
 * /*from   w w  w .  ja  v  a2s. co  m*/
 * @throws SAXException if an error occurs
 * @throws IOException if an error occurs
 * @throws ParserConfigurationException if an error occurs
 */
@BeforeClass
public static void startServer() throws ParserConfigurationException, IOException, SAXException {
    LOG.debug("Starting Solr Server...");

    final File solrDirectory = new File("src/test/resources/solr");
    final File xml = new File(solrDirectory, "conf/solrconfig.xml");
    final File data = new File(solrDirectory, "data");

    tmpData = new File(solrDirectory, "tmpData");
    FileUtils.copyDirectory(data, tmpData);
    tmpIndex = new File(tmpData, "index");

    final CoreContainer container = new CoreContainer();
    final SolrConfig config = new SolrConfig(solrDirectory.getAbsolutePath(), xml.getAbsolutePath(), null);
    final CoreDescriptor descriptor = new CoreDescriptor(container, "core1", tmpIndex.getAbsolutePath());

    core = new SolrCore("core1", tmpData.getAbsolutePath(), config, null, descriptor);
    container.register("core1", core, false);
    server = new EmbeddedSolrServer(container, "core1");

    LOG.debug("Started Solr Server");
}

From source file:eu.europeana.solr.SimpleCollectionSolrInstance.java

License:Apache License

/**
 * This method creates an in-process Solr server that otherwise behaves just
 * as youd expect.//from  www.ja  v  a  2s  .  c om
 */
private synchronized SolrServer getDelegate() throws SolrServerException {
    if (delegate != null) {
        return delegate;
    }

    try {

        final File dir = FileUtils.getTempDirectory();
        System.setProperty("solr.solr.home", dir.getAbsolutePath());
        final File conf = new File(new File(solrdir, "conf"), "solrconfig.xml");

        final CoreContainer cc = new CoreContainer();
        final SolrConfig sc = new SolrConfig(conf.getAbsolutePath());
        final CoreDescriptor cd = new CoreDescriptor(cc, CORE1, solrdir.getAbsolutePath());

        core = cc.create(cd);
        cc.register(CORE1, core, false);
        delegate = new EmbeddedSolrServer(cc, CORE1);
        return delegate;

    } catch (ParserConfigurationException ex) {
        throw new SolrServerException(ex);
    } catch (SAXException ex) {
        throw new SolrServerException(ex);
    } catch (IOException ex) {
        throw new SolrServerException(ex);
    }
}

From source file:eu.europeana.solr.SolrServerTester.java

License:Apache License

/**
 * This method creates an in-process Solr server that otherwise behaves just
 * as youd expect.//from w  ww.j  a  v  a  2 s  .com
 */
private synchronized SolrServer getDelegate() throws SolrServerException {
    if (delegate != null) {
        return delegate;
    }

    try {
        // File solrconfigXml = new File(new File(solrdir, "conf"),
        // "solrconfig.xml");
        //
        // logger.info("solrconfig = {}",solrconfigXml);
        //
        // CoreContainer container = new CoreContainer(solrdir.getPath(),
        // solrconfigXml);
        // CoreDescriptor descriptor = new CoreDescriptor(container,
        // "collection1",
        // solrdir.getCanonicalPath());
        //
        // core = container.create(descriptor);
        // container.register("collection1", core, false);
        // delegate = new EmbeddedSolrServer(container, "collection1");
        //
        // return delegate;

        final File dir = FileUtils.getTempDirectory();
        System.setProperty("solr.solr.home", dir.getAbsolutePath());
        final File conf = new File(new File(solrdir, "conf"), "solrconfig.xml");

        final CoreContainer cc = new CoreContainer();
        final SolrConfig sc = new SolrConfig(conf.getAbsolutePath());
        final CoreDescriptor cd = new CoreDescriptor(cc, CORE1, solrdir.getAbsolutePath());

        core = cc.create(cd);
        cc.register(CORE1, core, false);
        delegate = new EmbeddedSolrServer(cc, CORE1);
        return delegate;

    } catch (ParserConfigurationException ex) {
        throw new SolrServerException(ex);
    } catch (SAXException ex) {
        throw new SolrServerException(ex);
    } catch (IOException ex) {
        throw new SolrServerException(ex);
    }
}

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();/*from w w  w  .  j  a v a2s .  c o  m*/
    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);

    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:lux.solr.TLogTest.java

License:Mozilla Public License

@Test
@Ignore/*from  ww w .  j av  a  2 s. com*/
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   w  ww.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:org.alfresco.solr.AlfrescoSolrCloseHookTest.java

License:Open Source License

@Before
public void setUp() throws Exception {
    when(trackerRegistry.getModelTracker()).thenReturn(modelTracker);
    coreTrackers = Arrays.asList(new Tracker[] { contentTracker, metadataTracker, aclTracker });
    when(trackerRegistry.getTrackersForCore(CORE_NAME)).thenReturn(coreTrackers);
    when(trackerRegistry.getCoreNames()).thenReturn(new HashSet<String>(Arrays.asList(CORE_NAME)));
    when(adminHandler.getTrackerRegistry()).thenReturn(trackerRegistry);
    when(adminHandler.getScheduler()).thenReturn(scheduler);

    core = new SolrCore(CORE_NAME, new CoreDescriptor(new CoreContainer(), CORE_NAME, "instanceDir"));
    hook = new AlfrescoSolrCloseHook(adminHandler);
}

From source file:org.kimios.kernel.index.solr.SearchServiceSolrFactory.java

License:Open Source License

private static SolrServer initLocalServer(String solrHome, String coreName) {
    try {/*from   w w  w.  j  a v  a 2  s  .  com*/

        log.info("SOLR HOME " + solrHome);

        URL sorlHomeUrl = new URL("file://" + solrHome);
        File home = new File(sorlHomeUrl.getFile());
        checkSolrXmlFile(home, coreName);

        /*
        Check solr.xml existence. If not exist (create it)
         */

        File f = new File(home, "solr.xml");
        CoreContainer container = new CoreContainer();
        container.load(solrHome, f);
        coreContainer = container;
        EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, coreName);
        return server;
    } catch (Exception ex) {
        log.error("Error initializing SOLR server", ex);
        return null;
    }
}

From source file:org.openmrs.module.chartsearch.server.EmbeddedSolrServerCreator.java

License:Mozilla Public License

@Override
public SolrServer createSolrServer() {

    // If user has not setup solr config folder, set a default one
    // TODO use solr functions to determine config folder
    String configFolderPath = properties.getSolrHome() + File.separatorChar + "collection1" + File.separatorChar
            + "conf";
    File configFolder = new File(configFolderPath);
    if (configFolder.exists()) {
        try {/*from w w  w.ja va  2  s . c om*/
            FileUtils.deleteDirectory(configFolder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    URL url = getClass().getClassLoader().getResource("collection1/conf");
    try {
        File file = new File(url.toURI());
        FileUtils.copyDirectoryToDirectory(file,
                new File(properties.getSolrHome() + File.separatorChar + "collection1"));
        setDataImportConnectionInfo(configFolderPath);
    } catch (IOException e) {
        log.error("Failed to copy Solr config folder", e);
    } catch (Exception e) {
        log.error("Failed to set dataImport connection info", e);
    }

    // Get the solr home folder
    // Tell solr that this is our home folder
    System.setProperty("solr.solr.home", properties.getSolrHome());

    log.info(String.format("solr.solr.home: %s", properties.getSolrHome()));

    /*CoreContainer.Initializer initializer = new CoreContainer.Initializer();
    CoreContainer coreContainer;
    try {
       coreContainer = initializer.initialize();
       solrServer = new EmbeddedSolrServer(coreContainer, "");
       return solrServer;
    }
    catch (FileNotFoundException e) {
       e.printStackTrace();
       return null;
    }
    catch (Exception e) {
       e.printStackTrace();
       return null;
    }*/

    CoreContainer coreContainer = new CoreContainer();
    try {
        coreContainer.load();
        solrServer = new EmbeddedSolrServer(coreContainer, "");
        return solrServer;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:org.opensextant.util.SolrProxy.java

License:Apache License

/**
 * Creates an EmbeddedSolrServer given solr home &amp; the core to use.
 * These may be null and you get the default.
 *
 * @param _solrHome  solr home// w  w  w . j  ava2 s  .c o m
 * @param _coreName  name of core
 * @return the embedded solr server
 * @throws ConfigException on err
 */
public static EmbeddedSolrServer setupCore(String _solrHome, String _coreName) throws ConfigException {

    try {
        CoreContainer solrContainer;

        if (_solrHome == null) {
            solrContainer = new CoreContainer();
        } else {
            solrContainer = new CoreContainer(_solrHome);
        }
        solrContainer.load();// since Solr 4.4

        return new EmbeddedSolrServer(solrContainer, _coreName);

    } catch (Exception err) {
        throw new ConfigException("Failed to set up Embedded Solr at " + _solrHome + " CORE:" + _coreName, err);
    }
}