add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / search / TestBooleanOr.java
1 package org.apache.lucene.search;
2 /**
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 import java.io.IOException;
19
20 import org.apache.lucene.util.LuceneTestCase;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.store.Directory;
28
29 public class TestBooleanOr extends LuceneTestCase {
30
31   private static String FIELD_T = "T";
32   private static String FIELD_C = "C";
33
34   private TermQuery t1 = new TermQuery(new Term(FIELD_T, "files"));
35   private TermQuery t2 = new TermQuery(new Term(FIELD_T, "deleting"));
36   private TermQuery c1 = new TermQuery(new Term(FIELD_C, "production"));
37   private TermQuery c2 = new TermQuery(new Term(FIELD_C, "optimize"));
38
39   private IndexSearcher searcher = null;
40   private Directory dir;
41   private IndexReader reader;
42   
43
44   private int search(Query q) throws IOException {
45     QueryUtils.check(random, q,searcher);
46     return searcher.search(q, null, 1000).totalHits;
47   }
48
49   public void testElements() throws IOException {
50     assertEquals(1, search(t1));
51     assertEquals(1, search(t2));
52     assertEquals(1, search(c1));
53     assertEquals(1, search(c2));
54   }
55
56   /**
57    * <code>T:files T:deleting C:production C:optimize </code>
58    * it works.
59    *
60    * @throws IOException
61    */
62   public void testFlat() throws IOException {
63     BooleanQuery q = new BooleanQuery();
64     q.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
65     q.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
66     q.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
67     q.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
68     assertEquals(1, search(q));
69   }
70
71   /**
72    * <code>(T:files T:deleting) (+C:production +C:optimize)</code>
73    * it works.
74    *
75    * @throws IOException
76    */
77   public void testParenthesisMust() throws IOException {
78     BooleanQuery q3 = new BooleanQuery();
79     q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
80     q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
81     BooleanQuery q4 = new BooleanQuery();
82     q4.add(new BooleanClause(c1, BooleanClause.Occur.MUST));
83     q4.add(new BooleanClause(c2, BooleanClause.Occur.MUST));
84     BooleanQuery q2 = new BooleanQuery();
85     q2.add(q3, BooleanClause.Occur.SHOULD);
86     q2.add(q4, BooleanClause.Occur.SHOULD);
87     assertEquals(1, search(q2));
88   }
89
90   /**
91    * <code>(T:files T:deleting) +(C:production C:optimize)</code>
92    * not working. results NO HIT.
93    *
94    * @throws IOException
95    */
96   public void testParenthesisMust2() throws IOException {
97     BooleanQuery q3 = new BooleanQuery();
98     q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
99     q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
100     BooleanQuery q4 = new BooleanQuery();
101     q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
102     q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
103     BooleanQuery q2 = new BooleanQuery();
104     q2.add(q3, BooleanClause.Occur.SHOULD);
105     q2.add(q4, BooleanClause.Occur.MUST);
106     assertEquals(1, search(q2));
107   }
108
109   /**
110    * <code>(T:files T:deleting) (C:production C:optimize)</code>
111    * not working. results NO HIT.
112    *
113    * @throws IOException
114    */
115   public void testParenthesisShould() throws IOException {
116     BooleanQuery q3 = new BooleanQuery();
117     q3.add(new BooleanClause(t1, BooleanClause.Occur.SHOULD));
118     q3.add(new BooleanClause(t2, BooleanClause.Occur.SHOULD));
119     BooleanQuery q4 = new BooleanQuery();
120     q4.add(new BooleanClause(c1, BooleanClause.Occur.SHOULD));
121     q4.add(new BooleanClause(c2, BooleanClause.Occur.SHOULD));
122     BooleanQuery q2 = new BooleanQuery();
123     q2.add(q3, BooleanClause.Occur.SHOULD);
124     q2.add(q4, BooleanClause.Occur.SHOULD);
125     assertEquals(1, search(q2));
126   }
127
128   @Override
129   public void setUp() throws Exception {
130     super.setUp();
131
132     //
133     dir = newDirectory();
134
135
136     //
137     RandomIndexWriter writer = new RandomIndexWriter(random, dir);
138
139     //
140     Document d = new Document();
141     d.add(newField(
142         FIELD_T,
143         "Optimize not deleting all files",
144         Field.Store.YES,
145         Field.Index.ANALYZED));
146     d.add(newField(
147         FIELD_C,
148         "Deleted When I run an optimize in our production environment.",
149         Field.Store.YES,
150         Field.Index.ANALYZED));
151
152     //
153     writer.addDocument(d);
154
155     reader = writer.getReader();
156     //
157     searcher = newSearcher(reader);
158     writer.close();
159   }
160
161   @Override
162   public void tearDown() throws Exception {
163     searcher.close();
164     reader.close();
165     dir.close();
166     super.tearDown();
167   }
168 }