KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestFilteredQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
28 import java.io.IOException JavaDoc;
29
30
31 /**
32  * FilteredQuery JUnit tests.
33  *
34  * <p>Created: Apr 21, 2004 1:21:46 PM
35  *
36  * @author Tim Jones
37  * @version $Id: TestFilteredQuery.java,v 1.5 2004/07/10 06:19:01 otis Exp $
38  * @since 1.4
39  */

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 JavaDoc {
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 JavaDoc bits (IndexReader reader) throws IOException JavaDoc {
80         BitSet JavaDoc bitset = new BitSet JavaDoc(5);
81         bitset.set (1);
82         bitset.set (3);
83         return bitset;
84       }
85     };
86   }
87
88   public void tearDown()
89   throws Exception JavaDoc {
90     searcher.close();
91     directory.close();
92   }
93
94   public void testFilteredQuery()
95   throws Exception JavaDoc {
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   /**
120    * This tests FilteredQuery's rewrite correctness
121    */

122   public void testRangeQuery() throws Exception JavaDoc {
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