Skip to content

Instantly share code, notes, and snippets.

@nautical
Created March 29, 2014 18:21
Show Gist options
  • Save nautical/9859452 to your computer and use it in GitHub Desktop.
Save nautical/9859452 to your computer and use it in GitHub Desktop.
EmbeddedSolrExample.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();
}
}
@amirensi
Copy link

I'm using sorl-4.9.0,i'm asking for Jars that i need it ,because i'm trying this code using solr-core-4.9.0.jar and
solr-solrj-4.9.0.jar and i got this error
Exception in thread "main" java.lang.NoClassDefFoundError: org/ibex/classgen/CGConst
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.ibex.classgen.CGConst
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)
... 13 more

@amirensi
Copy link

please i need an answer !!!!!!!!!!

@amirensi
Copy link

are there any configuration in solr xml files

@amirensi
Copy link

i need to know what jars i exactly use,because every time i have errors !!!!!!!

@amirensi
Copy link

please it's urgent,can any one help me !!!!!!!!!!

@djavaphp
Copy link

I am also facing same issue now. have you resoled it? I have separate module which search from solr and call that jar dependency in MVC project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment