pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestQueryWrapperFilter.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field.Index;
24 import org.apache.lucene.document.Field.Store;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.RandomIndexWriter;
28 import org.apache.lucene.index.Term;
29 import org.apache.lucene.search.BooleanClause.Occur;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.English;
32 import org.apache.lucene.util.LuceneTestCase;
33
34 public class TestQueryWrapperFilter extends LuceneTestCase {
35
36   public void testBasic() throws Exception {
37     Directory dir = newDirectory();
38     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
39     Document doc = new Document();
40     doc.add(newField("field", "value", Store.NO, Index.ANALYZED));
41     writer.addDocument(doc);
42     IndexReader reader = writer.getReader();
43     writer.close();
44
45     TermQuery termQuery = new TermQuery(new Term("field", "value"));
46
47     // should not throw exception with primitive query
48     QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
49
50     IndexSearcher searcher = newSearcher(reader);
51     TopDocs hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
52     assertEquals(1, hits.totalHits);
53     hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
54     assertEquals(1, hits.totalHits);
55
56     // should not throw exception with complex primitive query
57     BooleanQuery booleanQuery = new BooleanQuery();
58     booleanQuery.add(termQuery, Occur.MUST);
59     booleanQuery.add(new TermQuery(new Term("field", "missing")),
60         Occur.MUST_NOT);
61     qwf = new QueryWrapperFilter(termQuery);
62
63     hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
64     assertEquals(1, hits.totalHits);
65     hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
66     assertEquals(1, hits.totalHits);
67
68     // should not throw exception with non primitive Query (doesn't implement
69     // Query#createWeight)
70     qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
71
72     hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
73     assertEquals(1, hits.totalHits);
74     hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
75     assertEquals(1, hits.totalHits);
76
77     // test a query with no hits
78     termQuery = new TermQuery(new Term("field", "not_exist"));
79     qwf = new QueryWrapperFilter(termQuery);
80     hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
81     assertEquals(0, hits.totalHits);
82     hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
83     assertEquals(0, hits.totalHits);
84     searcher.close();
85     reader.close();
86     dir.close();
87   }
88
89   // this test is for 3.x only, in 4.x we no longer support non-atomic readers passed to getDocIdSet():
90   public void test_LUCENE3442() throws Exception {
91
92     Directory dir = newDirectory();
93     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
94     Document doc = new Document();
95     doc.add(newField("id", "1001", Store.YES, Index.NOT_ANALYZED));
96     doc.add(newField("text", "headline one group one", Store.YES, Index.ANALYZED));
97     writer.addDocument(doc);
98     IndexReader rdr = writer.getReader();
99     writer.close();
100     IndexSearcher searcher = new IndexSearcher(rdr);
101     TermQuery tq = new TermQuery(new Term("text", "headline"));
102     TopDocs results = searcher.search(tq, 5);
103     assertEquals(1, results.totalHits);
104     
105     Filter f = new QueryWrapperFilter(tq);
106     // rdr may not be atomic (it isn't in most cases), TermQuery inside QWF should still work!
107     DocIdSet dis = f.getDocIdSet(rdr);
108     assertNotNull(dis);
109     DocIdSetIterator it = dis.iterator();
110     assertNotNull(it);
111     int docId, count = 0;
112     while ((docId = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
113       assertEquals("1001", rdr.document(docId).get("id"));
114       count++;
115     }
116     assertEquals(1, count);
117     searcher.close();
118     rdr.close();
119     dir.close();
120   }
121
122   public void testRandom() throws Exception {
123     final Directory d = newDirectory();
124     final RandomIndexWriter w = new RandomIndexWriter(random, d);
125     w.w.getConfig().setMaxBufferedDocs(17);
126     final int numDocs = atLeast(100);
127     final Set<String> aDocs = new HashSet<String>();
128     for(int i=0;i<numDocs;i++) {
129       final Document doc = new Document();
130       final String v;
131       if (random.nextInt(5) == 4) {
132         v = "a";
133         aDocs.add(""+i);
134       } else {
135         v = "b";
136       }
137       final Field f = newField("field", v, Field.Store.NO, Field.Index.NOT_ANALYZED);
138       doc.add(f);
139       doc.add(newField("id", ""+i, Field.Store.YES, Field.Index.NOT_ANALYZED));
140       w.addDocument(doc);
141     }
142
143     final int numDelDocs = atLeast(10);
144     for(int i=0;i<numDelDocs;i++) {
145       final String delID = ""+random.nextInt(numDocs);
146       w.deleteDocuments(new Term("id", delID));
147       aDocs.remove(delID);
148     }
149
150     final IndexReader r = w.getReader();
151     w.close();
152     final TopDocs hits = new IndexSearcher(r).search(new MatchAllDocsQuery(),
153                                                      new QueryWrapperFilter(new TermQuery(new Term("field", "a"))),
154                                                      numDocs);
155     assertEquals(aDocs.size(), hits.totalHits);
156     for(ScoreDoc sd: hits.scoreDocs) {
157       assertTrue(aDocs.contains(r.document(sd.doc).get("id")));
158     }
159     r.close();
160     d.close();
161   }
162   
163   public void testThousandDocuments() throws Exception {
164     Directory dir = newDirectory();
165     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
166     for (int i = 0; i < 1000; i++) {
167       Document doc = new Document();
168       doc.add(newField("field", English.intToEnglish(i), Field.Index.NOT_ANALYZED));
169       writer.addDocument(doc);
170     }
171     
172     IndexReader reader = writer.getReader();
173     writer.close();
174     
175     IndexSearcher searcher = newSearcher(reader);
176     
177     for (int i = 0; i < 1000; i++) {
178       TermQuery termQuery = new TermQuery(new Term("field", English.intToEnglish(i)));
179       QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
180       TopDocs td = searcher.search(new MatchAllDocsQuery(), qwf, 10);
181       assertEquals(1, td.totalHits);
182     }
183     
184     searcher.close();
185     reader.close();
186     dir.close();
187   }
188 }