FileDocCategorySizeDatePackage
TestSearchForDuplicates.javaAPI DocApache Lucene 1.4.34761Tue Mar 30 00:48:06 BST 2004org.apache.lucene

TestSearchForDuplicates

public class TestSearchForDuplicates extends TestCase
JUnit adaptation of an older test case DocTest.
author
dmitrys@earthlink.net
version
$Id: TestSearchForDuplicates.java,v 1.3 2004/03/29 22:48:05 cutting Exp $

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
Constructors Summary
Methods Summary
private voidcheckHits(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 voiddoTest(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 voidmain(java.lang.String[] args)
Main for running test case by itself.

        TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
    
private voidprintHits(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 voidtestRun()
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);