mirror of https://github.com/apache/lucene.git
Add options so that this is also useful for search benchmarking.
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150516 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e0c8c5a469
commit
faebce61db
|
@ -18,6 +18,8 @@ package org.apache.lucene.demo;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.Analyzer;
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
||||||
|
@ -29,29 +31,79 @@ import org.apache.lucene.search.Hits;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
import org.apache.lucene.queryParser.QueryParser;
|
||||||
|
|
||||||
class SearchFiles {
|
class SearchFiles {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws Exception {
|
||||||
try {
|
String usage =
|
||||||
Searcher searcher = new IndexSearcher("index");
|
"java org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-raw] ";
|
||||||
|
|
||||||
|
String index = "index";
|
||||||
|
String field = "contents";
|
||||||
|
String queries = null;
|
||||||
|
int repeat = 0;
|
||||||
|
boolean raw = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
if ("-index".equals(args[i])) {
|
||||||
|
index = args[i+1];
|
||||||
|
i++;
|
||||||
|
} else if ("-field".equals(args[i])) {
|
||||||
|
field = args[i+1];
|
||||||
|
i++;
|
||||||
|
} else if ("-queries".equals(args[i])) {
|
||||||
|
queries = args[i+1];
|
||||||
|
i++;
|
||||||
|
} else if ("-repeat".equals(args[i])) {
|
||||||
|
repeat = Integer.parseInt(args[i+1]);
|
||||||
|
i++;
|
||||||
|
} else if ("-raw".equals(args[i])) {
|
||||||
|
raw = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Searcher searcher = new IndexSearcher(index);
|
||||||
Analyzer analyzer = new StandardAnalyzer();
|
Analyzer analyzer = new StandardAnalyzer();
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader in = null;
|
||||||
|
if (queries != null) {
|
||||||
|
in = new BufferedReader(new FileReader(queries));
|
||||||
|
} else {
|
||||||
|
in = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (queries == null) // prompt the user
|
||||||
System.out.print("Query: ");
|
System.out.print("Query: ");
|
||||||
|
|
||||||
String line = in.readLine();
|
String line = in.readLine();
|
||||||
|
|
||||||
if (line.length() == -1)
|
if (line == null || line.length() == -1)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
Query query = QueryParser.parse(line, "contents", analyzer);
|
Query query = QueryParser.parse(line, field, analyzer);
|
||||||
System.out.println("Searching for: " + query.toString("contents"));
|
System.out.println("Searching for: " + query.toString(field));
|
||||||
|
|
||||||
Hits hits = searcher.search(query);
|
Hits hits = searcher.search(query);
|
||||||
|
|
||||||
|
if (repeat > 0) { // repeat & time as benchmark
|
||||||
|
Date start = new Date();
|
||||||
|
for (int i = 0; i < repeat; i++) {
|
||||||
|
hits = searcher.search(query);
|
||||||
|
}
|
||||||
|
Date end = new Date();
|
||||||
|
System.out.println("Time: "+(end.getTime()-start.getTime())+"ms");
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println(hits.length() + " total matching documents");
|
System.out.println(hits.length() + " total matching documents");
|
||||||
|
|
||||||
final int HITS_PER_PAGE = 10;
|
final int HITS_PER_PAGE = 10;
|
||||||
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
|
for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
|
||||||
int end = Math.min(hits.length(), start + HITS_PER_PAGE);
|
int end = Math.min(hits.length(), start + HITS_PER_PAGE);
|
||||||
for (int i = start; i < end; i++) {
|
for (int i = start; i < end; i++) {
|
||||||
|
|
||||||
|
if (raw) { // output raw format
|
||||||
|
System.out.println("doc="+hits.id(i)+" score="+hits.score(i));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Document doc = hits.doc(i);
|
Document doc = hits.doc(i);
|
||||||
String path = doc.get("path");
|
String path = doc.get("path");
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
|
@ -65,6 +117,9 @@ class SearchFiles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (queries != null) // non-interactive
|
||||||
|
break;
|
||||||
|
|
||||||
if (hits.length() > end) {
|
if (hits.length() > end) {
|
||||||
System.out.print("more (y/n) ? ");
|
System.out.print("more (y/n) ? ");
|
||||||
line = in.readLine();
|
line = in.readLine();
|
||||||
|
@ -74,10 +129,5 @@ class SearchFiles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
searcher.close();
|
searcher.close();
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(" caught a " + e.getClass() +
|
|
||||||
"\n with message: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue