1 package org.apache.lucene.search; 2 3 18 19 import junit.framework.TestCase; 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 import junit.textui.TestRunner; 23 import org.apache.lucene.store.RAMDirectory; 24 import org.apache.lucene.index.IndexWriter; 25 import org.apache.lucene.index.Term; 26 import org.apache.lucene.index.IndexReader; 27 import org.apache.lucene.analysis.WhitespaceAnalyzer; 28 import org.apache.lucene.document.Document; 29 import org.apache.lucene.document.Field; 30 import org.apache.lucene.search.PrefixQuery; 31 import org.apache.lucene.search.Query; 32 import org.apache.lucene.search.BooleanQuery; 33 34 import java.io.IOException ; 35 36 40 41 public class TestBooleanPrefixQuery extends TestCase { 42 43 public static void main(String [] args) { 44 TestRunner.run(suite()); 45 } 46 47 public static Test suite() { 48 return new TestSuite(TestBooleanPrefixQuery.class); 49 } 50 51 public TestBooleanPrefixQuery(String name) { 52 super(name); 53 } 54 55 public void testMethod() { 56 RAMDirectory directory = new RAMDirectory(); 57 58 String [] categories = new String []{"food", 59 "foodanddrink", 60 "foodanddrinkandgoodtimes", 61 "food and drink"}; 62 63 Query rw1 = null; 64 Query rw2 = null; 65 try { 66 IndexWriter writer = new IndexWriter(directory, new 67 WhitespaceAnalyzer(), true); 68 for (int i = 0; i < categories.length; i++) { 69 Document doc = new Document(); 70 doc.add(Field.Keyword("category", categories[i])); 71 writer.addDocument(doc); 72 } 73 writer.close(); 74 75 IndexReader reader = IndexReader.open(directory); 76 PrefixQuery query = new PrefixQuery(new Term("category", "foo")); 77 78 rw1 = query.rewrite(reader); 79 80 BooleanQuery bq = new BooleanQuery(); 81 bq.add(query, true, false); 82 83 rw2 = bq.rewrite(reader); 84 } catch (IOException e) { 85 fail(e.getMessage()); 86 } 87 88 BooleanQuery bq1 = null; 89 if (rw1 instanceof BooleanQuery) { 90 bq1 = (BooleanQuery) rw1; 91 } 92 93 BooleanQuery bq2 = null; 94 if (rw2 instanceof BooleanQuery) { 95 bq2 = (BooleanQuery) rw2; 96 } else { 97 fail("Rewrite"); 98 } 99 100 assertEquals("Number of Clauses Mismatch", bq1.getClauses().length, 101 bq2.getClauses().length); 102 } 103 } 104 105 | Popular Tags |