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:com.temenos.interaction.commands.solr.QueryCommandITCase.java

License:Open Source License

@Before
public void setUp() throws Exception {
    System.setProperty("solr.solr.home", getSolrHome());
    SolrConfig solrConfig = TestHarness.createConfig(getSolrHome(), "universalsearch", getSolrConfigFile());
    TestHarness h = new TestHarness(new File("./target/solr-test/data").getAbsolutePath(), solrConfig,
            getSchemaFile());//ww w .  jav a  2s. co m
    server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName());

    initialiseTestData();
}

From source file:com.tripod.solr.util.EmbeddedSolrServerFactory.java

License:Apache License

/**
 * @param solrHome the Solr home directory to use
 * @param configSetHome the directory containing config sets
 * @param coreName the name of the core, must have a matching directory in configHome
 * @param cleanSolrHome if true the directory for solrHome will be deleted and re-created if it already exists
 * @return an EmbeddedSolrServer with a core created for the given coreName
 *//*  w  ww.ja va 2  s.co  m*/
public static SolrClient create(final String solrHome, final String configSetHome, final String coreName,
        final boolean cleanSolrHome) throws IOException, SolrServerException {

    final File solrHomeDir = new File(solrHome);
    if (solrHomeDir.exists()) {
        FileUtils.deleteDirectory(solrHomeDir);
        solrHomeDir.mkdirs();
    } else {
        solrHomeDir.mkdirs();
    }

    final SolrResourceLoader loader = new SolrResourceLoader(solrHomeDir.toPath());
    final Path configSetPath = Paths.get(configSetHome).toAbsolutePath();

    final NodeConfig config = new NodeConfig.NodeConfigBuilder("embeddedSolrServerNode", loader)
            .setConfigSetBaseDirectory(configSetPath.toString()).build();

    final EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(config, coreName);

    final CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(coreName);
    createRequest.setConfigSet(coreName);
    embeddedSolrServer.request(createRequest);

    return embeddedSolrServer;
}

From source file:cz.brmlab.yodaqa.provider.solr.Solr.java

License:Apache License

private SolrServer createEmbeddedSolrServer(String core) throws Exception {
    System.setProperty("solr.solr.home", core);
    CoreContainer.Initializer initializer = new CoreContainer.Initializer();
    CoreContainer coreContainer = initializer.initialize();
    return new EmbeddedSolrServer(coreContainer, "");
}

From source file:ddf.catalog.source.solr.SolrServerFactory.java

License:Open Source License

/**
 * Provides an already instantiated {@link SolrServer} object. If an instance has not already
 * been instantiated, then the single instance will be instantiated with the provided
 * configuration file. If an instance already exists, it cannot be overwritten with a new
 * configuration./*from   w  ww. j a va  2 s  .co m*/
 * 
 * @param solrConfigXml
 *            the name of the solr configuration filename such as solrconfig.xml
 * @param schemaXml
 *            filename of the schema such as schema.xml
 * @param givenConfigFileProxy
 *            a ConfigurationFileProxy instance. If instance is <code>null</code>, a new
 *            {@link ConfigurationFileProxy} is used instead.
 * @return {@link SolrServer} instance
 */
public static EmbeddedSolrServer getEmbeddedSolrServer(String solrConfigXml, String schemaXml,
        ConfigurationFileProxy givenConfigFileProxy) {

    LOGGER.info("Retrieving embedded solr with the following properties: [" + solrConfigXml + "," + schemaXml
            + "," + givenConfigFileProxy + "]");

    String solrConfigFileName = DEFAULT_SOLRCONFIG_XML;

    String schemaFileName = DEFAULT_SCHEMA_XML;

    if (isNotBlank(solrConfigXml)) {
        solrConfigFileName = solrConfigXml;
    }

    if (isNotBlank(schemaXml)) {
        schemaFileName = schemaXml;
    }

    File solrConfigFile = null;
    File solrConfigHome = null;
    File solrSchemaFile = null;

    ConfigurationFileProxy configProxy = givenConfigFileProxy;

    if (givenConfigFileProxy == null) {
        configProxy = new ConfigurationFileProxy(null, ConfigurationStore.getInstance());
    }

    File configurationDir = new File(ConfigurationFileProxy.DEFAULT_SOLR_CONFIG_PARENT_DIR,
            ConfigurationFileProxy.SOLR_CONFIG_LOCATION_IN_BUNDLE);
    configProxy.writeBundleFilesTo(configurationDir);

    try {
        URL url = configProxy.getResource(solrConfigFileName);

        LOGGER.info("Solr config url: " + url);

        solrConfigFile = new File(new URI(url.toString()).getPath());

        solrConfigHome = new File(solrConfigFile.getParent());
    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    try {
        URL url = configProxy.getResource(schemaFileName);

        LOGGER.info("Solr schema url: " + url);

        solrSchemaFile = new File(new URI(url.toString()).getPath());

    } catch (URISyntaxException e) {
        LOGGER.warn(e);
    }

    SolrConfig solrConfig = null;
    IndexSchema indexSchema = null;
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(SolrServerFactory.class.getClassLoader());

        // NamedSPILoader uses the thread context classloader to lookup
        // codecs, posting formats, and analyzers
        solrConfig = new SolrConfig(solrConfigHome.getParent(), solrConfigFileName,
                new InputSource(FileUtils.openInputStream(solrConfigFile)));
        indexSchema = new IndexSchema(solrConfig, schemaFileName,
                new InputSource(FileUtils.openInputStream(solrSchemaFile)));
    } catch (ParserConfigurationException e) {
        LOGGER.warn(e);
    } catch (IOException e) {
        LOGGER.warn(e);
    } catch (SAXException e) {
        LOGGER.warn(e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    CoreContainer container = new CoreContainer(solrConfigHome.getAbsolutePath());
    container.load();
    CoreDescriptor dcore = new CoreDescriptor(container, "core1",
            solrConfig.getResourceLoader().getInstanceDir());

    File dataDir = configProxy.getDataDirectory();
    LOGGER.info("Using data directory [" + dataDir + "]");
    SolrCore core = new SolrCore("core1", dataDir.getAbsolutePath(), solrConfig, indexSchema, dcore);
    container.register("core1", core, false);

    return new EmbeddedSolrServer(container, "core1");
}

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

License:Apache License

/**
 * Starts the Solr server before all tests run.
 * // w ww  .  ja  va  2 s .c  o 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:de.unidue.inf.is.ezdl.dlwrapper.wrappers.cs.BibDBSolrWrapperTest.java

License:Open Source License

@Before
public void init() {
    System.setProperty("solr.solr.home", "");
    CoreContainer.Initializer initializer = new CoreContainer.Initializer();
    CoreContainer coreContainer;/*from  w  w w  . j a va 2s.  c o  m*/
    SolrServer solrServer;
    try {
        coreContainer = initializer.initialize();
        solrServer = new EmbeddedSolrServer(coreContainer, "c");
        bibDBSolrWrapper = new BibDBSolrWrapper(solrServer);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}

From source file:dev.Run.java

License:Apache License

public static void main(String[] args) throws Exception {
    PATH_SOLR_INDEX.mkdirs();// w  ww .  j av a2s.c  o  m

    System.setProperty("solr.solr.home", "solr/sarq");
    CoreContainer.Initializer initializer = new CoreContainer.Initializer();
    CoreContainer coreContainer = initializer.initialize();
    EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

    // Load data into TDB
    Dataset dataset = TDBFactory.assembleDataset(TDB_ASSEMBLER_FILENAME);
    Model model = dataset.getDefaultModel();
    model.add(RDF.first, RDF.first, "text");
    model.add(ResourceFactory.createResource(), RDFS.label, "foo");
    model.add(ResourceFactory.createResource("foo"), FOAF.knows, ResourceFactory.createResource("bar"));
    model.add(ResourceFactory.createResource(), ResourceFactory.createProperty(":p1"), "text");
    model.add(ResourceFactory.createResource(), ResourceFactory.createProperty(":p2"), "ciao");

    // Build Solr index
    IndexBuilderModel builder = new IndexBuilderString(server);
    // IndexBuilderModel builder = new IndexBuilderString("http://127.0.0.1:8983/solr/sarq");
    // IndexBuilderModel builder = new IndexBuilderSubject("http://127.0.0.1:8983/solr");

    builder.indexStatements(model.listStatements());
    builder.commit();

    SARQ.setDefaultIndex(builder.getSolrServer());

    // Perform a query
    Query q = QueryFactory.create("PREFIX sarq:     <http://openjena.org/SARQ/property#>" + ""
            + "select * where {" + "?doc ?p ?lit ." + "(?lit ?score ) sarq:search '+text' ." + "}");

    Op op = Algebra.compile(q);
    op = Algebra.optimize(op);
    System.out.println(op);

    QueryExecution qe = QueryExecutionFactory.create(q, dataset);
    ResultSet res = qe.execSelect();
    ResultSetFormatter.out(res);
    qe.close();

    server = null;
    System.exit(0);
}

From source file:edu.cornell.mannlib.vitro.utilities.solrtest.SolrConfigTester.java

License:Open Source License

@Before
public void setup() throws Exception {
    String solrHomeString = System.getProperty("test.solr.home");
    container = new CoreContainer(solrHomeString);

    try (LogLeveler l = new LogLeveler(SolrCore.class, Level.ERROR)) {
        container.load();/*from w w  w . ja  v  a 2s.c  om*/
    }

    server = new EmbeddedSolrServer(container, "collection1");
    server.deleteByQuery("*:*");
    server.commit();
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.BaseEmbeddedSolrTest.java

License:Apache License

@Before
public void setUp() throws Exception {

    File home = new File("src/test/resources/config");
    File configFile = new File(home, "solr.xml");

    System.setProperty("solr.data.dir", "src/test/resources/config/data/");
    container = new CoreContainer("src/test/resources/config", configFile);

    server = new EmbeddedSolrServer(container, "access-master");

    driver = new SolrUpdateDriver();
    driver.setSolrServer(server);//from www  .  ja  v a  2 s  .c om
    driver.setUpdateSolrServer(server);
}

From source file:edu.unc.lib.dl.ui.service.AbstractSolrQueryLayerTest.java

License:Apache License

@Before
public void setUp() throws Exception {
    initMocks(this);

    File home = new File("src/test/resources/config");
    File configFile = new File(home, "solr.xml");

    File dataDir = tmpFolder.newFolder("solrdata");
    System.setProperty("solr.data.dir", dataDir.getAbsolutePath());
    container = new CoreContainer("src/test/resources/config", configFile);

    server = new EmbeddedSolrServer(container, "access-master");

    searchSettings = new SearchSettings();
    Properties properties = new Properties();
    properties.load(new FileInputStream(new File("src/test/resources/search.properties")));
    searchSettings.setProperties(properties);

    solrSettings = new SolrSettings();
    Properties solrProperties = new Properties();
    solrProperties.load(new FileInputStream(new File("src/test/resources/solr.properties")));
    solrSettings.setProperties(solrProperties);

    stateFactory = new SearchStateFactory();
    stateFactory.setSearchSettings(searchSettings);

    facetUtil = new FacetFieldUtil();
    facetUtil.setSearchSettings(searchSettings);
    facetUtil.setSolrSettings(solrSettings);

    queryLayer = new SolrQueryLayerService();
    queryLayer.setCollectionsPid(new PID("cdr:Collections"));
    queryLayer.setSearchSettings(searchSettings);
    queryLayer.setSolrSettings(solrSettings);
    queryLayer.setSearchStateFactory(stateFactory);
    queryLayer.setFacetFieldUtil(facetUtil);
    setField(queryLayer, "server", server);
}