Example usage for org.apache.solr.core SolrResourceLoader SolrResourceLoader

List of usage examples for org.apache.solr.core SolrResourceLoader SolrResourceLoader

Introduction

In this page you can find the example usage for org.apache.solr.core SolrResourceLoader SolrResourceLoader.

Prototype

public SolrResourceLoader(Path instanceDir) 

Source Link

Usage

From source file:com.cloudera.cdk.morphline.solr.SolrLocator.java

License:Apache License

public IndexSchema getIndexSchema() {
    if (context instanceof SolrMorphlineContext) {
        IndexSchema schema = ((SolrMorphlineContext) context).getIndexSchema();
        if (schema != null) {
            validateSchema(schema);//from w  ww . j  a va2  s  .  c  om
            return schema;
        }
    }

    // If solrHomeDir isn't defined and zkHost and collectionName are defined 
    // then download schema.xml and solrconfig.xml, etc from zk and use that as solrHomeDir
    String mySolrHomeDir = solrHomeDir;
    if (solrHomeDir == null || solrHomeDir.length() == 0) {
        if (zkHost == null || zkHost.length() == 0) {
            // TODO: implement download from solrUrl if specified
            throw new MorphlineCompilationException(
                    "Downloading a Solr schema requires either parameter 'solrHomeDir' or parameters 'zkHost' and 'collection'",
                    config);
        }
        if (collectionName == null || collectionName.length() == 0) {
            throw new MorphlineCompilationException(
                    "Parameter 'zkHost' requires that you also pass parameter 'collection'", config);
        }
        ZooKeeperDownloader zki = new ZooKeeperDownloader();
        SolrZkClient zkClient = zki.getZkClient(zkHost);
        try {
            String configName = zki.readConfigName(zkClient, collectionName);
            File downloadedSolrHomeDir = zki.downloadConfigDir(zkClient, configName);
            mySolrHomeDir = downloadedSolrHomeDir.getAbsolutePath();
        } catch (KeeperException e) {
            throw new MorphlineCompilationException("Cannot download schema.xml from ZooKeeper", config, e);
        } catch (InterruptedException e) {
            throw new MorphlineCompilationException("Cannot download schema.xml from ZooKeeper", config, e);
        } catch (IOException e) {
            throw new MorphlineCompilationException("Cannot download schema.xml from ZooKeeper", config, e);
        } finally {
            zkClient.close();
        }
    }

    LOG.debug("SolrLocator loading IndexSchema from dir {}", mySolrHomeDir);
    try {
        SolrResourceLoader loader = new SolrResourceLoader(mySolrHomeDir);
        SolrConfig solrConfig = new SolrConfig(loader, "solrconfig.xml", null);
        InputSource is = new InputSource(loader.openSchema("schema.xml"));
        is.setSystemId(SystemIdResolver.createSystemIdFromResourceName("schema.xml"));

        IndexSchema schema = new IndexSchema(solrConfig, "schema.xml", is);
        validateSchema(schema);
        return schema;
    } catch (ParserConfigurationException e) {
        throw new MorphlineRuntimeException(e);
    } catch (IOException e) {
        throw new MorphlineRuntimeException(e);
    } catch (SAXException e) {
        throw new MorphlineRuntimeException(e);
    }
}

From source file:com.googlecode.solrgeonames.harvester.Harvester.java

License:Open Source License

/**
 * Start up an embedded Solr server.//from  w w  w  .ja v  a  2s .co m
 *
 * @param home: The path to the Solr home directory
 * @return EmbeddedSolrServer: The instantiated server
 * @throws Exception if any errors occur
 */
private EmbeddedSolrServer startSolr(String home) throws Exception {
    try {
        SolrConfig solrConfig = new SolrConfig(home, SOLR_CONFIG, null);
        IndexSchema schema = new IndexSchema(solrConfig, SOLR_SCHEMA, null);

        solrContainer = new CoreContainer(new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
        CoreDescriptor descriptor = new CoreDescriptor(solrContainer, "",
                solrConfig.getResourceLoader().getInstanceDir());
        descriptor.setConfigName(solrConfig.getResourceName());
        descriptor.setSchemaName(schema.getResourceName());

        solrCore = new SolrCore(null, solrConfig.getDataDir(), solrConfig, schema, descriptor);
        solrContainer.register("", solrCore, false);
        return new EmbeddedSolrServer(solrContainer, "");
    } catch (Exception ex) {
        log.error("\nFailed to start Solr server\n");
        throw ex;
    }
}

From source file:com.googlecode.solrgeonames.server.GeoContextListener.java

License:Open Source License

/**
 * Start up an embedded Solr server.// w  w w . java  2 s  .co m
 *
 * @param home: The path to the Solr home directory
 * @return EmbeddedSolrServer: The instantiated server
 * @throws Exception if any errors occur
 */
private EmbeddedSolrServer startSolr(String home) throws Exception {
    SolrConfig solrConfig = new SolrConfig(home, SOLR_CONFIG, null);
    IndexSchema schema = new IndexSchema(solrConfig, SOLR_SCHEMA, null);

    solrContainer = new CoreContainer(new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
    CoreDescriptor descriptor = new CoreDescriptor(solrContainer, "",
            solrConfig.getResourceLoader().getInstanceDir());
    descriptor.setConfigName(solrConfig.getResourceName());
    descriptor.setSchemaName(schema.getResourceName());

    SolrCore solrCore = new SolrCore(null, solrConfig.getDataDir(), solrConfig, schema, descriptor);
    solrContainer.register("", solrCore, false);
    return new EmbeddedSolrServer(solrContainer, "");
}

From source file:com.indoqa.solr.server.factory.EmbeddedSolrServerBuilder.java

License:Apache License

public static SolrServer build(String url, String embeddedSolrConfigurationDir) throws IOException {
    String solrHome = getNormalizedPath(embeddedSolrConfigurationDir);

    InputStream solrXmlInputStream = EmbeddedSolrServerBuilder.class
            .getResourceAsStream("/embedded-core-container/solr.xml");

    SolrResourceLoader loader = new SolrResourceLoader(solrHome);
    ConfigSolr configSolr = ConfigSolr.fromInputStream(loader, solrXmlInputStream);

    solrXmlInputStream.close();/*from   w  w w. ja v  a  2  s .com*/

    CoreContainer container = new CoreContainer(loader, configSolr);
    container.load();

    String dataDir = getNormalizedPath(getDataDir(url));
    CoreDescriptor coreDescriptor = new CoreDescriptor(container, "Embedded Core", solrHome, CORE_DATADIR,
            dataDir);
    SolrCore core = container.create(coreDescriptor);

    return new EmbeddedSolrServer(container, core.getName());
}

From source file:com.indoqa.solr.spring.client.EmbeddedSolrServerBuilder.java

License:Apache License

public static SolrClient build(String url, String embeddedSolrConfigurationPath) {
    if (new File(embeddedSolrConfigurationPath).exists()) {
        deleteOldCoreProperties(embeddedSolrConfigurationPath);

        SolrResourceLoader loader = new SolrResourceLoader(getNormalizedPath(embeddedSolrConfigurationPath));
        NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
        CoreContainer container = new CoreContainer(nodeConfig);
        container.load();/*  w w w . java 2 s  . c o m*/

        Map<String, String> properties = new HashMap<String, String>();
        properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());

        SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
        return new EmbeddedSolrServer(core);
    }

    // use a temporary directory for the resource loader because Solr needs a real directory for this
    SolrResourceLoader loader = new SolrResourceLoader(createTempDirectory());
    NodeConfig nodeConfig = new NodeConfigBuilder(null, loader).build();
    CoreContainer container = new CoreContainer(nodeConfig);
    container.load();

    Map<String, String> properties = new HashMap<String, String>();
    properties.put(CoreDescriptor.CORE_DATADIR, getNormalizedPath(getDataDir(url)).toString());
    properties.put(CoreDescriptor.CORE_CONFIG, embeddedSolrConfigurationPath + "/conf/solrconfig.xml");
    properties.put(CoreDescriptor.CORE_SCHEMA, embeddedSolrConfigurationPath + "/conf/schema.xml");

    SolrCore core = container.create(CORE_NAME, loader.getInstancePath(), properties);
    return new EmbeddedSolrServer(core);

}

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
 *///ww w.  ja  va 2  s.  c  o 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:jp.aegif.nemaki.util.impl.PropertyManagerImpl.java

License:Open Source License

/**
 * Constructor setting specified properties file and config object
 * @param propertiesFile/*from w  w  w. j  a  v  a2  s.c o m*/
 */
public PropertyManagerImpl(String propertiesFile) {
    this.setPropertiesFile(propertiesFile);

    Properties config = new Properties();
    SolrResourceLoader loader = new SolrResourceLoader(null);
    try {
        //Set key values
        InputStream inputStream = loader.openResource(propertiesFile);
        if (inputStream != null) {
            config.load(inputStream);
            this.setConfig(config);
        }

        //Set override files
        String _overrideFiles = config.getProperty(PropertyKey.OVERRIDE_FILES);
        if (StringUtils.isNotBlank(_overrideFiles)) {
            overrideFiles = split(_overrideFiles);
        }
    } catch (Exception e) {
        logger.error("Error occurred during setting of PropertyManager.", e);
    } finally {
        try {
            loader.close();
        } catch (Exception e) {
            logger.error("Error occurred during closing SolrResourceLoader.", e);
        }
    }
}

From source file:jp.aegif.nemaki.util.impl.PropertyManagerImpl.java

License:Open Source License

/**
 * Override is not supported for update//from  w  w  w. j  av a 2  s .  c om
 */
@Override
public void modifyValue(String key, String value) {
    config.setProperty(key, value);

    SolrResourceLoader loader = new SolrResourceLoader(null);
    ClassLoader classLoader = loader.getClassLoader();
    URL url = classLoader.getResource(propertiesFile);

    try {
        if (url == null) {
            config.store(new FileOutputStream(new File(loader.locateSolrHome() + "/conf/" + propertiesFile)),
                    null);
        } else {
            config.store(new FileOutputStream(new File(url.toURI())), null);
        }
    } catch (Exception e) {
        logger.error("Error occurred during modification of porperty value.", e);
    }

}

From source file:jp.sf.fess.solr.plugin.suggest.SuggestUpdateControllerTest.java

License:Apache License

public void test_updateAndSuggest() {
    try {/*from www .  j a  v  a2s .  com*/
        final SuggestSolrServer suggestSolrServer = TestUtils.createSuggestSolrServer();
        suggestSolrServer.deleteAll();
        final SuggestUpdateConfig config = TestUtils.getSuggestUpdateConfig();
        final SuggestUpdateController controller = new SuggestUpdateController(config,
                getSuggestFieldInfoList(config, false),
                new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
        controller.start();

        final SolrInputDocument doc = new SolrInputDocument();
        doc.setField("content",
                "Fess ?5 ?????????Java ????? OS ????Fess ? Apache ????? () ????????\n"
                        + "\n"
                        + "Seasar2 ????? 2 ??? Solr ????? ?? S2Robot ?????Web ???????MS Office ? zip ?????????????");
        doc.setField(config.getExpiresField(), DateUtil.getThreadLocalDateFormat().format(new Date()));
        controller.add(doc);
        controller.commit();
        Thread.sleep(5 * 1000);

        //assert
        assertTrue(suggestSolrServer.select("*:*").getNumFound() > 10);
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":jav*")
                .getNumFound() > 0);
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":kensakuenjinn*")
                .getNumFound() > 0);
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":inde*")
                .getNumFound() > 0);

        //suggest check
        final Suggester suggester = new Suggester();
        suggester.setNormalizer(TestUtils.createNormalizer());
        suggester.setConverter(TestUtils.createConverter());

        String q = suggester.buildSuggestQuery("jav", Arrays.asList(new String[] { "content" }), null, null);
        assertTrue(suggestSolrServer.select(q).getNumFound() > 0);
        q = suggester.buildSuggestQuery("kensakuenj", Arrays.asList(new String[] { "content" }), null, null);
        assertTrue(suggestSolrServer.select(q).getNumFound() > 0);
        q = suggester.buildSuggestQuery("inde", Arrays.asList(new String[] { "content" }), null, null);
        assertTrue(suggestSolrServer.select(q).getNumFound() > 0);

    } catch (final Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:jp.sf.fess.solr.plugin.suggest.SuggestUpdateControllerTest.java

License:Apache License

public void test_update_multifield() {
    final SuggestSolrServer suggestSolrServer = TestUtils.createSuggestSolrServer();

    try {// w w w .  ja va 2  s.  c o  m
        suggestSolrServer.deleteAll();

        final SuggestUpdateConfig config = TestUtils.getSuggestUpdateConfig();
        final SuggestUpdateController controller = new SuggestUpdateController(config,
                getSuggestFieldInfoList(config, true),
                new SolrResourceLoader(SolrResourceLoader.locateSolrHome()));
        controller.start();
        final SolrInputDocument doc = new SolrInputDocument();
        doc.setField("content",
                "Fess ?5 ?????????Java ????? OS ????Fess ? Apache ????? () ????????\n"
                        + "\n"
                        + "Seasar2 ????? 2 ??? Solr ????? ?? S2Robot ?????Web ???????MS Office ? zip ?????????????");
        doc.setField("title", "Fess?????page");
        doc.setField(config.getExpiresField(), DateUtil.getThreadLocalDateFormat().format(new Date()));
        controller.add(doc);
        controller.commit();
        Thread.sleep(5 * 1000);

        //assert
        assertTrue(suggestSolrServer.select("*:*").getNumFound() > 10);
        assertEquals(1,
                suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":jav*").getNumFound());
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":kensakuenjinn*")
                .getNumFound() > 0);
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":inde*")
                .getNumFound() > 0);
        assertTrue(suggestSolrServer.select(SuggestConstants.SuggestFieldNames.READING + ":fessnituitenose*")
                .getNumFound() > 0);

        //suggester check
        final Suggester suggester = new Suggester();
        suggester.setNormalizer(TestUtils.createNormalizer());
        suggester.setConverter(TestUtils.createConverter());

        String q = suggester.buildSuggestQuery("fessnituitenosetumei", Arrays.asList(new String[] { "title" }),
                null, null);
        assertEquals(1, suggestSolrServer.select(q).getNumFound());
        q = suggester.buildSuggestQuery("fessnituitenosetumei", Arrays.asList(new String[] { "content" }), null,
                null);
        assertEquals(0, suggestSolrServer.select(q).getNumFound());
    } catch (final Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}