package com.gruter.lia.meetlucene;
import java.io.File;
import java.util.Date;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
* Lucene version 2.4
* Lucene In Action 1.4.2 색인 내의 검색 코드에 대한 버전 수정본
**/
public class Searcher {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new Exception("Usage: java " + Searcher.class.getName()
+ " <index dir> <query>");
}
File indexDir = new File(args[0]);
String q = args[1];
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir +
" does not exist or is not a directory.");
}
search(indexDir, q);
}
public static void search(File indexDir, String q)
throws Exception {
// Directory fsDir = FSDirectory.getDirectory(indexDir, false);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher is = new IndexSearcher(fsDir);
// Query query = QueryParser.parse(q, "contents",
// new StandardAnalyzer());
QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
Query query = parser.parse(q);
long start = new Date().getTime();
// Hits hits = is.search(query);
TopDocs docs = is.search(query, 100);
long end = new Date().getTime();
// System.err.println("Found " + hits.length() +
// " document(s) (in " + (end - start) +
// " milliseconds) that matched query '" +
// q + "':");
//
// for (int i = 0; i < hits.length(); i++) {
// Document doc = hits.doc(i);
// System.out.println(doc.get("filename"));
// }
System.err.println("Found " + docs.totalHits +
" document(s) (in " + (end - start) +
" milliseconds) that matched query '" +
q + "':");
ScoreDoc[] scores = docs.scoreDocs;
float maxScore = docs.getMaxScore();
for (int i=0 ;i<docs.scoreDocs.length;i++){
Document doc = is.doc(docs.scoreDocs[i].doc);
System.out.println(doc.get("filename"));
}
}
}
