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