Skip to content

Instantly share code, notes, and snippets.

@bluepapa32
Created December 27, 2011 22:29
Show Gist options
  • Save bluepapa32/1525366 to your computer and use it in GitHub Desktop.
Save bluepapa32/1525366 to your computer and use it in GitHub Desktop.
Apache Solr Japanese 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 { jetty }
repositories {
mavenCentral()
ivy { artifactPattern "http://lucene-gosen.googlecode.com/files/[artifact]-[revision](-[classifier]).[ext]" }
}
dependencies {
jetty 'com.google.lucene-gosen:lucene-gosen:1.2.0'
jetty 'com.google.lucene-gosen:lucene-gosen:1.2.0:ipadic'
// jetty 'com.google.lucene-gosen:lucene-gosen:1.2.0:naist-chasen'
}
task download {
onlyIf { !file("${basename}.zip").exists() }
doLast { ant.get(src: "${baseurl}/${basename}.zip", dest: '.') }
}
task unzip(dependsOn: download) {
onlyIf { !file(basename).exists() }
doLast { ant.unzip(src: "${basename}.zip", dest: '.') }
}
task lib(type: Sync, dependsOn: unzip) {
from configurations.jetty
into "${basename}/example/solr/lib"
}
task schema(dependsOn: unzip) {
def bak = file("${basename}/example/solr/conf/schema.xml.BAK")
onlyIf { !bak.exists() }
doLast {
def xml = file("${basename}/example/solr/conf/schema.xml")
ant.copy(file: xml, tofile: bak)
def root = new XmlParser().parse(bak)
root.types[0].appendNode('fieldType', [name:'text_ja', class:'solr.TextField', positionIncrementGap:'100'])
.appendNode('analyzer')
.appendNode('tokenizer', [class:'solr.JapaneseTokenizerFactory'])
root.fields.field.find{ it.@name == 'text' }.@type = 'text_ja'
xml.withPrintWriter('UTF-8') { new XmlNodePrinter(it).print(root) }
}
}
task install(dependsOn: [download, unzip, lib, schema])
task downloadDocs {
onlyIf { !file("postgres.tar.gz").exists() }
doLast { ant.get(src: "http://www.postgresql.jp/document/9.1/postgres.tar.gz", dest: '.') }
}
task unzipDocs(dependsOn: downloadDocs) {
onlyIf { !file('docs').exists() }
doLast { ant.untar(src: "postgres.tar.gz", dest: 'docs', compression: 'gzip') }
}
task index(dependsOn: [install, unzipDocs]) << {
def server = new CommonsHttpSolrServer(serverurl)
server.deleteByQuery("*:*")
server.add(fileTree(dir: 'docs').collect { f ->
println f
def doc = new SolrInputDocument()
doc.addField( "id", f.path, 1.0f )
doc.addField( "text", f.getText('EUC_JP'))
return doc
})
server.commit()
}
tasks.addRule("Pattern: ?<query>") { String taskName ->
if (taskName.startsWith("?")) {
task(taskName, dependsOn: install) << {
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: install) << {
ant.java(dir: "${basename}/example",
classname: 'org.mortbay.start.Main',
classpath: "${basename}/example/start.jar",
spawn: true, fork: true) {
sysproperty(key: 'STOP.PORT', value: '8982')
sysproperty(key: 'STOP.KEY', value: 'secret')
if (name == 'stop') arg(value: '--stop')
}
}
}
start.dependsOn stop
task clean(type: Delete) {
delete "${basename}", 'docs'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment