pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestBoolean2.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
20
21 import java.util.Random;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.WhitespaceAnalyzer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.index.RandomIndexWriter;
28 import org.apache.lucene.index.Term;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.queryParser.ParseException;
31 import org.apache.lucene.queryParser.QueryParser;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.store.MockDirectoryWrapper;
34 import org.apache.lucene.store.RAMDirectory;
35 import org.apache.lucene.util.LuceneTestCase;
36 import org.apache.lucene.util._TestUtil;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40
41 /** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
42  * This also tests the scoring order of BooleanQuery.
43  */
44 public class TestBoolean2 extends LuceneTestCase {
45   private static IndexSearcher searcher;
46   private static IndexSearcher bigSearcher;
47   private static IndexReader reader;
48   private static IndexReader littleReader;
49   private static int NUM_EXTRA_DOCS = 6000;
50
51   public static final String field = "field";
52   private static Directory directory;
53   private static Directory dir2;
54   private static int mulFactor;
55
56   @BeforeClass
57   public static void beforeClass() throws Exception {
58     directory = newDirectory();
59     RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
60     for (int i = 0; i < docFields.length; i++) {
61       Document doc = new Document();
62       doc.add(newField(field, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
63       writer.addDocument(doc);
64     }
65     writer.close();
66     littleReader = IndexReader.open(directory);
67     searcher = new IndexSearcher(littleReader);
68
69     // Make big index
70     dir2 = new MockDirectoryWrapper(random, new RAMDirectory(directory));
71
72     // First multiply small test index:
73     mulFactor = 1;
74     int docCount = 0;
75     do {
76       final Directory copy = new MockDirectoryWrapper(random, new RAMDirectory(dir2));
77       RandomIndexWriter w = new RandomIndexWriter(random, dir2);
78       w.addIndexes(new Directory[] {copy});
79       docCount = w.maxDoc();
80       w.close();
81       mulFactor *= 2;
82     } while(docCount < 3000);
83
84     RandomIndexWriter w = new RandomIndexWriter(random, dir2, 
85         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
86         .setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
87     Document doc = new Document();
88     doc.add(newField("field2", "xxx", Field.Store.NO, Field.Index.ANALYZED));
89     for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {
90       w.addDocument(doc);
91     }
92     doc = new Document();
93     doc.add(newField("field2", "big bad bug", Field.Store.NO, Field.Index.ANALYZED));
94     for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {
95       w.addDocument(doc);
96     }
97     reader = w.getReader();
98     bigSearcher = newSearcher(reader);
99     w.close();
100   }
101
102   @AfterClass
103   public static void afterClass() throws Exception {
104     searcher.close();
105     reader.close();
106     littleReader.close();
107     dir2.close();
108     directory.close();
109     bigSearcher.close();
110     searcher = null;
111     reader = null;
112     littleReader = null;
113     dir2 = null;
114     directory = null;
115     bigSearcher = null;
116   }
117
118   private static String[] docFields = {
119     "w1 w2 w3 w4 w5",
120     "w1 w3 w2 w3",
121     "w1 xx w2 yy w3",
122     "w1 w3 xx w2 yy w3"
123   };
124
125   public Query makeQuery(String queryText) throws ParseException {
126     Query q = (new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random))).parse(queryText);
127     return q;
128   }
129
130   public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
131 //System.out.println();
132 //System.out.println("Query: " + queryText);
133
134     Query query = makeQuery(queryText);
135     TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
136     searcher.search(query, null, collector);
137     ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
138
139     collector = TopScoreDocCollector.create(1000, true);
140     searcher.search(query, null, collector);
141     ScoreDoc[] hits2 = collector.topDocs().scoreDocs; 
142
143     assertEquals(mulFactor * collector.totalHits,
144                  bigSearcher.search(query, 1).totalHits);
145       
146     CheckHits.checkHitsQuery(query, hits1, hits2, expDocNrs);
147   }
148
149   @Test
150   public void testQueries01() throws Exception {
151     String queryText = "+w3 +xx";
152     int[] expDocNrs = {2,3};
153     queriesTest(queryText, expDocNrs);
154   }
155
156   @Test
157   public void testQueries02() throws Exception {
158     String queryText = "+w3 xx";
159     int[] expDocNrs = {2,3,1,0};
160     queriesTest(queryText, expDocNrs);
161   }
162
163   @Test
164   public void testQueries03() throws Exception {
165     String queryText = "w3 xx";
166     int[] expDocNrs = {2,3,1,0};
167     queriesTest(queryText, expDocNrs);
168   }
169
170   @Test
171   public void testQueries04() throws Exception {
172     String queryText = "w3 -xx";
173     int[] expDocNrs = {1,0};
174     queriesTest(queryText, expDocNrs);
175   }
176
177   @Test
178   public void testQueries05() throws Exception {
179     String queryText = "+w3 -xx";
180     int[] expDocNrs = {1,0};
181     queriesTest(queryText, expDocNrs);
182   }
183
184   @Test
185   public void testQueries06() throws Exception {
186     String queryText = "+w3 -xx -w5";
187     int[] expDocNrs = {1};
188     queriesTest(queryText, expDocNrs);
189   }
190
191   @Test
192   public void testQueries07() throws Exception {
193     String queryText = "-w3 -xx -w5";
194     int[] expDocNrs = {};
195     queriesTest(queryText, expDocNrs);
196   }
197
198   @Test
199   public void testQueries08() throws Exception {
200     String queryText = "+w3 xx -w5";
201     int[] expDocNrs = {2,3,1};
202     queriesTest(queryText, expDocNrs);
203   }
204
205   @Test
206   public void testQueries09() throws Exception {
207     String queryText = "+w3 +xx +w2 zz";
208     int[] expDocNrs = {2, 3};
209     queriesTest(queryText, expDocNrs);
210   }
211
212   @Test
213   public void testQueries10() throws Exception {
214     String queryText = "+w3 +xx +w2 zz";
215     int[] expDocNrs = {2, 3};
216     Similarity oldSimilarity = searcher.getSimilarity();
217     try {
218       searcher.setSimilarity(new DefaultSimilarity(){
219         @Override
220         public float coord(int overlap, int maxOverlap) {
221           return overlap / ((float)maxOverlap - 1);
222         }
223       });
224       queriesTest(queryText, expDocNrs);
225     } finally {
226       searcher.setSimilarity(oldSimilarity);
227     }
228   }
229
230   @Test
231   public void testRandomQueries() throws Exception {
232     String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
233
234     int tot=0;
235
236     BooleanQuery q1 = null;
237     try {
238
239       // increase number of iterations for more complete testing
240       int num = atLeast(10);
241       for (int i=0; i<num; i++) {
242         int level = random.nextInt(3);
243         q1 = randBoolQuery(new Random(random.nextLong()), random.nextBoolean(), level, field, vals, null);
244         
245         // Can't sort by relevance since floating point numbers may not quite
246         // match up.
247         Sort sort = Sort.INDEXORDER;
248
249         QueryUtils.check(random, q1,searcher);
250
251         TopFieldCollector collector = TopFieldCollector.create(sort, 1000,
252             false, true, true, true);
253
254         searcher.search(q1, null, collector);
255         ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
256
257         collector = TopFieldCollector.create(sort, 1000,
258             false, true, true, false);
259         
260         searcher.search(q1, null, collector);
261         ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
262         tot+=hits2.length;
263         CheckHits.checkEqual(q1, hits1, hits2);
264
265         BooleanQuery q3 = new BooleanQuery();
266         q3.add(q1, BooleanClause.Occur.SHOULD);
267         q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD);
268         TopDocs hits4 = bigSearcher.search(q3, 1);
269         assertEquals(mulFactor*collector.totalHits + NUM_EXTRA_DOCS/2, hits4.totalHits);
270       }
271
272     } catch (Exception e) {
273       // For easier debugging
274       System.out.println("failed query: " + q1);
275       throw e;
276     }
277
278     // System.out.println("Total hits:"+tot);
279   }
280
281
282   // used to set properties or change every BooleanQuery
283   // generated from randBoolQuery.
284   public static interface Callback {
285     public void postCreate(BooleanQuery q);
286   }
287
288   // Random rnd is passed in so that the exact same random query may be created
289   // more than once.
290   public static BooleanQuery randBoolQuery(Random rnd, boolean allowMust, int level, String field, String[] vals, Callback cb) {
291     BooleanQuery current = new BooleanQuery(rnd.nextInt()<0);
292     for (int i=0; i<rnd.nextInt(vals.length)+1; i++) {
293       int qType=0; // term query
294       if (level>0) {
295         qType = rnd.nextInt(10);
296       }
297       Query q;
298       if (qType < 3) {
299         q = new TermQuery(new Term(field, vals[rnd.nextInt(vals.length)]));
300       } else if (qType < 7) {
301         q = new WildcardQuery(new Term(field, "w*"));
302       } else {
303         q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb);
304       }
305
306       int r = rnd.nextInt(10);
307       BooleanClause.Occur occur;
308       if (r<2) {
309         occur=BooleanClause.Occur.MUST_NOT;
310       }
311       else if (r<5) {
312         if (allowMust) {
313           occur=BooleanClause.Occur.MUST;
314         } else {
315           occur=BooleanClause.Occur.SHOULD;
316         }
317       } else {
318         occur=BooleanClause.Occur.SHOULD;
319       }
320
321       current.add(q, occur);
322     }
323     if (cb!=null) cb.postCreate(current);
324     return current;
325   }
326
327
328 }