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.
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.TimeUnit;
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.analysis.WhitespaceAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.MultiReader;
30 import org.apache.lucene.index.RandomIndexWriter;
31 import org.apache.lucene.index.Term;
32 import org.apache.lucene.queryParser.QueryParser;
33 import org.apache.lucene.store.Directory;
34 import org.apache.lucene.util.LuceneTestCase;
36 public class TestBooleanQuery extends LuceneTestCase {
38 public void testEquality() throws Exception {
39 BooleanQuery bq1 = new BooleanQuery();
40 bq1.add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
41 bq1.add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
42 BooleanQuery nested1 = new BooleanQuery();
43 nested1.add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
44 nested1.add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
45 bq1.add(nested1, BooleanClause.Occur.SHOULD);
47 BooleanQuery bq2 = new BooleanQuery();
48 bq2.add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.SHOULD);
49 bq2.add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.SHOULD);
50 BooleanQuery nested2 = new BooleanQuery();
51 nested2.add(new TermQuery(new Term("field", "nestedvalue1")), BooleanClause.Occur.SHOULD);
52 nested2.add(new TermQuery(new Term("field", "nestedvalue2")), BooleanClause.Occur.SHOULD);
53 bq2.add(nested2, BooleanClause.Occur.SHOULD);
55 assertEquals(bq1, bq2);
58 public void testException() {
60 BooleanQuery.setMaxClauseCount(0);
62 } catch (IllegalArgumentException e) {
68 public void testNullOrSubScorer() throws Throwable {
69 Directory dir = newDirectory();
70 RandomIndexWriter w = new RandomIndexWriter(random, dir);
71 Document doc = new Document();
72 doc.add(newField("field", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
75 IndexReader r = w.getReader();
76 IndexSearcher s = newSearcher(r);
77 BooleanQuery q = new BooleanQuery();
78 q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
80 // LUCENE-2617: make sure that a term not in the index still contributes to the score via coord factor
81 float score = s.search(q, 10).getMaxScore();
82 Query subQuery = new TermQuery(new Term("field", "not_in_index"));
84 q.add(subQuery, BooleanClause.Occur.SHOULD);
85 float score2 = s.search(q, 10).getMaxScore();
86 assertEquals(score*.5, score2, 1e-6);
88 // now test BooleanScorer2
89 subQuery = new TermQuery(new Term("field", "b"));
91 q.add(subQuery, BooleanClause.Occur.MUST);
92 score2 = s.search(q, 10).getMaxScore();
93 assertEquals(score*(2.0/3), score2, 1e-6);
95 // PhraseQuery w/ no terms added returns a null scorer
96 PhraseQuery pq = new PhraseQuery();
97 q.add(pq, BooleanClause.Occur.SHOULD);
98 assertEquals(1, s.search(q, 10).totalHits);
100 // A required clause which returns null scorer should return null scorer to
102 q = new BooleanQuery();
103 pq = new PhraseQuery();
104 q.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
105 q.add(pq, BooleanClause.Occur.MUST);
106 assertEquals(0, s.search(q, 10).totalHits);
108 DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);
109 dmq.add(new TermQuery(new Term("field", "a")));
111 assertEquals(1, s.search(dmq, 10).totalHits);
119 public void testDeMorgan() throws Exception {
120 Directory dir1 = newDirectory();
121 RandomIndexWriter iw1 = new RandomIndexWriter(random, dir1);
122 Document doc1 = new Document();
123 doc1.add(newField("field", "foo bar", Field.Index.ANALYZED));
124 iw1.addDocument(doc1);
125 IndexReader reader1 = iw1.getReader();
128 Directory dir2 = newDirectory();
129 RandomIndexWriter iw2 = new RandomIndexWriter(random, dir2);
130 Document doc2 = new Document();
131 doc2.add(newField("field", "foo baz", Field.Index.ANALYZED));
132 iw2.addDocument(doc2);
133 IndexReader reader2 = iw2.getReader();
136 QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
137 qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
139 MultiReader multireader = new MultiReader(reader1, reader2);
140 IndexSearcher searcher = newSearcher(multireader);
141 assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
144 final ExecutorService es = Executors.newCachedThreadPool();
145 searcher = new IndexSearcher(multireader, es);
147 System.out.println("rewritten form: " + searcher.rewrite(qp.parse("+foo -ba*")));
148 assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
150 es.awaitTermination(1, TimeUnit.SECONDS);