Created
March 3, 2015 20:11
-
-
Save tomgullo/97cfac7e521fb3aa419e to your computer and use it in GitHub Desktop.
Worlds smallest Solr program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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