So, the answer to my own ? is:
I was getting:
java.lang.NullPointerException
at org.apache.solr.core.CoreContainer.create(CoreContainer.java:315)
at
com.grantingersoll.noodles.EmbeddedTest.testEmbedded(EmbeddedTest.java:
23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun
.reflect
.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
For the code:
CoreContainer container = new CoreContainer();
CoreDescriptor descriptor = new CoreDescriptor(container,
"spell", "src/main/resources/solr");
SolrCore core = container.create(descriptor);
final SolrServer client = new EmbeddedSolrServer(container,
"spell");
assertTrue("client is null and it shouldn't be", client != null);
This is b/c CoreContainer contains the following code:
File idir = new File(dcore.getInstanceDir());
if (!idir.isAbsolute()) {
idir = new File(loader.getInstanceDir(), dcore.getInstanceDir());
}
The problem is loader is not initialized yet, b/c load isn't called.
Switching to an absolute path is a workaround, but I suppose this is a
bug.
-Grant
On Oct 8, 2008, at 12:27 PM, Grant Ingersoll wrote:
I get an NPE in create when trying this. Seems the loader member is
not instantiated for the container.create() call, since load is not
called.
On Oct 2, 2008, at 10:37 AM, Ryan McKinley wrote:
You could also use the CoreContainer to create a Core from the
descriptor:
CoreContainer container = new CoreContainer();
CoreDescriptor descriptor = new CoreDescriptor(container,
"core1", "/Users/erik/apache-solr-1.3.0/example/solr");
SolrCore core = container.create( descriptor );
if you are using a custom solrconfig name, you would need to call
setConfigName( path ) on the descriptor.
As for closing... have you tried core.close()?
ryan
On Oct 2, 2008, at 8:49 AM, Erik Hatcher wrote:
I'm doing some Java experiments to get ready for a solr-ruby
overhaul such that JRuby comes into play nicely so that
EmbeddedSolrServer can be used transparently too. I've not tried
this since the whole CoreContainer/CoreDescriptor stuff was added,
and I don't quite understand it all. Here's what I've got:
public static void main(String[] args) throws IOException,
ParserConfigurationException, SAXException, SolrServerException {
CoreContainer container = new CoreContainer();
SolrConfig config = new SolrConfig("/Users/erik/apache-solr-1.3.0/
example/solr", "solrconfig.xml", null);
CoreDescriptor descriptor = new CoreDescriptor(container,
"core1", "/Users/erik/apache-solr-1.3.0/example/solr");
SolrCore core = new SolrCore("core1", "/Users/erik/apache-
solr-1.3.0/example/solr/data", config, null, descriptor);
container.register("core1", core, false);
SolrServer solr = new EmbeddedSolrServer(container, "core1");
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = solr.query(query);
System.out.println("response = " + response);
}
This works, but has a fair bit of seemingly unnecessary
duplication, and it also leaves the JVM stays running for some
reason.
Is this the proper way to use EmbeddedSolrServer, or are there
some tips to improving the code and reducing the duplication?
Also, why does the JVM keep running? Are we spinning off a thread
that needs to be shut down? Is there some sort of close() call
that is needed?
Thanks,
Erik