Commits

Jonathan Simms committed 13fd5d2

implement repl

Comments (0)

Files changed (1)

src/net/freenose/lucener/SearchFiles.scala

 import org.apache.lucene.analysis.standard.StandardAnalyzer
 import org.apache.lucene.util.Version
 import org.apache.lucene.queryparser.classic.QueryParser
+import java.util.Date
 
 /*
 "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/java/4_0/demo.html for details.";
                   val parser: QueryParser,
                   val input: BufferedReader) {
 
+  def repl() {
+    if (!isQueries && !isQuery) println("Enter query: ")
+
+    val rawLine = if (isQuery) queryString else input.readLine()
+
+    if (rawLine == null || rawLine.length == -1) return
+
+    val line = rawLine.trim
+
+    if (line.length == 0) return
+
+    val query = parser.parse(line)
+    println("Searching for: " + query.toString(field))
+
+    if (repeat > 0) {
+      val start = new Date()
+      var count = 0
+      while (count < repeat) {
+        searcher.search(query, null, 100)
+        count += 1
+      }
+      val end = new Date()
+      println("Time: " + (end.getTime - start.getTime) + "ms")
+    }
+
+    if (isQuery) return
+    repl()  // Let's do it again! TCO FTW!
+  }
+
+
+  private def isQueries = SearchFiles.options.queries.isDefined
+  private def isQuery = SearchFiles.options.query.isDefined
+  private def queryString = SearchFiles.options.query.value.get
+  private def field = SearchFiles.options.field.value.get
+  private def repeat = SearchFiles.options.repeat.value.get
+
 }
 
 object SearchFiles {