Solr 4.2 EmbeddedSolrServer无法加载现有的索引数据

时间:2013-04-11 11:04:45

标签: solr lucene solr4

我正在扩展Solr的 AbstractSolrTestCase 以进行单元测试。

我有'schema.xml','solrconfig.xml'和索引数据。我可以启动一个嵌入式solr服务器来加载现有的集合及其数据。然后在solr。中测试搜索doc。

这些测试文件(solr conf和数据索引)仍然适用于Solr 4.2。我可以在命令行中启动solr服务器,并通过管理页面成功查询doc。

  

java -Dsolr.solr.home = / somewhere / testdata / solr -jar start.jar

然而,在适应Solr 4.2.1之后,这种方式不再适用。经过一番调查,我发现看起来索引数据没有被solr的EmbeddedSolrServer加载。

有人知道如何让EmbeddedSolrServer加载现有的索引数据吗?

我正在使用以下代码创建用于单元测试的嵌入式服务器。在下面的代码中,我使用了solr官方示例中的索引数据。

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<?> 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
                    && 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() > 0);
    }

}

0 个答案:

没有答案