1 package org.apache.lucene.search; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.analysis.WhitespaceAnalyzer; 21 import org.apache.lucene.document.Document; 22 import org.apache.lucene.document.Field; 23 import org.apache.lucene.index.IndexWriter; 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.index.Term; 26 import org.apache.lucene.store.RAMDirectory; 27 import java.util.BitSet ; 28 import java.io.IOException ; 29 30 31 40 public class TestFilteredQuery 41 extends TestCase { 42 43 private IndexSearcher searcher; 44 private RAMDirectory directory; 45 private Query query; 46 private Filter filter; 47 48 public void setUp() 49 throws Exception { 50 directory = new RAMDirectory(); 51 IndexWriter writer = new IndexWriter (directory, new WhitespaceAnalyzer(), true); 52 53 Document doc = new Document(); 54 doc.add (Field.Text ("field", "one two three four five")); 55 doc.add (Field.Text ("sorter", "b")); 56 writer.addDocument (doc); 57 58 doc = new Document(); 59 doc.add (Field.Text ("field", "one two three four")); 60 doc.add (Field.Text ("sorter", "d")); 61 writer.addDocument (doc); 62 63 doc = new Document(); 64 doc.add (Field.Text ("field", "one two three y")); 65 doc.add (Field.Text ("sorter", "a")); 66 writer.addDocument (doc); 67 68 doc = new Document(); 69 doc.add (Field.Text ("field", "one two x")); 70 doc.add (Field.Text ("sorter", "c")); 71 writer.addDocument (doc); 72 73 writer.optimize (); 74 writer.close (); 75 76 searcher = new IndexSearcher (directory); 77 query = new TermQuery (new Term ("field", "three")); 78 filter = new Filter() { 79 public BitSet bits (IndexReader reader) throws IOException { 80 BitSet bitset = new BitSet (5); 81 bitset.set (1); 82 bitset.set (3); 83 return bitset; 84 } 85 }; 86 } 87 88 public void tearDown() 89 throws Exception { 90 searcher.close(); 91 directory.close(); 92 } 93 94 public void testFilteredQuery() 95 throws Exception { 96 Query filteredquery = new FilteredQuery (query, filter); 97 Hits hits = searcher.search (filteredquery); 98 assertEquals (1, hits.length()); 99 assertEquals (1, hits.id(0)); 100 101 hits = searcher.search (filteredquery, new Sort("sorter")); 102 assertEquals (1, hits.length()); 103 assertEquals (1, hits.id(0)); 104 105 filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "one")), filter); 106 hits = searcher.search (filteredquery); 107 assertEquals (2, hits.length()); 108 109 filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "x")), filter); 110 hits = searcher.search (filteredquery); 111 assertEquals (1, hits.length()); 112 assertEquals (3, hits.id(0)); 113 114 filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "y")), filter); 115 hits = searcher.search (filteredquery); 116 assertEquals (0, hits.length()); 117 } 118 119 122 public void testRangeQuery() throws Exception { 123 RangeQuery rq = new RangeQuery( 124 new Term("sorter", "b"), new Term("sorter", "d"), true); 125 126 Query filteredquery = new FilteredQuery(rq, filter); 127 Hits hits = searcher.search(filteredquery); 128 assertEquals(2, hits.length()); 129 } 130 131 } 132 133 | Popular Tags |