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:nl.knaw.huygens.facetedsearch.LocalSolrServer.java

License:Open Source License

@Override
public void setServer() {
    try {/*from   ww  w.  ja  v a2s. c o  m*/
        container = new CoreContainer(solrDir);
        container.load();
        server = new EmbeddedSolrServer(container, coreName);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:org.ala.lucene.SolrIndexTest.java

License:Open Source License

/**
 * @param args/*from w  ww  .  java  2  s .  c  o  m*/
 */
public static void main(String[] args) throws Exception {

    System.out.print("Quick search test >>> ");
    String input = "";

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (!"q".equals((input = br.readLine()))) {
        long start = System.currentTimeMillis();
        if (StringUtils.trimToNull(input) != null) {
            System.out.println("---------------------------------------------");
            input = StringUtils.trimToNull(input).toLowerCase();
            //            IndexSearcher is = new IndexSearcher("/data/lucene/taxonConcept");

            String solrHome = "/data/solr/bie";

            /**
              * Initialise the SOLR server instance
              */
            System.setProperty("solr.solr.home", solrHome);
            CoreContainer coreContainer = null;
            try {
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            } catch (Exception e) {
                //FIXME this is a hack - there must be a better way of initialising SOLR here
                Directory dir = FSDirectory.open(new File(solrHome + "/index"));
                IndexWriterConfig indexWriterConfig = new IndexWriterConfig(SolrUtils.BIE_LUCENE_VERSION,
                        new StandardAnalyzer(SolrUtils.BIE_LUCENE_VERSION));
                IndexWriter idxWriter = new IndexWriter(dir, indexWriterConfig);
                idxWriter.commit();
                idxWriter.close();
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            }

            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

            SolrQuery query = new SolrQuery(input);
            QueryResponse qr = server.query(query);

            SolrDocumentList sdl = qr.getResults();
            Iterator iter = sdl.iterator();
            while (iter.hasNext()) {
                SolrDocument doc = (SolrDocument) iter.next();
                System.out.print(doc.getFieldValue("guid"));
                System.out.print("\t\t");
                Object valuesList = doc.getFieldValue("name");

                if (valuesList instanceof List) {
                    List<String> values = (List<String>) valuesList;
                    for (String s : values) {
                        System.out.println(">>  name: " + s);
                    }
                } else {
                    System.out.println(">>  name: " + valuesList);
                }

                System.out.print(doc.getFieldValue("guid"));
                System.out.println();
            }

            System.out.println();
        }
        System.out.print("Quick search test >>> ");
    }

    System.out.println("bye bye");
    System.exit(1);
}

From source file:org.ambraproject.testutils.EmbeddedSolrServerFactory.java

License:Apache License

/**
 * Constructor.// www.  java2 s. c  om
 *
 * TODO: this constructor does a lot of IO, and can throw IOException, not ideal
 * in a constructor.  Refactor to eliminate this.
 */
public EmbeddedSolrServerFactory() throws IOException {

    // Build a directory structure for the embedded solr server's solr home,
    // and copy configuration files there from the classpath.
    solrHome = System.getProperty("java.io.tmpdir") + File.separator + "EmbeddedSolrTest_"
            + System.currentTimeMillis();
    File conf = new File(solrHome + File.separator + "collection1" + File.separator + "conf");
    if (!conf.mkdirs()) {
        throw new RuntimeException("Could not create dir " + conf.getCanonicalPath());
    }
    destSolrXmlFile = solrHome + File.separator + "solr.xml";
    copyResource("solr/collection1/conf/test-solr.xml", destSolrXmlFile);
    copyResource("solr/collection1/conf/test-solr-config.xml",
            conf.getCanonicalPath() + File.separator + "solrconfig.xml");
    copyResource("solr/collection1/conf/test-solr-schema.xml",
            conf.getCanonicalPath() + File.separator + "schema.xml");
    copyResource("solr/collection1/conf/stopwords.txt",
            conf.getCanonicalPath() + File.separator + "stopwords.txt");
    copyResource("solr/collection1/conf/author_stopwords.txt",
            conf.getCanonicalPath() + File.separator + "author_stopwords.txt");

    System.setProperty("solr.solr.home", solrHome);
    CoreContainer coreContainer = new CoreContainer(solrHome, new File(destSolrXmlFile));
    server = new EmbeddedSolrServer(coreContainer, "collection1");
    log.info("EmbeddedSolrServer started with solr home " + solrHome);
}

From source file:org.anon.smart.d2cache.store.index.solr.SolrConnection.java

License:Open Source License

public void open(String name) throws CtxException {
    try {/*from  w  w  w  .j  av a2s.c o  m*/
        if (_server == null) {
            String corename = name;
            if (name != null) {
                corename = name.split(CORENAME_DELIM, 2)[0];
            }

            //System.out.println("CORE NAME is :"+corename);
            File f = new File(_home, corename);
            if (!f.exists()) {
                //has not yet been created. Create again.
                URL url = getClass().getClassLoader().getResource(SOLR_CONFIG);
                assertion().assertTrue((url != null),
                        "The solr config and schema jars are not a part of the classpath. Please include it.");
                url = getClass().getClassLoader().getResource(SCHEMA_CONFIG);
                assertion().assertTrue((url != null),
                        "The solr config and schema jars are not a part of the classpath. Please include it.");
                f.mkdirs(); //create the directory

                //have to test if there needs to be synchronization
                _server = new EmbeddedSolrServer(_container, DEFAULT_CORE);
                CoreAdminRequest.createCore(corename, corename, _server, SOLR_CONFIG, SCHEMA_CONFIG);
                CoreAdminRequest.persist(CORE_CONFIG, _server);
                _server = new EmbeddedSolrServer(_container, corename);
                _logger.info("Solr server opened for connections:" + _server + "::" + corename);
            } else {
                _server = new EmbeddedSolrServer(_container, corename);
                _logger.info(
                        ">>>>>>>>>>>>>>>>>Solr server opened for connections:" + _server + "::" + corename);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        except().rt(e, new CtxException.Context("SolrConnection.open", "Exception"));
    }

}

From source file:org.apache.coheigea.bigdata.solr.SolrTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    CoreContainer container = new CoreContainer("target/test-classes/solr");
    container.load();//from  www.j  a  va 2s.c  om

    server = new EmbeddedSolrServer(container, "core1");
}

From source file:org.apache.droids.solr.AdvancedSolrHandleTest.java

License:Apache License

@Override
public void setUp() throws Exception {
    super.setUp();

    SolrResourceLoader loader = new SolrResourceLoader(getSolrHome());
    CoreContainer container = new CoreContainer(loader);
    CoreDescriptor descriptor = new CoreDescriptor(container, "cname", ".");
    SolrCore core = container.create(descriptor);
    container.register(core.getName(), core, false);

    solr = new EmbeddedSolrServer(container, core.getName());
}

From source file:org.apache.hadoop.chukwa.datacollection.writer.solr.TestSolrWriter.java

License:Apache License

public void setUp() {
    try {/* ww w  .  j  ava 2 s  .  co  m*/
        String dataDir = System.getProperty("CHUKWA_DATA_DIR", "target/test/var");
        container = new CoreContainer(dataDir);
        container.load();

        server = new EmbeddedSolrServer(container, "collection1");
        super.setUp();
    } catch (Exception e) {
        log.error(ExceptionUtil.getStackTrace(e));
        Assert.fail(e.getMessage());
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.solr.server.EmbeddedSolrServerProvider.java

License:Apache License

private SolrServer createSolrServer() throws Exception {

    log.info("creating new embedded solr server with config: {}", solrServerConfiguration);

    String solrHomePath = solrServerConfiguration.getSolrHomePath();
    String coreName = solrServerConfiguration.getCoreName();
    EmbeddedSolrServerConfiguration.HttpConfiguration httpConfiguration = solrServerConfiguration
            .getHttpConfiguration();/*  w w  w. j av  a 2 s. com*/

    if (solrHomePath != null && coreName != null) {
        checkSolrConfiguration(solrHomePath, coreName);
        if (httpConfiguration != null) {
            if (log.isInfoEnabled()) {
                log.info("starting embedded Solr server with http bindings");
            }
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(JettySolrRunner.class.getClassLoader());

            Integer httpPort = httpConfiguration.getHttpPort();
            String context = httpConfiguration.getContext();
            JettySolrRunner jettySolrRunner = null;
            try {
                jettySolrRunner = new JettySolrRunner(solrHomePath, context, httpPort, "solrconfig.xml",
                        "schema.xml", true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner instantiated");
                }
                jettySolrRunner.start(true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner started");
                }
            } catch (Exception t) {
                if (log.isErrorEnabled()) {
                    log.error("an error has occurred while starting Solr Jetty", t);
                }
            } finally {
                if (jettySolrRunner != null && !jettySolrRunner.isRunning()) {
                    try {
                        jettySolrRunner.stop();
                        if (log.isInfoEnabled()) {
                            log.info("Jetty runner stopped");
                        }
                    } catch (Exception e) {
                        if (log.isErrorEnabled()) {
                            log.error("error while stopping the Jetty runner", e);
                        }
                    }
                }
                Thread.currentThread().setContextClassLoader(classLoader);
            }
            if (log.isInfoEnabled()) {
                log.info("starting HTTP Solr server");
            }
            return new HttpWithJettySolrServer(
                    SolrServerConfigurationDefaults.LOCAL_BASE_URL + ':' + httpPort + context + '/' + coreName,
                    jettySolrRunner);
        } else {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());

            CoreContainer coreContainer = new CoreContainer(solrHomePath);
            try {
                if (!coreContainer.isLoaded(coreName)) {
                    coreContainer.load();
                }
            } catch (Exception e) {
                log.error("cannot load core {}, shutting down embedded Solr..", coreName, e);
                try {
                    coreContainer.shutdown();
                } catch (Exception se) {
                    log.error("could not shutdown embedded Solr", se);
                }
                return null;
            } finally {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, coreName);
            if (server.ping().getStatus() == 0) {
                return server;
            } else {
                throw new IOException("the embedded Solr server is not alive");
            }
        }
    } else {
        throw new Exception("SolrServer configuration proprties not set");
    }
}

From source file:org.apache.logging.log4j.nosql.appender.solr.SolrProvider.java

License:Apache License

/**
 * create SolrProvider with EmbeddedSolrServer.
 *//*from   w  ww .  j  a  va  2 s . c o m*/
private static SolrProvider getEmbeddedSolrServer(String solrHome, String coreName, int commitWithinMs) {
    CoreContainer coreContainer = new CoreContainer(solrHome);
    coreContainer.load();
    String description = "EmbeddedSolrServer(\"" + solrHome + "\", \"" + coreName + "\")";

    return new SolrProvider(new EmbeddedSolrServer(coreContainer, coreName), commitWithinMs, description);
}

From source file:org.apache.nifi.processors.solr.EmbeddedSolrServerFactory.java

License:Apache License

/**
 *
 * @param solrHome// w w  w.  ja  v a2 s.c o m
 *              path to directory where solr.xml lives
 * @param coreName
 *              the name of the core to load
 * @param dataDir
 *              the data dir for the core
 *
 * @return an EmbeddedSolrServer for the given core
 */
public static SolrClient create(String solrHome, String coreName, String dataDir) throws IOException {

    Map<String, String> props = new HashMap<>();
    if (dataDir != null) {
        File coreDataDir = new File(dataDir + "/" + coreName);
        if (coreDataDir.exists()) {
            FileUtils.deleteDirectory(coreDataDir);
        }
        props.put("dataDir", dataDir + "/" + coreName);
    }

    final CoreContainer coreContainer = new CoreContainer(solrHome);
    coreContainer.load();

    return new EmbeddedSolrServer(coreContainer, coreName);
}