add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestBooleanQuery.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 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.TimeUnit;
23
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;
35
36 public class TestBooleanQuery extends LuceneTestCase {
37   
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);
46
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);
54
55     assertEquals(bq1, bq2);
56   }
57
58   public void testException() {
59     try {
60       BooleanQuery.setMaxClauseCount(0);
61       fail();
62     } catch (IllegalArgumentException e) {
63       // okay
64     }
65   }
66
67   // LUCENE-1630
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));
73     w.addDocument(doc);
74
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);
79
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"));
83     subQuery.setBoost(0);
84     q.add(subQuery, BooleanClause.Occur.SHOULD);
85     float score2 = s.search(q, 10).getMaxScore();
86     assertEquals(score*.5, score2, 1e-6);
87
88     // now test BooleanScorer2
89     subQuery = new TermQuery(new Term("field", "b"));
90     subQuery.setBoost(0);
91     q.add(subQuery, BooleanClause.Occur.MUST);
92     score2 = s.search(q, 10).getMaxScore();
93     assertEquals(score*(2.0/3), score2, 1e-6);
94  
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);
99
100     // A required clause which returns null scorer should return null scorer to
101     // IndexSearcher.
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);
107
108     DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);
109     dmq.add(new TermQuery(new Term("field", "a")));
110     dmq.add(pq);
111     assertEquals(1, s.search(dmq, 10).totalHits);
112     
113     s.close();
114     r.close();
115     w.close();
116     dir.close();
117   }
118   
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();
126     iw1.close();
127     
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();
134     iw2.close();
135     
136     QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
137     qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
138     
139     MultiReader multireader = new MultiReader(reader1, reader2);
140     IndexSearcher searcher = newSearcher(multireader);
141     assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
142     searcher.close();
143     
144     final ExecutorService es = Executors.newCachedThreadPool();
145     searcher = new IndexSearcher(multireader, es);
146     if (VERBOSE)
147       System.out.println("rewritten form: " + searcher.rewrite(qp.parse("+foo -ba*")));
148     assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
149     es.shutdown();
150     es.awaitTermination(1, TimeUnit.SECONDS);
151
152     multireader.close();
153     reader1.close();
154     reader2.close();
155     dir1.close();
156     dir2.close();
157   }
158 }