searcher.groovy

来自「大名鼎鼎的java动态脚本语言。已经通过了sun的认证」· GROOVY 代码 · 共 39 行

GROOVY
39
字号
import org.apache.lucene.analysis.standard.StandardAnalyzerimport org.apache.lucene.queryParser.QueryParserimport org.apache.lucene.search.IndexSearcherimport org.apache.lucene.store.FSDirectory/** * Searcher: searches a Lucene index for a query passed as an argument * * @author Jeremy Rayner <groovy@ross-rayner.com> * based on examples in the wonderful 'Lucene in Action' book * by Erik Hatcher and Otis Gospodnetic ( http://www.lucenebook.com ) * * requires a lucene-1.x.x.jar from http://lucene.apache.org */if (args.size() != 2) {    throw new Exception("Usage: groovy -cp lucene-1.4.3.jar Searcher <index dir> <query>")}def indexDir = new File(args[0]) // Index directory create by Indexerdef q = args[1] // Query stringif (!indexDir.exists() || !indexDir.directory) {    throw new Exception("$indexDir does not exist or is not a directory")}def fsDir = FSDirectory.getDirectory(indexDir, false)def is = new IndexSearcher(fsDir) // Open indexdef query = QueryParser.parse(q, "contents", new StandardAnalyzer()) // Parse querydef start = new Date().timedef hits = is.search(query) // Search indexdef end = new Date().timeprintln "Found ${hits.length()} document(s) (in ${end - start} milliseconds) that matched query '$q':"for ( i in 0 ..< hits.length() ) {    println(hits.doc(i)["filename"]) // Retrieve matching document and display filename}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?