List of usage examples for org.apache.solr.client.solrj.embedded EmbeddedSolrServer EmbeddedSolrServer
public EmbeddedSolrServer(CoreContainer coreContainer, String coreName)
From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java
License:Open Source License
/** * This constructor should be used to set up embedded solr given a String to a SolrHome directory to use, or if * 'solrhome' is passed in as a parameter we will use the java temp directory to setup solr * * @param solrServer/*w ww .j a v a 2 s . co m*/ * @throws IOException * @throws ParserConfigurationException * @throws SAXException * @throws IllegalStateException */ public SolrConfiguration(String solrServer) throws IOException, ParserConfigurationException, SAXException, IllegalStateException { // using embedded solr so we will default the core names this.setPrimaryName("primary"); this.setReindexName("reindex"); if (Objects.equals("solrhome", solrServer)) { final String baseTempPath = System.getProperty("java.io.tmpdir"); File tempDir = new File(baseTempPath + File.separator + System.getProperty("user.name") + File.separator + "solrhome-5.3.1"); if (System.getProperty("tmpdir.solrhome") != null) { //allow for an override of tmpdir tempDir = new File(System.getProperty("tmpdir.solrhome")); } if (!tempDir.exists()) { tempDir.mkdirs(); } solrServer = tempDir.getAbsolutePath(); } setSolrHomePath(solrServer); File solrXml = new File(new File(solrServer), "solr.xml"); if (!solrXml.exists()) { copyConfigToSolrHome(this.getClass().getResourceAsStream("/solr-default.xml"), solrXml); } buildSolrCoreDirectories(solrServer); LOG.debug(String.format("Using [%s] as solrhome", solrServer)); LOG.debug(String.format("Using [%s] as solr.xml", solrXml.getAbsoluteFile())); if (LOG.isTraceEnabled()) { LOG.trace("Contents of solr.xml:"); BufferedReader br = null; try { br = new BufferedReader(new FileReader(solrXml)); String line; while ((line = br.readLine()) != null) { LOG.trace(line); } } finally { if (br != null) { try { br.close(); } catch (Throwable e) { //do nothing } } } LOG.trace("Done printing solr.xml"); } CoreContainer coreContainer = CoreContainer.createAndLoad(solrServer, solrXml); EmbeddedSolrServer primaryServer = new EmbeddedSolrServer(coreContainer, getPrimaryName()); EmbeddedSolrServer reindexServer = new EmbeddedSolrServer(coreContainer, getReindexName()); this.setServer(primaryServer); this.setReindexServer(reindexServer); //NOTE: There is no reason to set the admin server here as the we will return the primary server //if the admin server is not set... }
From source file:org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl.java
License:Apache License
public SolrSearchServiceImpl(String solrServer) throws IOException, ParserConfigurationException, SAXException { if ("solrhome".equals(solrServer)) { final String baseTempPath = System.getProperty("java.io.tmpdir"); File tempDir = new File(baseTempPath + File.separator + System.getProperty("user.name") + File.separator + "solrhome-4.10.3"); if (System.getProperty("tmpdir.solrhome") != null) { //allow for an override of tmpdir tempDir = new File(System.getProperty("tmpdir.solrhome")); }/*from w w w . ja va 2s . c o m*/ if (!tempDir.exists()) { tempDir.mkdirs(); } solrServer = tempDir.getAbsolutePath(); } solrHomePath = solrServer; File solrXml = new File(new File(solrServer), "solr.xml"); if (!solrXml.exists()) { copyConfigToSolrHome(this.getClass().getResourceAsStream("/solr-default.xml"), solrXml); } buildSolrCoreDirectories(solrServer); LOG.debug(String.format("Using [%s] as solrhome", solrServer)); LOG.debug(String.format("Using [%s] as solr.xml", solrXml.getAbsoluteFile())); if (LOG.isTraceEnabled()) { LOG.trace("Contents of solr.xml:"); BufferedReader br = null; try { br = new BufferedReader(new FileReader(solrXml)); String line; while ((line = br.readLine()) != null) { LOG.trace(line); } } finally { if (br != null) { try { br.close(); } catch (Throwable e) { //do nothing } } } LOG.trace("Done printing solr.xml"); } CoreContainer coreContainer = CoreContainer.createAndLoad(solrServer, solrXml); EmbeddedSolrServer primaryServer = new EmbeddedSolrServer(coreContainer, SolrContext.PRIMARY); EmbeddedSolrServer reindexServer = new EmbeddedSolrServer(coreContainer, SolrContext.REINDEX); SolrContext.setPrimaryServer(primaryServer); SolrContext.setReindexServer(reindexServer); //NOTE: There is no reason to set the admin server here as the SolrContext will return the primary server //if the admin server is not set... }
From source file:org.codice.ddf.ui.searchui.query.solr.FilteringSolrIndex.java
License:Open Source License
private static EmbeddedSolrServer createSolrServer(String coreName, ConfigurationFileProxy configProxy) { File configFile = getConfigFile(IMMEMORY_SOLRCONFIG_XML, configProxy, coreName); if (configFile == null) { throw new IllegalArgumentException("Unable to find Solr configuration file"); }/* ww w . j a v a 2 s . c o m*/ File schemaFile = getConfigFile(DEFAULT_SCHEMA_XML, configProxy, coreName); if (schemaFile == null) { throw new IllegalArgumentException("Unable to find Solr schema file"); } File solrConfigHome = new File(configFile.getParent()); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(EmbeddedSolrFactory.class.getClassLoader()); SolrConfig solrConfig = new SolrConfig(Paths.get(solrConfigHome.getParent()), IMMEMORY_SOLRCONFIG_XML, new InputSource(FileUtils.openInputStream(configFile))); if (indexSchema == null) { indexSchema = new IndexSchema(solrConfig, DEFAULT_SCHEMA_XML, new InputSource(FileUtils.openInputStream(schemaFile))); } SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrConfigHome.getAbsolutePath())); SolrCoreContainer container = new SolrCoreContainer(loader); CoreDescriptor coreDescriptor = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstancePath()); SolrCore core = new SolrCore(coreName, null, solrConfig, indexSchema, null, coreDescriptor, null, null, null); container.register(coreName, core, false); return new EmbeddedSolrServer(container, coreName); } catch (ParserConfigurationException | SAXException | IOException e) { throw new IllegalArgumentException("Unable to parse Solr configuration file", e); } finally { Thread.currentThread().setContextClassLoader(tccl); } }
From source file:org.codice.solr.factory.EmbeddedSolrFactory.java
License:Open Source License
/** * Provides an already instantiated {@link org.apache.solr.client.solrj.SolrClient} 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 2s . 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 org.apache.solr.client.solrj.SolrClient} instance */ public static EmbeddedSolrServer getEmbeddedSolrServer(String solrConfigXml, String schemaXml, ConfigurationFileProxy givenConfigFileProxy) { LOGGER.debug("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; } ConfigurationFileProxy configProxy = givenConfigFileProxy; if (givenConfigFileProxy == null) { configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance()); } File solrConfigFile = getConfigFile(solrConfigFileName, configProxy); File solrSchemaFile = getConfigFile(schemaFileName, configProxy); File solrConfigHome = new File(solrConfigFile.getParent()); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(EmbeddedSolrFactory.class.getClassLoader()); // NamedSPILoader uses the thread context classloader to lookup // codecs, posting formats, and analyzers SolrConfig solrConfig = new SolrConfig(Paths.get(solrConfigHome.getParent()), solrConfigFileName, new InputSource(FileUtils.openInputStream(solrConfigFile))); IndexSchema indexSchema = new IndexSchema(solrConfig, schemaFileName, new InputSource(FileUtils.openInputStream(solrSchemaFile))); SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrConfigHome.getAbsolutePath())); SolrCoreContainer container = new SolrCoreContainer(loader); String dataDirPath = null; if (!ConfigurationStore.getInstance().isInMemory()) { File dataDir = configProxy.getDataDirectory(); if (dataDir != null) { LOGGER.debug("Using data directory [{}]", dataDir); dataDirPath = dataDir.getAbsolutePath(); } } else { PluginInfo info = solrConfig.getPluginInfo(DirectoryFactory.class.getName()); if (!"solr.RAMDirectoryFactory".equals(info.className)) { LOGGER.warn("Using in-memory configuration without RAMDirectoryFactory."); } } CoreDescriptor coreDescriptor = new CoreDescriptor(container, DEFAULT_EMBEDDED_CORE_NAME, solrConfig.getResourceLoader().getInstancePath()); SolrCore core = new SolrCore(DEFAULT_EMBEDDED_CORE_NAME, dataDirPath, solrConfig, indexSchema, null, coreDescriptor, null, null, null); container.register(DEFAULT_EMBEDDED_CORE_NAME, core, false); return new EmbeddedSolrServer(container, DEFAULT_EMBEDDED_CORE_NAME); } catch (ParserConfigurationException | IOException | SAXException e) { throw new IllegalArgumentException("Unable to parse Solr configuration file: " + solrConfigFileName, e); } finally { Thread.currentThread().setContextClassLoader(tccl); } }
From source file:org.codice.solr.factory.impl.EmbeddedSolrFactory.java
License:Open Source License
/** * Creates a new {@link EmbeddedSolrServer} using the Solr core and configuration file names, * schema and configuration file proxy provided. * * @param coreName name of the Solr core * @param solrConfigXml name of the Solr configuration file. Defaults to * {@value HttpSolrClientFactory#DEFAULT_SOLRCONFIG_XML} if * {@code null}. * @param schemaXml file name of the Solr core schema. Defaults to * {@value HttpSolrClientFactory#DEFAULT_SCHEMA_XML} if * {@code null}. * @param givenConfigFileProxy {@link ConfigurationFileProxy} instance to use. If {@code null}, * a new {@link ConfigurationFileProxy} will be used. * @return a new {@link EmbeddedSolrServer} instance *//*from w w w.ja va 2s. co m*/ public static EmbeddedSolrServer getEmbeddedSolrServer(String coreName, @Nullable String solrConfigXml, @Nullable String schemaXml, @Nullable ConfigurationFileProxy givenConfigFileProxy) { LOGGER.debug("Retrieving embedded solr with the following properties: [{},{},{}]", solrConfigXml, schemaXml, givenConfigFileProxy); String solrConfigFileName = HttpSolrClientFactory.DEFAULT_SOLRCONFIG_XML; String schemaFileName = HttpSolrClientFactory.DEFAULT_SCHEMA_XML; if (isNotBlank(solrConfigXml)) { solrConfigFileName = solrConfigXml; } if (isNotBlank(schemaXml)) { schemaFileName = schemaXml; } ConfigurationFileProxy configProxy = givenConfigFileProxy; if (givenConfigFileProxy == null) { configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance()); } configProxy.writeSolrConfiguration(coreName); File solrConfigFile = getConfigFile(solrConfigFileName, configProxy, coreName); File solrSchemaFile = getConfigFile(schemaFileName, configProxy, coreName); if (solrSchemaFile == null) { solrSchemaFile = getConfigFile("managed-schema", configProxy, coreName); if (solrSchemaFile == null) { throw new IllegalArgumentException("Unable to find Solr schema file."); } } File solrConfigHome = new File(solrConfigFile.getParent()); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(EmbeddedSolrFactory.class.getClassLoader()); // NamedSPILoader uses the thread context classloader to lookup // codecs, posting formats, and analyzers SolrConfig solrConfig = new SolrConfig(Paths.get(solrConfigHome.getParent()), solrConfigFileName, new InputSource(FileUtils.openInputStream(solrConfigFile))); IndexSchema indexSchema = new IndexSchema(solrConfig, schemaFileName, new InputSource(FileUtils.openInputStream(solrSchemaFile))); SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrConfigHome.getAbsolutePath())); SolrCoreContainer container = new SolrCoreContainer(loader); String dataDirPath = null; if (!ConfigurationStore.getInstance().isInMemory()) { File dataDir = configProxy.getDataDirectory(); if (dataDir != null) { dataDirPath = Paths.get(dataDir.getAbsolutePath(), coreName, "data").toString(); LOGGER.debug("Using data directory [{}]", dataDirPath); } } else { PluginInfo info = solrConfig.getPluginInfo(DirectoryFactory.class.getName()); if (info != null && !"solr.RAMDirectoryFactory".equals(info.className)) { LOGGER.debug("Using in-memory configuration without RAMDirectoryFactory."); } } CoreDescriptor coreDescriptor = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstancePath()); SolrCore core = new SolrCore(coreName, dataDirPath, solrConfig, indexSchema, null, coreDescriptor, null, null, null); container.register(coreName, core, false); return new EmbeddedSolrServer(container, coreName); } catch (ParserConfigurationException | IOException | SAXException e) { throw new IllegalArgumentException("Unable to parse Solr configuration file: " + solrConfigFileName, e); } finally { Thread.currentThread().setContextClassLoader(tccl); } }
From source file:org.codice.solr.factory.SolrServerFactory.java
License:Open Source License
/** * Provides an already instantiated {@link org.apache.solr.client.solrj.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.// w w w.ja va 2 s.c om * * @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 org.apache.solr.client.solrj.SolrServer} instance */ public static EmbeddedSolrServer getEmbeddedSolrServer(String solrConfigXml, String schemaXml, ConfigurationFileProxy givenConfigFileProxy) { LOGGER.debug("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; } ConfigurationFileProxy configProxy = givenConfigFileProxy; if (givenConfigFileProxy == null) { configProxy = new ConfigurationFileProxy(ConfigurationStore.getInstance()); } File solrConfigFile = getConfigFile(solrConfigFileName, configProxy); File solrSchemaFile = getConfigFile(schemaFileName, configProxy); File solrFile = getConfigFile(DEFAULT_SOLR_XML, configProxy); File solrConfigHome = new File(solrConfigFile.getParent()); 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 solrConfig = new SolrConfig(solrConfigHome.getParent(), solrConfigFileName, new InputSource(FileUtils.openInputStream(solrConfigFile))); IndexSchema indexSchema = new IndexSchema(solrConfig, schemaFileName, new InputSource(FileUtils.openInputStream(solrSchemaFile))); SolrResourceLoader loader = new SolrResourceLoader(solrConfigHome.getAbsolutePath()); SolrCoreContainer container = new SolrCoreContainer(loader, solrFile); String dataDirPath = null; if (!ConfigurationStore.getInstance().isInMemory()) { File dataDir = configProxy.getDataDirectory(); if (dataDir != null) { LOGGER.debug("Using data directory [{}]", dataDir); dataDirPath = dataDir.getAbsolutePath(); } } else { PluginInfo info = solrConfig.getPluginInfo(DirectoryFactory.class.getName()); if (!"solr.RAMDirectoryFactory".equals(info.className)) { LOGGER.warn("Using in-memory configuration without RAMDirectoryFactory."); } } CoreDescriptor coreDescriptor = new CoreDescriptor(container, DEFAULT_EMBEDDED_CORE_NAME, solrConfig.getResourceLoader().getInstanceDir()); SolrCore core = new SolrCore(DEFAULT_EMBEDDED_CORE_NAME, dataDirPath, solrConfig, indexSchema, coreDescriptor); container.register(DEFAULT_EMBEDDED_CORE_NAME, core, false); return new EmbeddedSolrServer(container, DEFAULT_EMBEDDED_CORE_NAME); } catch (ParserConfigurationException | IOException | SAXException e) { throw new IllegalArgumentException("Unable to parse Solr configuration file: " + solrConfigFileName, e); } finally { Thread.currentThread().setContextClassLoader(tccl); } }
From source file:org.codice.solr.query.SolrQueryFilterVisitorTest.java
License:Open Source License
@Test @Ignore//from w ww . jav a2 s . c o m public void test() throws Exception { LOGGER.info("Running test ..."); // setup String workingDir = System.getProperty("user.dir") + "/src/test/resources/"; String solrConfDir = workingDir + "solr/conf/"; File solrConfigFile = new File(solrConfDir + "solrconfig.xml"); //getConfigFile(solrConfigFileName, configProxy); assertTrue(solrConfigFile.exists()); File solrSchemaFile = new File(solrConfDir + "schema.xml"); //getConfigFile(schemaFileName, configProxy); assertTrue(solrSchemaFile.exists()); File solrFile = new File(solrConfDir + "solr.xml"); //getConfigFile(DEFAULT_SOLR_XML, configProxy); assertTrue(solrFile.exists()); File solrConfigHome = new File(solrConfigFile.getParent()); assertTrue(solrConfigHome.exists()); SolrConfig solrConfig = null; IndexSchema indexSchema = null; SolrResourceLoader resourceLoader = null; SolrCoreContainer container = null; try { // NamedSPILoader uses the thread context classloader to lookup // codecs, posting formats, and analyzers solrConfig = new SolrConfig(solrConfigHome.getParent(), "solrConfig.xml", new InputSource(FileUtils.openInputStream(solrConfigFile))); assertNotNull(solrConfig); indexSchema = new IndexSchema(solrConfig, "schema.xml", new InputSource(FileUtils.openInputStream(solrSchemaFile))); assertNotNull(indexSchema); resourceLoader = new SolrResourceLoader(solrConfigHome.getAbsolutePath()); assertNotNull(resourceLoader); container = new SolrCoreContainer(resourceLoader, solrFile); assertNotNull(container); CoreDescriptor coreDescriptor = new CoreDescriptor(container, CORE_NAME, solrConfig.getResourceLoader().getInstanceDir()); assertNotNull(coreDescriptor); File dataDir = new File(workingDir + "data"); //configProxy.getDataDirectory(); LOGGER.debug("Using data directory [{}]", dataDir); SolrCore core = new SolrCore(CORE_NAME, dataDir.getAbsolutePath(), solrConfig, indexSchema, coreDescriptor); container.register(CORE_NAME, core, false); assertNotNull(core); EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, CORE_NAME); // the test SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrServer, CORE_NAME); Filter filter = ECQL.toFilter("Name = 'Hugh'"); SolrQuery solrQuery = (SolrQuery) filter.accept(visitor, null); assertNotNull(solrQuery); // Solr does not support outside parenthesis in certain queries and throws EOF exception. String queryPhrase = solrQuery.getQuery().trim(); if (queryPhrase.matches("\\(\\s*\\{!.*\\)")) { solrQuery.setQuery(queryPhrase.replaceAll("^\\(\\s*|\\s*\\)$", "")); } LOGGER.info("solrQuery = {}", solrQuery); QueryResponse solrResponse = solrServer.query(solrQuery, METHOD.POST); assertNotNull(solrResponse); long numResults = solrResponse.getResults().getNumFound(); LOGGER.info("numResults = {}", numResults); } catch (ParserConfigurationException e) { LOGGER.warn("Parser configuration exception loading index schema", e); } catch (IOException e) { LOGGER.warn("IO exception loading index schema", e); } catch (SAXException e) { LOGGER.warn("SAX exception loading index schema", e); } }
From source file:org.dataconservancy.dcs.access.impl.solr.DcsSolrAccessService.java
License:Apache License
/** * Returns an AccessService attached to the SolrServer specified by the * system property solr.solr.home. If solr home is a full url, it connects * to the solr server running on that url. Otherwise solr home is treated as * a file path and an embedded solr server is started. * /*from w w w . j a v a2s . c o m*/ * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public DcsSolrAccessService() throws IOException, ParserConfigurationException, SAXException { String solrhome = System.getProperty("solr.solr.home"); if (solrhome == null) { throw new IOException("System property solr.solr.home must be set to a url or local path"); } boolean embedded = true; try { if (new URL(solrhome).getHost() != null) { embedded = false; } } catch (MalformedURLException e) { } SolrServer server; if (embedded) { container = new CoreContainer.Initializer().initialize(); server = new EmbeddedSolrServer(container, ""); } else { container = null; CommonsHttpSolrServer httpserv = new CommonsHttpSolrServer(solrhome); httpserv.setAllowCompression(true); server = httpserv; } this.index = new DcsSolrIndex(server); this.store = null; }
From source file:org.dataconservancy.dcs.access.impl.solr.DcsSolrAccessService.java
License:Apache License
public DcsSolrAccessService(File solrhome, ArchiveStore store) throws IOException, ParserConfigurationException, SAXException { System.setProperty("solr.solr.home", solrhome.getCanonicalPath()); this.container = new CoreContainer.Initializer().initialize(); this.index = new DcsSolrIndex(new EmbeddedSolrServer(container, "")); this.store = store; }
From source file:org.dataconservancy.dcs.access.maven.IndexArchiveMojo.java
License:Apache License
@Override public void execute() throws MojoExecutionException, MojoFailureException { final ElmArchiveStore archive = prepareArchive(getElmEntityDirectory(), getElmMetadataDirectory()); // <bean id="solrServer" class="org.apache.solr.client.solrj.embedded.EmbeddedSolrServer"> // <constructor-arg> // <bean class="org.apache.solr.core.CoreContainer"> // <constructor-arg value="${solr.solr.home}"/> // <constructor-arg value="${solr.solr.config}"/> // </bean> // </constructor-arg> // <constructor-arg value="${solr.containername}"/> // </bean> final EmbeddedSolrServer solrServer; try {//from ww w.j a va 2 s.com solrServer = new EmbeddedSolrServer(new CoreContainer(solrHome.getAbsolutePath(), solrCoreConfig), solrContainerName); } catch (ParserConfigurationException e) { throw new MojoExecutionException(e.getMessage()); } catch (IOException e) { throw new MojoExecutionException(e.getMessage()); } catch (SAXException e) { throw new MojoExecutionException(e.getMessage()); } final DcsSolrAccessService accessSvc; try { accessSvc = new DcsSolrAccessService(solrServer, archive); } catch (IOException e) { throw new MojoExecutionException(e.getMessage()); } try { accessSvc.indexArchive(); } catch (IOException e) { throw new MojoExecutionException(e.getMessage()); } }