TestSearchForDuplicatespublic class TestSearchForDuplicates extends TestCase JUnit adaptation of an older test case DocTest. |
Fields Summary |
---|
static final String | PRIORITY_FIELD | static final String | ID_FIELD | static final String | HIGH_PRIORITY | static final String | MED_PRIORITY | static final String | LOW_PRIORITY |
Methods Summary |
---|
private void | checkHits(org.apache.lucene.search.Hits hits, int expectedCount)
assertEquals("total results", expectedCount, hits.length());
for (int i = 0 ; i < hits.length(); i++) {
if ( i < 10 || (i > 94 && i < 105) ) {
Document d = hits.doc(i);
assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD));
}
}
| private void | doTest(java.io.PrintWriter out, boolean useCompoundFiles)
Directory directory = new RAMDirectory();
Analyzer analyzer = new SimpleAnalyzer();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
writer.setUseCompoundFile(useCompoundFiles);
final int MAX_DOCS = 225;
for (int j = 0; j < MAX_DOCS; j++) {
Document d = new Document();
d.add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY));
d.add(Field.Text(ID_FIELD, Integer.toString(j)));
writer.addDocument(d);
}
writer.close();
// try a search without OR
Searcher searcher = new IndexSearcher(directory);
Hits hits = null;
QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer);
Query query = parser.parse(HIGH_PRIORITY);
out.println("Query: " + query.toString(PRIORITY_FIELD));
hits = searcher.search(query);
printHits(out, hits);
checkHits(hits, MAX_DOCS);
searcher.close();
// try a new search with OR
searcher = new IndexSearcher(directory);
hits = null;
parser = new QueryParser(PRIORITY_FIELD, analyzer);
query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
out.println("Query: " + query.toString(PRIORITY_FIELD));
hits = searcher.search(query);
printHits(out, hits);
checkHits(hits, MAX_DOCS);
searcher.close();
| public static void | main(java.lang.String[] args)Main for running test case by itself.
TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
| private void | printHits(java.io.PrintWriter out, org.apache.lucene.search.Hits hits)
out.println(hits.length() + " total results\n");
for (int i = 0 ; i < hits.length(); i++) {
if ( i < 10 || (i > 94 && i < 105) ) {
Document d = hits.doc(i);
out.println(i + " " + d.get(ID_FIELD));
}
}
| public void | testRun()This test compares search results when using and not using compound
files.
TODO: There is rudimentary search result validation as well, but it is
simply based on asserting the output observed in the old test case,
without really knowing if the output is correct. Someone needs to
validate this output and make any changes to the checkHits method.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
doTest(pw, false);
pw.close();
sw.close();
String multiFileOutput = sw.getBuffer().toString();
//System.out.println(multiFileOutput);
sw = new StringWriter();
pw = new PrintWriter(sw, true);
doTest(pw, true);
pw.close();
sw.close();
String singleFileOutput = sw.getBuffer().toString();
assertEquals(multiFileOutput, singleFileOutput);
|
|