1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.util.Random;
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;
41 /** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.
42 * This also tests the scoring order of BooleanQuery.
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;
51 public static final String field = "field";
52 private static Directory directory;
53 private static Directory dir2;
54 private static int mulFactor;
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);
66 littleReader = IndexReader.open(directory);
67 searcher = new IndexSearcher(littleReader);
70 dir2 = new MockDirectoryWrapper(random, new RAMDirectory(directory));
72 // First multiply small test index:
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();
82 } while(docCount < 3000);
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++) {
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++) {
97 reader = w.getReader();
98 bigSearcher = newSearcher(reader);
103 public static void afterClass() throws Exception {
106 littleReader.close();
118 private static String[] docFields = {
125 public Query makeQuery(String queryText) throws ParseException {
126 Query q = (new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random))).parse(queryText);
130 public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
131 //System.out.println();
132 //System.out.println("Query: " + queryText);
134 Query query = makeQuery(queryText);
135 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
136 searcher.search(query, null, collector);
137 ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
139 collector = TopScoreDocCollector.create(1000, true);
140 searcher.search(query, null, collector);
141 ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
143 assertEquals(mulFactor * collector.totalHits,
144 bigSearcher.search(query, 1).totalHits);
146 CheckHits.checkHitsQuery(query, hits1, hits2, expDocNrs);
150 public void testQueries01() throws Exception {
151 String queryText = "+w3 +xx";
152 int[] expDocNrs = {2,3};
153 queriesTest(queryText, expDocNrs);
157 public void testQueries02() throws Exception {
158 String queryText = "+w3 xx";
159 int[] expDocNrs = {2,3,1,0};
160 queriesTest(queryText, expDocNrs);
164 public void testQueries03() throws Exception {
165 String queryText = "w3 xx";
166 int[] expDocNrs = {2,3,1,0};
167 queriesTest(queryText, expDocNrs);
171 public void testQueries04() throws Exception {
172 String queryText = "w3 -xx";
173 int[] expDocNrs = {1,0};
174 queriesTest(queryText, expDocNrs);
178 public void testQueries05() throws Exception {
179 String queryText = "+w3 -xx";
180 int[] expDocNrs = {1,0};
181 queriesTest(queryText, expDocNrs);
185 public void testQueries06() throws Exception {
186 String queryText = "+w3 -xx -w5";
187 int[] expDocNrs = {1};
188 queriesTest(queryText, expDocNrs);
192 public void testQueries07() throws Exception {
193 String queryText = "-w3 -xx -w5";
194 int[] expDocNrs = {};
195 queriesTest(queryText, expDocNrs);
199 public void testQueries08() throws Exception {
200 String queryText = "+w3 xx -w5";
201 int[] expDocNrs = {2,3,1};
202 queriesTest(queryText, expDocNrs);
206 public void testQueries09() throws Exception {
207 String queryText = "+w3 +xx +w2 zz";
208 int[] expDocNrs = {2, 3};
209 queriesTest(queryText, expDocNrs);
213 public void testQueries10() throws Exception {
214 String queryText = "+w3 +xx +w2 zz";
215 int[] expDocNrs = {2, 3};
216 Similarity oldSimilarity = searcher.getSimilarity();
218 searcher.setSimilarity(new DefaultSimilarity(){
220 public float coord(int overlap, int maxOverlap) {
221 return overlap / ((float)maxOverlap - 1);
224 queriesTest(queryText, expDocNrs);
226 searcher.setSimilarity(oldSimilarity);
231 public void testRandomQueries() throws Exception {
232 String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
236 BooleanQuery q1 = null;
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);
245 // Can't sort by relevance since floating point numbers may not quite
247 Sort sort = Sort.INDEXORDER;
249 QueryUtils.check(random, q1,searcher);
251 TopFieldCollector collector = TopFieldCollector.create(sort, 1000,
252 false, true, true, true);
254 searcher.search(q1, null, collector);
255 ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
257 collector = TopFieldCollector.create(sort, 1000,
258 false, true, true, false);
260 searcher.search(q1, null, collector);
261 ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
263 CheckHits.checkEqual(q1, hits1, hits2);
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);
272 } catch (Exception e) {
273 // For easier debugging
274 System.out.println("failed query: " + q1);
278 // System.out.println("Total hits:"+tot);
282 // used to set properties or change every BooleanQuery
283 // generated from randBoolQuery.
284 public static interface Callback {
285 public void postCreate(BooleanQuery q);
288 // Random rnd is passed in so that the exact same random query may be created
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
295 qType = rnd.nextInt(10);
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*"));
303 q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb);
306 int r = rnd.nextInt(10);
307 BooleanClause.Occur occur;
309 occur=BooleanClause.Occur.MUST_NOT;
313 occur=BooleanClause.Occur.MUST;
315 occur=BooleanClause.Occur.SHOULD;
318 occur=BooleanClause.Occur.SHOULD;
321 current.add(q, occur);
323 if (cb!=null) cb.postCreate(current);