Skip to content

Instantly share code, notes, and snippets.

@bluepapa32
Created December 22, 2011 17:50
Show Gist options
  • Save bluepapa32/1511174 to your computer and use it in GitHub Desktop.
Save bluepapa32/1511174 to your computer and use it in GitHub Desktop.
Apache Solr DEMO with Gradle
import org.apache.solr.common.*
import org.apache.solr.client.solrj.*
import org.apache.solr.client.solrj.impl.*
import org.apache.solr.client.solrj.response.*
basename = "apache-solr-3.5.0"
baseurl = "http://ftp.jaist.ac.jp/pub/apache/lucene/solr/3.5.0"
serverurl = "http://localhost:8983/solr"
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.apache.solr:solr-solrj:3.5.0' }
}
configurations { source; jetty }
repositories { mavenCentral() }
dependencies {
source 'org.apache.lucene:lucene-core:3.5.0:sources'
jetty fileTree(dir: "${basename}/example", includes: ['start.jar', 'lib/**/*.jar'])
}
task init {
onlyIf { !file(basename).exists() }
doLast {
println "get ${baseurl}/${basename}.zip..."
ant.get(src: "${baseurl}/${basename}.zip", dest: '.')
ant.unzip(src: "${basename}.zip", dest: '.')
ant.mkdir(dir: 'src')
ant.unzip(src: configurations.source.singleFile, dest: 'src')
}
}
task index(dependsOn: init) << {
def server = new CommonsHttpSolrServer(serverurl)
server.deleteByQuery("*:*")
server.add(fileTree(dir: 'src').collect { f ->
println f
def doc = new SolrInputDocument()
doc.addField( "id", f.path, 1.0f )
doc.addField( "text", f.text)
return doc
})
server.commit()
}
tasks.addRule("Pattern: ?<query>") { String taskName ->
if (taskName.startsWith("?")) {
task(taskName, dependsOn: init) << {
def query = new SolrQuery(taskName[1..-1]).set("rows", 1000)
new CommonsHttpSolrServer(serverurl).query(query).results.each {
println it.getFieldValue("id")
}
}
}
}
['start', 'stop'].each { name ->
task(name, dependsOn: init) << {
ant.java(dir: "${basename}/example",
classname: 'org.mortbay.start.Main',
classpath: configurations.jetty.asPath,
spawn: true, fork: true) {
sysproperty(key: 'STOP.PORT', value: '8982')
sysproperty(key: 'STOP.KEY', value: 'secret')
if (name == 'stop') arg(value: '--stop')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment