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:org.dataconservancy.dcs.index.dcpsolr.SolrHomeSetBySpringPropertiesTest.java

License:Apache License

@Test
public void testStartSolrDefaultContainer()
        throws IOException, SolrServerException, SAXException, ParserConfigurationException {
    System.setProperty("solr.solr.home", solrHome);
    final File solrXml = new File(solrHome, "solr.xml");
    assertTrue("Cannot find '" + solrXml.getAbsolutePath(),
            solrXml.exists() && solrXml.canRead() && solrXml.isFile());
    final CoreContainer container = new CoreContainer(solrHome, solrXml);
    final EmbeddedSolrServer server = new EmbeddedSolrServer(container, "default");

    // TODO: I'm not sure how to interpret the ping status response.
    //        final int pingStatus = server.ping().getStatus();
    //        assertTrue("Unable to start Embedded Solr Server: " + pingStatus,
    //                pingStatus >= 200 && pingStatus < 400);
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrHomeSetBySystemPropertyTest.java

License:Apache License

@Test
public void testStartSolrDefaultContainer() throws IOException, SolrServerException {
    CoreContainer container = new CoreContainer(System.getProperty("solr.solr.home"));
    EmbeddedSolrServer server = new EmbeddedSolrServer(container, "default");

    // TODO: I'm not sure how to interpret the ping status response.
    //        final SolrPingResponse pingResponse = server.ping();
    //        final int pingStatus = pingResponse.getStatus();
    //        assertTrue("Unable to start Embedded Solr Server: " + pingStatus + " (" + pingResponse.getElapsedTime() + ")",
    //                pingStatus >= 200 && pingStatus < 400);
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrService.java

License:Apache License

/**
 * Returns an SolrService attached to the SolrServer specified by the system
 * property solr.solr.home. If solr home is a full url, it connects to the
 * solr server running on that url. Otherwise solr home is treated as a file
 * path and an embedded solr server is started.
 *
 * @throws IOException/*from w  w  w . ja  v  a  2  s . c o m*/
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IllegalStateException if the <code>solr.solr.home</code> System property is not set
 */
public SolrService() throws IOException, ParserConfigurationException, SAXException {
    String solrhome = System.getProperty("solr.solr.home");

    if (solrhome == null) {
        throw new IllegalStateException("System property solr.solr.home must be set to a url or local path");
    }

    boolean embedded = true;

    try {
        if (new URL(solrhome).getHost() != null) {
            embedded = false;
        }
    } catch (MalformedURLException e) {
    }

    if (embedded) {
        final File solrXml = new File(solrhome, "solr.xml");
        log.debug("Instantiating new SolrService (EmbeddedSolrServer) with solr.solr.home '{}', solr.xml '{}'",
                solrhome, solrXml);
        this.container = new CoreContainer(solrhome, solrXml);
        this.server = new EmbeddedSolrServer(container, "default");
    } else {
        this.container = null;
        log.debug("Instantiating new SolrService (CommonsHttpSolrServer) with solr.solr.home '{}'", solrhome);
        CommonsHttpSolrServer httpserv = new CommonsHttpSolrServer(solrhome);
        httpserv.setAllowCompression(true);
        this.server = httpserv;
    }
}

From source file:org.dataconservancy.dcs.index.dcpsolr.SolrService.java

License:Apache License

/**
 * Instantiates a SolrService backed by an EmbeddedSolrServer.  Unlike the other constructors for SolrService,
 * this one does not expect a <code>solr.solr.home</code> system property to be set; it will set the property.
 * The provided <code>solrhome</code> is used to set the system property <code>solr.solr.home</code>.  Existing
 * (if any) values for the property <code>solr.solr.home</code> are overwritten.
 *
 * @param solrhome the path to the Solr home directory
 * @throws IOException// w  ww  .  j av a2s. com
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public SolrService(File solrhome) throws IOException, ParserConfigurationException, SAXException {

    /* Not strictly necessary... */
    System.setProperty("solr.solr.home", solrhome.getCanonicalPath());

    final File solrXml = new File(solrhome, "solr.xml");
    log.debug("Instantiating new SolrService (EmbeddedSolrServer) with solr.solr.home '{}', solr.xml '{}'",
            solrhome, solrXml);
    this.container = new CoreContainer(solrhome.getCanonicalPath(), solrXml);
    this.server = new EmbeddedSolrServer(container, "default");
}

From source file:org.dspace.solr.MockSolrServer.java

License:BSD License

private static synchronized SolrServer initSolrServerForCore(final String coreName) {
    SolrServer server = loadedCores.get(coreName);
    if (server == null) {
        initSolrContainer();/*from  w  w w  . j  av a  2 s  . c  o  m*/

        server = new EmbeddedSolrServer(container, coreName);

        //Start with an empty index
        try {
            server.deleteByQuery("*:*");
            server.commit();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }

        loadedCores.put(coreName, server);
        log.info("SOLR Server for core " + coreName + " initialized");
    }
    return server;
}

From source file:org.easyrec.plugin.profilesolr.SolrSimilarityGenerator.java

License:Open Source License

@Override
protected void doInitialize() throws Exception {
    if (solrHomeFolder == null) {
        File target = pluginFolder.getFile();
        solrHomeFolder = Paths.get(target.getPath(), "solr");
    }//from  w  ww  . j  a  v a2 s  .com
    if (Files.notExists(solrHomeFolder)) {
        doInstall();
    }

    EmbeddedSolrServer solrServer = new EmbeddedSolrServer(solrHomeFolder, "easyrec");

    //        String urlString = "http://localhost:8983/solr/easyrec";
    //        SolrClient solrClient = new HttpSolrClient(urlString);
    if (solrServer == null)
        throw new Exception("Could not initialize Solr server!");
    solrSimilarityService.setSolrClient(solrServer);
}

From source file:org.eclipse.orion.internal.server.search.SearchActivator.java

License:Open Source License

/**
 * Creates and returns a search server instance. Returns null if there was a
 * failure instantiating the server.//  w ww . j a  v  a 2s .  com
 */
private void createServer() {
    try {
        File rootFile = Activator.getDefault().getPlatformLocation().toFile();
        File baseDir = new File(rootFile, ".metadata/.plugins/" + PI_SEARCH); //$NON-NLS-1$
        // discard all server data if the index generation has changed
        if (readIndexGeneration(baseDir) != CURRENT_INDEX_GENERATION) {
            delete(baseDir);
            writeIndexGeneration(baseDir);
        }
        createSolrConfig(baseDir);
        String solrDataDir = baseDir.toString();
        solrContainer = new CoreContainer(solrDataDir);
        CoreDescriptor descriptor = new CoreDescriptor(solrContainer, "Eclipse Web Search", solrDataDir); //$NON-NLS-1$
        descriptor.setDataDir(solrDataDir.toString() + File.separatorChar + "data"); //$NON-NLS-1$
        solrCore = solrContainer.create(descriptor);
        solrContainer.register(solrCore, false);
        server = new EmbeddedSolrServer(solrContainer, "Eclipse Web Search"); //$NON-NLS-1$
    } catch (Exception e) {
        LogHelper.log(e);
    }
}

From source file:org.eclipse.rdf4j.sail.solr.client.embedded.Factory.java

License:Open Source License

@Override
public SolrClient create(String spec) {
    String solrHome = SolrResourceLoader.locateSolrHome();
    File configFile = new File(solrHome, SolrXmlConfig.SOLR_XML_FILE);
    return new EmbeddedSolrServer(CoreContainer.createAndLoad(solrHome, configFile), "embedded");
}

From source file:org.emonocot.test.EmbeddedSolrServerFactoryBean.java

License:Open Source License

@Override
public SolrServer getObject() throws Exception {
    if (solrServer == null) {
        System.setProperty("solr.solr.home", solrHome.getFile().getAbsolutePath());
        CoreContainer.Initializer initializer = new CoreContainer.Initializer();
        coreContainer = initializer.initialize();
        solrServer = new EmbeddedSolrServer(coreContainer, "collection1");
    }//from w ww.  ja v  a  2 s. c o  m
    return solrServer;
}

From source file:org.entrystore.repository.impl.RepositoryManagerImpl.java

License:Apache License

private void initSolr() {
    log.info(/*from w w  w. j  a va 2  s  .c  om*/
            "Manually setting property \"javax.xml.parsers.DocumentBuilderFactory\" to \"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl\"");
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
            "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

    boolean reindex = "on".equalsIgnoreCase(config.getString(Settings.SOLR_REINDEX_ON_STARTUP, "off"));
    String solrURL = config.getString(Settings.SOLR_URL);
    if (solrURL.startsWith("http://")) {
        log.info("Using HTTP Solr server");
        solrServer = new HttpSolrServer(solrURL);
        ((HttpSolrServer) solrServer).setAllowCompression(true);
    } else {
        log.info("Using embedded Solr server");
        File solrDir = new File(solrURL);
        if (solrDir.list() != null && solrDir.list().length == 0) {
            log.info("Solr directory is empty, scheduling conditional reindexing of repository");
            reindex = true;
        }
        try {
            System.setProperty("solr.solr.home", solrURL);
            log.info("solr.solr.home set to " + solrURL);
            // URL solrConfig = ConverterUtil.findResource("solrconfig.xml");
            // solrServer = new EmbeddedSolrServer(CoreContainer.createAndLoad(solrURL, new File(solrConfig.getPath())), "");
            CoreContainer.Initializer initializer = new CoreContainer.Initializer();
            CoreContainer coreContainer = initializer.initialize();
            solrServer = new EmbeddedSolrServer(coreContainer, "");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
    if (solrServer != null) {
        solrSupport = new SolrSupport(this, solrServer);
        if (reindex) {
            solrSupport.reindexLiterals();
        }
    } else {
        log.error("Unable to initialize Solr, check settings in scam.properties");
    }
}