我使用https://gist.github.com/nautical/9859452中的以下java代码:
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.common.SolrInputDocument;
public class EmbeddedSolrExample{
static CoreContainer container;
static EmbeddedSolrServer server;
public static void main(String[] args) throws Exception{
String solrDir = "/Users/TheRoot/Downloads/solr-4.7.0/example/solr/";
container = new CoreContainer(solrDir);
container.load();
server = new EmbeddedSolrServer(container, "collection1");
}
public static String query(String qstring){
String output = null;
ModifiableSolrParams solrParams = new ModifiableSolrParams();
System.out.println(qstring);
solrParams.add(CommonParams.Q, qstring);
QueryResponse queryResponse = null;
try {
queryResponse = server.query(solrParams);
} catch (SolrServerException e) {
e.printStackTrace();
}
for (SolrDocument document : queryResponse.getResults()) {
output += (document.getFieldValueMap().toString());
}
System.out.println(output);
return output;
}
public static void entervalues(String Category,int Id, String Name) throws Exception{
SolrInputDocument doc = new SolrInputDocument();
doc.addField("cat", Category);
doc.addField("id", Id);
doc.addField("name", Name);
server.add(doc);
server.commit(); // commit slows down .. do it after ~N inserts .. Seperate it
}
public static void close(){
server.shutdown();
container.shutdown();
}
}
我正在使用sorl-4.9.0,我要求Jars我需要它,因为我正在使用solr-core-4.9.0.jar尝试此代码 solr-solrj-4.9.0.jar我收到了这个错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.apache.solr.core.CoreContainer.<clinit>(CoreContainer.java:64)
at EmbeddedSolrExample.main(EmbeddedSolrExample.java:23)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more