Note that there are some explanatory texts on larger screens.

plurals
  1. POSolr 4.2 EmbeddedSolrServer can not load existing index data
    text
    copied!<p>I'm extending Solr's <strong>AbstractSolrTestCase</strong> for unit testing.</p> <p>I have existing 'schema.xml', 'solrconfig.xml' and index data. I can start an embedded solr server to load existing collection and its data. Then test searching doc in solr.</p> <p>Those test files(solr conf and data index) still work with Solr 4.2. I can launch a solr server in command line, and successfully query doc via admin page.</p> <blockquote> <p>java -Dsolr.solr.home=/somewhere/testdata/solr -jar start.jar</p> </blockquote> <p>However this way does not work any more after adapting to Solr 4.2.1. After some investigating, I found it looks like the index data is not loaded by solr's EmbeddedSolrServer.</p> <p>Does anybody know how to let EmbeddedSolrServer load the existing index data?</p> <p>I'm using below code to create embedded server for unit testing. In below code I'm using index data from solr official example.</p> <pre><code>public class IntegrationSolrTest2 extends AbstractSolrTestCase { private static final String CORE_NAME = "collection1"; private static File solrTestHome = new File( "/Users/kane/Downloads/solr-4.2.1/example/solr"); private static File confHome = new File(solrTestHome, CORE_NAME + "/conf"); private static File indexFile = new File(confHome.getParentFile(), "data/index"); private SolrServer server; @Override public String getSolrHome() { return solrTestHome.getAbsolutePath(); } final String configFile = new File(confHome, "solrconfig.xml") .getAbsolutePath(); // Creates a container based on infos needed to create one core static class Initializer extends CoreContainer.Initializer { String coreName; String dataDirectory; SolrConfig solrConfig; IndexSchema indexSchema; public Initializer(String coreName, String dataDirectory, SolrConfig solrConfig, IndexSchema indexSchema) { if (coreName == null) coreName = CoreContainer.DEFAULT_DEFAULT_CORE_NAME; this.coreName = coreName; this.dataDirectory = dataDirectory; this.solrConfig = solrConfig; this.indexSchema = indexSchema; } public String getCoreName() { return coreName; } @Override public CoreContainer initialize() { CoreContainer container = new CoreContainer(new SolrResourceLoader( SolrResourceLoader.locateSolrHome())) { { hostPort = System.getProperty("hostPort"); hostContext = "solr"; defaultCoreName = coreName; initShardHandler(null); initZooKeeper(System.getProperty("zkHost"), 10000); } }; LogWatcher&lt;?&gt; logging = new JulWatcher("test"); logging.registerListener(new ListenerConfig(), container); container.setLogging(logging); CoreDescriptor dcore = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstanceDir()); dcore.setConfigName(solrConfig.getResourceName()); dcore.setSchemaName(indexSchema.getResourceName()); SolrCore core = new SolrCore(coreName, dataDirectory, solrConfig, indexSchema, dcore); container.register(coreName, core, false); // TODO: we should be exercising the *same* core container // initialization code, not equivalent code! if (container.getZkController() == null &amp;&amp; core.getUpdateHandler().getUpdateLog() != null) { // always kick off recovery if we are in standalone mode. core.getUpdateHandler().getUpdateLog().recoverFromLog(); } return container; } } @Before public void prepare() throws Exception { super.setUp(); solrConfig = TestHarness.createConfig(solrTestHome.getAbsolutePath(), CORE_NAME, configFile); h = new TestHarness(IntegrationSolrTest2.CORE_NAME, new Initializer( IntegrationSolrTest2.CORE_NAME, indexFile.getParentFile() .getAbsolutePath(), solrConfig, new IndexSchema( solrConfig, getSchemaFile(), null))); server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore() .getName()); h.getCoreContainer().reload(CORE_NAME); } @After public void cleanup() { h.getCoreContainer().shutdown(); } @Test public void searchAll() throws Exception { h.reload(); SolrQuery params = new SolrQuery(); params.setQuery("*:*"); QueryResponse response = server.query(params); final SolrDocumentList results = response.getResults(); assertTrue("Not search the expected goods!", results.getNumFound() &gt; 0); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload