我正在扩展 Solr 的AbstractSolrTestCase以进行单元测试。
我有现有的“schema.xml”、“solrconfig.xml”和索引数据。我可以启动一个嵌入式 solr 服务器来加载现有的集合及其数据。然后在 solr 中测试搜索文档。
这些测试文件(solr conf 和数据索引)仍然适用于 Solr 4.2。我可以在命令行中启动 solr 服务器,并通过管理页面成功查询文档。
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);
}
}