Search in sources :

Example 6 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project jackrabbit-oak by apache.

the class EmbeddedSolrServerProvider method createSolrServer.

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();
    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");
    }
}
Also used : EmbeddedSolrServerConfiguration(org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration) CoreContainer(org.apache.solr.core.CoreContainer) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) IOException(java.io.IOException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) IOException(java.io.IOException)

Example 7 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project stanbol by apache.

the class EmbeddedSolrHelper method makeEmbeddedSolrServer.

/**
     * Create a single core Solr server with it's own folder hierarchy.
     */
public static EmbeddedSolrServer makeEmbeddedSolrServer(File rootFolder, String solrServerId, String coreId, String configName) throws IOException, ParserConfigurationException, SAXException {
    File solrFolder = new File(rootFolder, solrServerId);
    FileUtils.deleteQuietly(solrFolder);
    solrFolder.mkdir();
    // solr conf file
    File solrFile = new File(solrFolder, "solr.xml");
    InputStream is = EmbeddedSolrHelper.class.getResourceAsStream("/solr.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test solr.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(solrFile));
    // solr conf folder with schema
    File solrCoreFolder = new File(solrFolder, coreId);
    solrCoreFolder.mkdir();
    File solrConfFolder = new File(solrCoreFolder, "conf");
    solrConfFolder.mkdir();
    File schemaFile = new File(solrConfFolder, "schema.xml");
    is = EmbeddedSolrHelper.class.getResourceAsStream("/" + configName + "/conf/schema.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test schema.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(schemaFile));
    File solrConfigFile = new File(solrConfFolder, "solrconfig.xml");
    is = EmbeddedSolrHelper.class.getResourceAsStream("/" + configName + "/conf/solrconfig.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test solrconfig.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(solrConfigFile));
    // create the embedded server
    SolrResourceLoader loader = new SolrResourceLoader(solrFolder.getAbsolutePath());
    CoreContainer coreContainer = new CoreContainer(loader);
    //NOTE: with Solr 4.4 we need to call coreContainer.load() otherwise we
    //would be affected by the issue stated at 
    //http://mail-archives.apache.org/mod_mbox/lucene-solr-user/201301.mbox/%3CB7B8B36F1A0BE24F842758C318E56E925EB334%40EXCHDB2.na1.ad.group%3E
    //while this was introduced with 4.1 this only affects this code with 4.4
    //as with an API change the methods implicitly calling load() where
    //removed.
    coreContainer.load();
    CoreDescriptor coreDescriptor = new CoreDescriptor(coreContainer, coreId, solrCoreFolder.getAbsolutePath());
    SolrCore core = coreContainer.create(coreDescriptor);
    //        coreContainer.createAndLoad(solrHome, configFile)load();
    coreContainer.register(coreId, core, true);
    return new EmbeddedSolrServer(coreContainer, coreId);
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CoreContainer(org.apache.solr.core.CoreContainer) InputStream(java.io.InputStream) CoreDescriptor(org.apache.solr.core.CoreDescriptor) SolrCore(org.apache.solr.core.SolrCore) FileOutputStream(java.io.FileOutputStream) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)

Example 8 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project stanbol by apache.

the class FstLinkingEngineTest method setup.

@BeforeClass
public static void setup() throws Exception {
    // get the working directory
    // use property substitution to test this feature!
    String prefix = System.getProperty("basedir") == null ? "." : "${basedir}";
    String solrServerDir = prefix + TEST_INDEX_REL_PATH;
    log.info("Test Solr Server Directory: {}", solrServerDir);
    System.setProperty(ManagedSolrServer.MANAGED_SOLR_DIR_PROPERTY, solrServerDir);
    SolrYardConfig config = new SolrYardConfig(TEST_YARD_ID, TEST_SOLR_CORE_NAME);
    config.setAllowInitialisation(false);
    //the dbpedia default data
    config.setIndexConfigurationName(TEST_SOLR_CORE_CONFIGURATION);
    //init from datafile provider
    config.setAllowInitialisation(true);
    config.setName("DBpedia.org default data");
    config.setDescription("Data used for FstLinkingEngie tests");
    // create the Yard used for the tests
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    SolrServer server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrIndexRef, config.getIndexConfigurationName());
    Assert.assertNotNull("Unable to initialise SolrServer for testing", server);
    core = ((EmbeddedSolrServer) server).getCoreContainer().getCore(solrIndexRef.getIndex());
    Assert.assertNotNull("Unable to get SolrCore '" + config.getIndexConfigurationName() + "' from SolrServer " + server, core);
    yard = new SolrYard(server, config, null);
    //setup the index configuration
    LanguageConfiguration langConf = new LanguageConfiguration("not.used", new String[] { "en;field=dbpedia-ont:surfaceForm;generate=true" });
    fstConfig = new IndexConfiguration(langConf, core, FieldEncodingEnum.SolrYard, "");
    fstConfig.setExecutorService(Executors.newFixedThreadPool(1));
    fstConfig.setTypeField("rdf:type");
    fstConfig.setRankingField("entityhub:entityRank");
    //fstConfig.setEntityCacheManager(new FastLRUCacheManager(2048));
    fstConfig.setOrigin(new PlainLiteralImpl(TEST_ORIGIN));
    //activate the FST config
    //activate this configuration
    fstConfig.activate();
    //validate that the index contains the expected entities
    validateTestIndex();
    //now create the FST models
    List<Future<?>> creationTasks = new ArrayList<Future<?>>();
    for (CorpusInfo corpus : fstConfig.getCorpora()) {
        Assert.assertTrue("Failure in UnitTest - all FST models need to be generate=true", corpus.allowCreation);
        if (!corpus.isFstFile()) {
            //create a task on the FST corpus creation service
            creationTasks.add(fstConfig.getExecutorService().submit(new CorpusCreationTask(fstConfig, corpus)));
        }
    }
    //typical hardware
    for (Future<?> future : creationTasks) {
        try {
            future.get(FST_CREATION_WAIT_TIME, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
        // we assert on future.isDone instead
        }
        Assert.assertTrue("FST Model creation not finished after " + FST_CREATION_WAIT_TIME + "seconds", future.isDone());
    }
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) ArrayList(java.util.ArrayList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) SolrYardConfig(org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig) IndexConfiguration(org.apache.stanbol.enhancer.engines.lucenefstlinking.IndexConfiguration) CorpusInfo(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusInfo) Future(java.util.concurrent.Future) CorpusCreationTask(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusCreationTask) LanguageConfiguration(org.apache.stanbol.enhancer.nlp.utils.LanguageConfiguration) IndexReference(org.apache.stanbol.commons.solr.IndexReference) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) TimeoutException(java.util.concurrent.TimeoutException) BeforeClass(org.junit.BeforeClass)

Example 9 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project Xponents by OpenSextant.

the class SolrProxy method setupCore.

/**
     * Creates an EmbeddedSolrServer given solr home &amp; the core to use.
     * These may be null and you get the default.
     *
     * @param _solrHome  solr home
     * @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);
        }
        // since Solr 4.4
        solrContainer.load();
        return new EmbeddedSolrServer(solrContainer, _coreName);
    } catch (Exception err) {
        throw new ConfigException("Failed to set up Embedded Solr at " + _solrHome + " CORE:" + _coreName, err);
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) ConfigException(org.opensextant.ConfigException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) MalformedURLException(java.net.MalformedURLException) ConfigException(org.opensextant.ConfigException) IOException(java.io.IOException) SolrServerException(org.apache.solr.client.solrj.SolrServerException)

Example 10 with EmbeddedSolrServer

use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project jackrabbit-oak by apache.

the class FullTextSolrSearchTest method createEmbeddedSolrServerProvider.

private EmbeddedSolrServerProvider createEmbeddedSolrServerProvider(boolean http) throws Exception {
    String tempDirectoryPath = FileUtils.getTempDirectoryPath();
    File solrHome = new File(tempDirectoryPath, "solr" + System.nanoTime());
    EmbeddedSolrServerConfiguration embeddedSolrServerConfiguration = new EmbeddedSolrServerConfiguration(solrHome.getAbsolutePath(), "oak");
    if (http) {
        embeddedSolrServerConfiguration = embeddedSolrServerConfiguration.withHttpConfiguration("/solr", 8983);
    }
    EmbeddedSolrServerProvider embeddedSolrServerProvider = embeddedSolrServerConfiguration.getProvider();
    SolrServer solrServer = embeddedSolrServerProvider.getSolrServer();
    if (storageEnabled != null && !storageEnabled) {
        // change schema.xml and reload the core
        File schemaXML = new File(solrHome.getAbsolutePath() + "/oak/conf", "schema.xml");
        InputStream inputStream = getClass().getResourceAsStream("/solr/oak/conf/schema.xml");
        String schemaString = IOUtils.toString(inputStream).replace("<dynamicField name=\"*\" type=\"text_general\" indexed=\"true\" stored=\"true\" multiValued=\"true\"/>", "<dynamicField name=\"*\" type=\"text_general\" indexed=\"true\" stored=\"false\" multiValued=\"true\"/>");
        FileOutputStream fileOutputStream = new FileOutputStream(schemaXML);
        IOUtils.copy(new StringReader(schemaString), fileOutputStream);
        fileOutputStream.flush();
        ((EmbeddedSolrServer) solrServer).getCoreContainer().reload("oak");
    }
    return embeddedSolrServerProvider;
}
Also used : EmbeddedSolrServerConfiguration(org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration) EmbeddedSolrServerProvider(org.apache.jackrabbit.oak.plugins.index.solr.server.EmbeddedSolrServerProvider) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) StringReader(java.io.StringReader) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer)

Aggregations

EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)17 File (java.io.File)5 IOException (java.io.IOException)5 CoreContainer (org.apache.solr.core.CoreContainer)5 CoreDescriptor (org.apache.solr.core.CoreDescriptor)5 SolrCore (org.apache.solr.core.SolrCore)5 Test (org.junit.Test)4 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)3 IndexSchema (org.apache.solr.schema.IndexSchema)3 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 EmbeddedSolrServerConfiguration (org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration)2 SolrClient (org.apache.solr.client.solrj.SolrClient)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServer (org.apache.solr.client.solrj.SolrServer)2 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)2