1 package org.apache.lucene; 2 3 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 23 import org.apache.lucene.store.*; 24 import org.apache.lucene.document.*; 25 import org.apache.lucene.analysis.*; 26 import org.apache.lucene.index.*; 27 import org.apache.lucene.search.*; 28 import org.apache.lucene.queryParser.*; 29 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 import junit.textui.TestRunner; 33 34 35 39 public class TestSearchForDuplicates extends TestCase { 40 41 42 public static void main(String args[]) { 43 TestRunner.run (new TestSuite(TestSearchForDuplicates.class)); 44 } 45 46 47 48 static final String PRIORITY_FIELD ="priority"; 49 static final String ID_FIELD ="id"; 50 static final String HIGH_PRIORITY ="high"; 51 static final String MED_PRIORITY ="medium"; 52 static final String LOW_PRIORITY ="low"; 53 54 55 63 public void testRun() throws Exception { 64 StringWriter sw = new StringWriter (); 65 PrintWriter pw = new PrintWriter (sw, true); 66 doTest(pw, false); 67 pw.close(); 68 sw.close(); 69 String multiFileOutput = sw.getBuffer().toString(); 70 72 sw = new StringWriter (); 73 pw = new PrintWriter (sw, true); 74 doTest(pw, true); 75 pw.close(); 76 sw.close(); 77 String singleFileOutput = sw.getBuffer().toString(); 78 79 assertEquals(multiFileOutput, singleFileOutput); 80 } 81 82 83 private void doTest(PrintWriter out, boolean useCompoundFiles) throws Exception { 84 Directory directory = new RAMDirectory(); 85 Analyzer analyzer = new SimpleAnalyzer(); 86 IndexWriter writer = new IndexWriter(directory, analyzer, true); 87 88 writer.setUseCompoundFile(useCompoundFiles); 89 90 final int MAX_DOCS = 225; 91 92 for (int j = 0; j < MAX_DOCS; j++) { 93 Document d = new Document(); 94 d.add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY)); 95 d.add(Field.Text(ID_FIELD, Integer.toString(j))); 96 writer.addDocument(d); 97 } 98 writer.close(); 99 100 Searcher searcher = new IndexSearcher(directory); 102 Hits hits = null; 103 104 QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer); 105 106 Query query = parser.parse(HIGH_PRIORITY); 107 out.println("Query: " + query.toString(PRIORITY_FIELD)); 108 109 hits = searcher.search(query); 110 printHits(out, hits); 111 checkHits(hits, MAX_DOCS); 112 113 searcher.close(); 114 115 searcher = new IndexSearcher(directory); 117 hits = null; 118 119 parser = new QueryParser(PRIORITY_FIELD, analyzer); 120 121 query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY); 122 out.println("Query: " + query.toString(PRIORITY_FIELD)); 123 124 hits = searcher.search(query); 125 printHits(out, hits); 126 checkHits(hits, MAX_DOCS); 127 128 searcher.close(); 129 } 130 131 132 private void printHits(PrintWriter out, Hits hits ) throws IOException { 133 out.println(hits.length() + " total results\n"); 134 for (int i = 0 ; i < hits.length(); i++) { 135 if ( i < 10 || (i > 94 && i < 105) ) { 136 Document d = hits.doc(i); 137 out.println(i + " " + d.get(ID_FIELD)); 138 } 139 } 140 } 141 142 private void checkHits(Hits hits, int expectedCount) throws IOException { 143 assertEquals("total results", expectedCount, hits.length()); 144 for (int i = 0 ; i < hits.length(); i++) { 145 if ( i < 10 || (i > 94 && i < 105) ) { 146 Document d = hits.doc(i); 147 assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD)); 148 } 149 } 150 } 151 152 } 153 | Popular Tags |