Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Created March 3, 2015 20:11
Show Gist options
  • Save tomgullo/97cfac7e521fb3aa419e to your computer and use it in GitHub Desktop.
Save tomgullo/97cfac7e521fb3aa419e to your computer and use it in GitHub Desktop.
Worlds smallest Solr program
@Grapes([
@Grab('org.apache.solr:solr-solrj:4.5.0'),
@Grab(group='org.apache.solr', module='solr-core', version='4.5.0'),
@GrabExclude('org.restlet.jee:org.restlet'),
@GrabExclude('org.restlet.jee:org.restlet.ext.servlet'),
])
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.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.search.SolrIndexSearcher;
//The directory structure is solr/collection1/conf
// solr/collection1/conf/schema.xml
// solr/collection1/conf/solrconfig.xml
// solr/data
def container = new CoreContainer("solr");
container.load();
def server = new EmbeddedSolrServer(container, "collection1" );
//add document
def newDoc = new SolrInputDocument();
newDoc.addField("id", "1");
newDoc.addField("text", "Hello World")
server.add(newDoc);
server.commit();
def params = new ModifiableSolrParams();
params.set("q", "text:hello");
QueryResponse qResp = server.query(params);
SolrDocumentList docList = qResp.getResults();
println("Num docs: " + docList.getNumFound());
println("Text: " + docList.get(0))
server.shutdown()
//delete contents of data directory
def f = new File("solr/collection1/data")
f.deleteDir()
f.mkdirs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment