使用junit测试lucene:
import junit.framework.*;
import org.apache.lucene.index.IndexReader;doc.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.ANALYZED));
writer.addDocument(doc);
}
writer.close();
}
private IndexWriter getWriter() throws IOException {
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new WhitespaceAnalyzer(Version.LUCENE_46));
return new IndexWriter(directory, config);
}
protected int getHitCount(String fieldName, String searchString) throws IOException {
IndexReader reader = IndexReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Term t = new Term(fieldName, searchString);
Query query = new TermQuery(t);
int hitCount = IndexTester.hitCount(searcher, query);
reader.close();
return hitCount;
}
@Test
public void testIndexWriter() throws IOException {
IndexWriter writer = getWriter();
assertEquals(ids.length, writer.numDocs());
writer.close();
}
@Test
public void testIndexReader() throws IOException {
IndexReader reader = IndexReader.open(directory);
assertEquals(ids.length, reader.maxDoc());
assertEquals(ids.length, reader.numDocs());
reader.close();
}
public static int hitCount(IndexSearcher searcher, Query query)
throws IOException {
return searcher.search(query, 1).totalHits;
}
}
不使用编译工具:
编译:
javac -classpath lib/lucene-core-4.6-SNAPSHOT.jar:lib/lucene-analyzers-common-4.6-SNAPSHOT.jar:lib/junit.jar IndexTester.java
运行:
java -cp lib/junit.jar:lib/lucene-analyzers-common-4.6-SNAPSHOT.jar:lib/lucene-core-4.6-SNAPSHOT.jar:IndexTester.jar:./ org.junit.runner.JUnitCore IndexTester