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.fcrepo.indexer.SolrIndexerTest.java

License:Apache License

/**
 * @throws java.lang.Exception/*from  w w  w .  j ava 2  s  .  c o  m*/
 */
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName());
    indexer = new SolrIndexer(server);
}

From source file:org.fcrepo.indexer.SolrServerFactory.java

License:Apache License

/**
 * Returns a SolrServer instance for indexing purpose
 * //from  w  w  w  .  j a  va 2 s .  co  m
 * @return Solr server (SolrServer) instance or null if no instance could
 * be created
 */
public SolrServer getSolrServer() {
    if (!isEmbedded()) {
        return new HttpSolrServer(solrServerUrl);

    } else {
        EmbeddedSolrServer embeddedSolrServer = null;
        try {
            // trying to set up a new embedded instance
            System.setProperty("solr.solr.home", solrTestHome);
            Initializer initializer = new CoreContainer.Initializer();
            CoreContainer cc = initializer.initialize();
            embeddedSolrServer = new EmbeddedSolrServer(cc, "");
        } catch (IOException | ParserConfigurationException | SAXException e) {

            LOGGER.error("Couldn't initialize CoreContainer", e);
        }
        return embeddedSolrServer;
    }

}

From source file:org.filteredpush.dataentry.Utils.java

License:Open Source License

public static EmbeddedSolrServer startSolr(File solrDir) {
    if (!solrDir.exists()) {
        throw new Error("No solr directory at " + solrDir);
    }//from   w  ww. j a v a  2  s.  c  o  m
    CoreContainer coreContainer = new CoreContainer(solrDir.toString());
    coreContainer.load();
    return new EmbeddedSolrServer(coreContainer, "");
}

From source file:org.gbif.common.search.inject.BaseSearchModule.java

License:Apache License

/**
 * Provider method for creating the SolrServer instance.
 *
 * @param isEmbedded     flag to determine if the SolrServer is an embedded or a remote instance
 * @param solrServerPath the url(remote http url) or path (if is Embedded) to the SolrServer
 *//*from  www.  ja v a2  s . c  o  m*/
@Provides
@Singleton
protected SolrServer provideSolrServer(@Named(SOLR_SERVER_KEY) String solrServerPath,
        @Named(SOLR_EMBEDDED_KEY) Boolean isEmbedded) {
    SolrServer solrServer = null;
    try {
        LOG.info("Creating solr server with path={}", solrServerPath);
        if (isEmbedded) {
            System.setProperty(SOLR_HOME, solrServerPath);
            CoreContainer coreContainer = new CoreContainer(solrServerPath);
            solrServer = new EmbeddedSolrServer(coreContainer, "");
        } else { // remote instance
            solrServer = new HttpSolrServer(solrServerPath);
            ((HttpSolrServer) solrServer).setRequestWriter(new BinaryRequestWriter());
            ((HttpSolrServer) solrServer).setAllowCompression(true);
        }
        solrServer.ping();
    } catch (MalformedURLException e) {
        LOG.error("Error reaching remote SolrServer files", e);
        throw new SearchException(e);
    } catch (IOException e) {
        LOG.error("Error accessing SolrServer configuration files", e);
        throw new SearchException(e);
    } catch (SolrException e) {
        LOG.error("Error parsing SolrServer configuration files", e);
        throw new SearchException(e);
    } catch (SolrServerException e) {
        LOG.error("Error creating a SolrServer instance", e);
        throw new SearchException(e);
    }

    return solrServer;
}

From source file:org.gbif.ocurrence.index.solr.SolrRecordWriter.java

License:Apache License

@SuppressWarnings("unchecked")
public SolrRecordWriter(TaskAttemptContext context) {
    conf = context.getConfiguration();// ww  w .  j a v  a 2  s. c o m
    batchSize = SolrOutputFormat.getBatchSize(conf);

    setLogLevel("org.apache.solr.core", "WARN");
    setLogLevel("org.apache.solr.update", "WARN");

    heartBeater = new HeartBeater(context);
    try {
        heartBeater.needHeartBeat();
        /** The actual file in hdfs that holds the configuration. */

        final String configuredSolrConfigPath = conf.get(SolrOutputFormat.SETUP_OK);
        if (configuredSolrConfigPath == null) {
            throw new IllegalStateException(
                    String.format("The job did not pass %s", SolrOutputFormat.SETUP_OK));
        }
        outputZipFile = SolrOutputFormat.isOutputZipFormat(conf);

        this.fs = FileSystem.get(conf);
        perm = new Path(FileOutputFormat.getOutputPath(context), getOutFileName(context, "part"));

        // Make a task unique name that contains the actual index output name to
        // make debugging simpler
        // Note: if using JVM reuse, the sequence number will not be reset for a
        // new task using the jvm

        temp = conf.getLocalPath("mapred.local.dir",
                "solr_" + conf.get("mapred.task.id") + '.' + sequence.incrementAndGet());

        if (outputZipFile && !perm.getName().endsWith(".zip")) {
            perm = perm.suffix(".zip");
        }
        fs.delete(perm, true); // delete old, if any
        Path local = fs.startLocalOutput(perm, temp);

        solrHome = findSolrConfig(conf);

        // }
        // Verify that the solr home has a conf and lib directory
        if (solrHome == null) {
            throw new IOException("Unable to find solr home setting");
        }

        // Setup a solr instance that we can batch writes to
        LOG.info("SolrHome: " + solrHome.toUri());
        String dataDir = new File(local.toString(), "data").toString();
        // copy the schema to the conf dir
        File confDir = new File(local.toString(), "conf");
        confDir.mkdirs();
        File srcSchemaFile = new File(solrHome.toString(), "conf/schema.xml");
        assert srcSchemaFile.exists();
        FileUtils.copyFile(srcSchemaFile, new File(confDir, "schema.xml"));
        Properties props = new Properties();
        props.setProperty("solr.data.dir", dataDir);
        props.setProperty("solr.home", solrHome.toString());
        SolrResourceLoader loader = new SolrResourceLoader(solrHome.toString(), null, props);
        LOG.info(String.format(
                "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to temporary directory %s, with permdir %s",
                solrHome, solrHome.toUri(), loader.getInstanceDir(), loader.getConfigDir(), dataDir, perm));
        CoreContainer container = new CoreContainer(loader);
        CoreDescriptor descr = new CoreDescriptor(container, "core1", solrHome.toString());
        descr.setDataDir(dataDir);
        descr.setCoreProperties(props);
        core = container.create(descr);
        container.register(core, false);
        solr = new EmbeddedSolrServer(container, "core1");
        batchWriter = new BatchWriter(solr, batchSize, context.getTaskAttemptID().getTaskID(),
                SolrOutputFormat.getSolrWriterThreadCount(conf), SolrOutputFormat.getSolrWriterQueueSize(conf));

        // instantiate the converter
        String className = SolrDocumentConverter.getSolrDocumentConverter(conf);
        Class<? extends SolrDocumentConverter> cls = (Class<? extends SolrDocumentConverter>) Class
                .forName(className);
        converter = (SolrDocumentConverter<K, V>) ReflectionUtils.newInstance(cls, conf);
    } catch (Exception e) {
        throw new IllegalStateException(String.format("Failed to initialize record writer for %s, %s",
                context.getJobName(), conf.get("mapred.task.id")), e);
    } finally {
        heartBeater.cancelHeartBeat();
    }
}

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  va2s .c om

        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.kitesdk.morphline.solr.SolrLocator.java

License:Apache License

public SolrServer getSolrServer() {
    if (zkHost != null && zkHost.length() > 0) {
        if (collectionName == null || collectionName.length() == 0) {
            throw new MorphlineCompilationException(
                    "Parameter 'zkHost' requires that you also pass parameter 'collection'", config);
        }/*from  w  ww  .  j  a  va  2  s  .co m*/
        CloudSolrServer cloudSolrServer = new CloudSolrServer(zkHost);
        cloudSolrServer.setDefaultCollection(collectionName);
        return cloudSolrServer;
    } else {
        if (solrUrl == null && solrHomeDir != null) {
            CoreContainer coreContainer = new CoreContainer(solrHomeDir);
            coreContainer.load();
            EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(coreContainer, collectionName);
            return embeddedSolrServer;
        }
        if (solrUrl == null || solrUrl.length() == 0) {
            throw new MorphlineCompilationException("Missing parameter 'solrUrl'", config);
        }
        int solrServerNumThreads = 2;
        int solrServerQueueLength = solrServerNumThreads;
        SolrServer server = new SafeConcurrentUpdateSolrServer(solrUrl, solrServerQueueLength,
                solrServerNumThreads);
        return server;
    }
}

From source file:org.lunifera.runtime.solr.server.internal.SolrServerService.java

License:Open Source License

private EmbeddedSolrServer initialize(String name, String instanceDir) {
    if (name == null || instanceDir == null) {
        throw new RuntimeException("Core name and instance dir must be configured.");
    }/* www  .  j ava  2s .com*/

    CoreDescriptor coreDescriptor = new CoreDescriptor(coreContainer, name, instanceDir);
    SolrCore solrCore = coreContainer.create(coreDescriptor);
    coreContainer.register(solrCore, false);
    return new EmbeddedSolrServer(coreContainer, name);
}

From source file:org.mitre.opensextant.extraction.SolrProxy.java

License:Apache License

/** 
 */// w w w . j a va 2s.  c om
public static SolrServer initialize_embedded(String solr_home, String corename) throws IOException {

    try {
        File solr_xml = new File(solr_home + File.separator + "solr.xml");
        CoreContainer solrContainer = new CoreContainer(solr_home);
        solrContainer.load(solr_home, solr_xml);
        return new EmbeddedSolrServer(solrContainer, corename);
    } catch (Exception err) {
        throw new IOException("Failed to set up Embedded Solr", err);
    }
}

From source file:org.mitre.opensextant.extraction.SolrProxy.java

License:Apache License

/**
 * Much simplified EmbeddedSolr setup.// ww w.  j ava2 s. c  om
 * 
 * @throws IOException 
 */
public static SolrServer initialize_embedded() throws IOException {

    try {
        CoreContainer.Initializer initializer = new CoreContainer.Initializer();
        CoreContainer solrContainer = initializer.initialize();
        return new EmbeddedSolrServer(solrContainer, "");
    } catch (Exception err) {
        throw new IOException("Failed to set up Embedded Solr", err);
    }
}