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 int NUM_EXTRA_DOCS = 6000;
50
public static final String field = "field";
51
private static Directory directory;
52
private static Directory dir2;
53
private static int mulFactor;
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);
65
searcher = new IndexSearcher(directory, true);
68
dir2 = new MockDirectoryWrapper(random, new RAMDirectory(directory));
70
// First multiply small test index:
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();
80
} while(docCount < 3000);
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++) {
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++) {
95
reader = w.getReader();
96
bigSearcher = newSearcher(reader);
101
public static void afterClass() throws Exception {
114
private static String[] docFields = {
121
public Query makeQuery(String queryText) throws ParseException {
122
Query q = (new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random))).parse(queryText);
126
public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
127
//System.out.println();
128
//System.out.println("Query: " + queryText);
130
Query query = makeQuery(queryText);
131
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
132
searcher.search(query, null, collector);
133
ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
135
collector = TopScoreDocCollector.create(1000, true);
136
searcher.search(query, null, collector);
137
ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
139
assertEquals(mulFactor * collector.totalHits,
140
bigSearcher.search(query, 1).totalHits);
142
CheckHits.checkHitsQuery(query, hits1, hits2, expDocNrs);
146
public void testQueries01() throws Exception {
147
String queryText = "+w3 +xx";
148
int[] expDocNrs = {2,3};
149
queriesTest(queryText, expDocNrs);
153
public void testQueries02() throws Exception {
154
String queryText = "+w3 xx";
155
int[] expDocNrs = {2,3,1,0};
156
queriesTest(queryText, expDocNrs);
160
public void testQueries03() throws Exception {
161
String queryText = "w3 xx";
162
int[] expDocNrs = {2,3,1,0};
163
queriesTest(queryText, expDocNrs);
167
public void testQueries04() throws Exception {
168
String queryText = "w3 -xx";
169
int[] expDocNrs = {1,0};
170
queriesTest(queryText, expDocNrs);
174
public void testQueries05() throws Exception {
175
String queryText = "+w3 -xx";
176
int[] expDocNrs = {1,0};
177
queriesTest(queryText, expDocNrs);
181
public void testQueries06() throws Exception {
182
String queryText = "+w3 -xx -w5";
183
int[] expDocNrs = {1};
184
queriesTest(queryText, expDocNrs);
188
public void testQueries07() throws Exception {
189
String queryText = "-w3 -xx -w5";
190
int[] expDocNrs = {};
191
queriesTest(queryText, expDocNrs);
195
public void testQueries08() throws Exception {
196
String queryText = "+w3 xx -w5";
197
int[] expDocNrs = {2,3,1};
198
queriesTest(queryText, expDocNrs);
202
public void testQueries09() throws Exception {
203
String queryText = "+w3 +xx +w2 zz";
204
int[] expDocNrs = {2, 3};
205
queriesTest(queryText, expDocNrs);
209
public void testQueries10() throws Exception {
210
String queryText = "+w3 +xx +w2 zz";
211
int[] expDocNrs = {2, 3};
212
Similarity oldSimilarity = searcher.getSimilarity();
214
searcher.setSimilarity(new DefaultSimilarity(){
216
public float coord(int overlap, int maxOverlap) {
217
return overlap / ((float)maxOverlap - 1);
220
queriesTest(queryText, expDocNrs);
222
searcher.setSimilarity(oldSimilarity);
227
public void testRandomQueries() throws Exception {
228
String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};
232
BooleanQuery q1 = null;
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);
241
// Can't sort by relevance since floating point numbers may not quite
243
Sort sort = Sort.INDEXORDER;
245
QueryUtils.check(random, q1,searcher);
247
TopFieldCollector collector = TopFieldCollector.create(sort, 1000,
248
false, true, true, true);
250
searcher.search(q1, null, collector);
251
ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
253
collector = TopFieldCollector.create(sort, 1000,
254
false, true, true, false);
256
searcher.search(q1, null, collector);
257
ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
259
CheckHits.checkEqual(q1, hits1, hits2);
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);
268
} catch (Exception e) {
269
// For easier debugging
270
System.out.println("failed query: " + q1);
274
// System.out.println("Total hits:"+tot);
278
// used to set properties or change every BooleanQuery
279
// generated from randBoolQuery.
280
public static interface Callback {
281
public void postCreate(BooleanQuery q);
284
// Random rnd is passed in so that the exact same random query may be created
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
291
qType = rnd.nextInt(10);
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*"));
299
q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb);
302
int r = rnd.nextInt(10);
303
BooleanClause.Occur occur;
305
occur=BooleanClause.Occur.MUST_NOT;
309
occur=BooleanClause.Occur.MUST;
311
occur=BooleanClause.Occur.SHOULD;
314
occur=BooleanClause.Occur.SHOULD;
317
current.add(q, occur);
319
if (cb!=null) cb.postCreate(current);