pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestComplexExplanations.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 org.apache.lucene.search.BooleanClause.Occur;
21 import org.apache.lucene.search.spans.*;
22
23 /**
24  * TestExplanations subclass that builds up super crazy complex queries
25  * on the assumption that if the explanations work out right for them,
26  * they should work for anything.
27  */
28 public class TestComplexExplanations extends TestExplanations {
29
30   /**
31    * Override the Similarity used in our searcher with one that plays
32    * nice with boosts of 0.0
33    */
34   @Override
35   public void setUp() throws Exception {
36     super.setUp();
37     searcher.setSimilarity(createQnorm1Similarity());
38   }
39   
40   @Override
41   public void tearDown() throws Exception {
42     searcher.close();
43     super.tearDown();
44   }
45
46   // must be static for weight serialization tests 
47   private static DefaultSimilarity createQnorm1Similarity() {
48     return new DefaultSimilarity() {
49         @Override
50         public float queryNorm(float sumOfSquaredWeights) {
51           return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);
52         }
53       };
54   }
55
56   
57   public void test1() throws Exception {
58     
59     BooleanQuery q = new BooleanQuery();
60     
61     q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
62     q.add(snear(st("w2"),
63                 sor("w5","zz"),
64                 4, true),
65           Occur.SHOULD);
66     q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
67           Occur.SHOULD);
68     
69     Query t = new FilteredQuery(qp.parse("xx"),
70                                 new ItemizedFilter(new int[] {1,3}));
71     t.setBoost(1000);
72     q.add(t, Occur.SHOULD);
73     
74     t = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
75     t.setBoost(30);
76     q.add(t, Occur.SHOULD);
77     
78     DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
79     dm.add(snear(st("w2"),
80                  sor("w5","zz"),
81                  4, true));
82     dm.add(qp.parse("QQ"));
83     dm.add(qp.parse("xx yy -zz"));
84     dm.add(qp.parse("-xx -w1"));
85
86     DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
87     dm2.add(qp.parse("w1"));
88     dm2.add(qp.parse("w2"));
89     dm2.add(qp.parse("w3"));
90     dm.add(dm2);
91
92     q.add(dm, Occur.SHOULD);
93
94     BooleanQuery b = new BooleanQuery();
95     b.setMinimumNumberShouldMatch(2);
96     b.add(snear("w1","w2",1,true), Occur.SHOULD);
97     b.add(snear("w2","w3",1,true), Occur.SHOULD);
98     b.add(snear("w1","w3",3,true), Occur.SHOULD);
99
100     q.add(b, Occur.SHOULD);
101     
102     qtest(q, new int[] { 0,1,2 });
103   }
104
105   public void test2() throws Exception {
106     
107     BooleanQuery q = new BooleanQuery();
108     
109     q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
110     q.add(snear(st("w2"),
111                 sor("w5","zz"),
112                 4, true),
113           Occur.SHOULD);
114     q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
115           Occur.SHOULD);
116     
117     Query t = new FilteredQuery(qp.parse("xx"),
118                                 new ItemizedFilter(new int[] {1,3}));
119     t.setBoost(1000);
120     q.add(t, Occur.SHOULD);
121     
122     t = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
123     t.setBoost(-20.0f);
124     q.add(t, Occur.SHOULD);
125     
126     DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
127     dm.add(snear(st("w2"),
128                  sor("w5","zz"),
129                  4, true));
130     dm.add(qp.parse("QQ"));
131     dm.add(qp.parse("xx yy -zz"));
132     dm.add(qp.parse("-xx -w1"));
133
134     DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
135     dm2.add(qp.parse("w1"));
136     dm2.add(qp.parse("w2"));
137     dm2.add(qp.parse("w3"));
138     dm.add(dm2);
139
140     q.add(dm, Occur.SHOULD);
141
142     BooleanQuery b = new BooleanQuery();
143     b.setMinimumNumberShouldMatch(2);
144     b.add(snear("w1","w2",1,true), Occur.SHOULD);
145     b.add(snear("w2","w3",1,true), Occur.SHOULD);
146     b.add(snear("w1","w3",3,true), Occur.SHOULD);
147     b.setBoost(0.0f);
148     
149     q.add(b, Occur.SHOULD);
150     
151     qtest(q, new int[] { 0,1,2 });
152   }
153   
154   // :TODO: we really need more crazy complex cases.
155
156
157   // //////////////////////////////////////////////////////////////////
158
159   // The rest of these aren't that complex, but they are <i>somewhat</i>
160   // complex, and they expose weakness in dealing with queries that match
161   // with scores of 0 wrapped in other queries
162
163   public void testT3() throws Exception {
164     bqtest("w1^0.0", new int[] { 0,1,2,3 });
165   }
166
167   public void testMA3() throws Exception {
168     Query q=new MatchAllDocsQuery();
169     q.setBoost(0);
170     bqtest(q, new int[] { 0,1,2,3 });
171   }
172   
173   public void testFQ5() throws Exception {
174     bqtest(new FilteredQuery(qp.parse("xx^0"),
175                              new ItemizedFilter(new int[] {1,3})),
176            new int[] {3});
177   }
178   
179   public void testCSQ4() throws Exception {
180     Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {3}));
181     q.setBoost(0);
182     bqtest(q, new int[] {3});
183   }
184   
185   public void testDMQ10() throws Exception {
186     DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
187     q.add(qp.parse("yy w5^100"));
188     q.add(qp.parse("xx^0"));
189     q.setBoost(0.0f);
190     bqtest(q, new int[] { 0,2,3 });
191   }
192   
193   public void testMPQ7() throws Exception {
194     MultiPhraseQuery q = new MultiPhraseQuery();
195     q.add(ta(new String[] {"w1"}));
196     q.add(ta(new String[] {"w2"}));
197     q.setSlop(1);
198     q.setBoost(0.0f);
199     bqtest(q, new int[] { 0,1,2 });
200   }
201   
202   public void testBQ12() throws Exception {
203     // NOTE: using qtest not bqtest
204     qtest("w1 w2^0.0", new int[] { 0,1,2,3 });
205   }
206   public void testBQ13() throws Exception {
207     // NOTE: using qtest not bqtest
208     qtest("w1 -w5^0.0", new int[] { 1,2,3 });
209   }
210   public void testBQ18() throws Exception {
211     // NOTE: using qtest not bqtest
212     qtest("+w1^0.0 w2", new int[] { 0,1,2,3 });
213   }
214   public void testBQ21() throws Exception {
215     bqtest("(+w1 w2)^0.0", new int[] { 0,1,2,3 });
216   }
217   public void testBQ22() throws Exception {
218     bqtest("(+w1^0.0 w2)^0.0", new int[] { 0,1,2,3 });
219   }
220
221   public void testST3() throws Exception {
222     SpanQuery q = st("w1");
223     q.setBoost(0);
224     bqtest(q, new int[] {0,1,2,3});
225   }
226   public void testST6() throws Exception {
227     SpanQuery q = st("xx");
228     q.setBoost(0);
229     qtest(q, new int[] {2,3});
230   }
231
232   public void testSF3() throws Exception {
233     SpanQuery q = sf(("w1"),1);
234     q.setBoost(0);
235     bqtest(q, new int[] {0,1,2,3});
236   }
237   public void testSF7() throws Exception {
238     SpanQuery q = sf(("xx"),3);
239     q.setBoost(0);
240     bqtest(q, new int[] {2,3});
241   }
242   
243   public void testSNot3() throws Exception {
244     SpanQuery q = snot(sf("w1",10),st("QQ"));
245     q.setBoost(0);
246     bqtest(q, new int[] {0,1,2,3});
247   }
248   public void testSNot6() throws Exception {
249     SpanQuery q = snot(sf("w1",10),st("xx"));
250     q.setBoost(0);
251     bqtest(q, new int[] {0,1,2,3});
252   }
253
254   public void testSNot8() throws Exception {
255     // NOTE: using qtest not bqtest
256     SpanQuery f = snear("w1","w3",10,true);
257     f.setBoost(0);
258     SpanQuery q = snot(f, st("xx"));
259     qtest(q, new int[] {0,1,3});
260   }
261   public void testSNot9() throws Exception {
262     // NOTE: using qtest not bqtest
263     SpanQuery t = st("xx");
264     t.setBoost(0);
265     SpanQuery q = snot(snear("w1","w3",10,true), t);
266     qtest(q, new int[] {0,1,3});
267   }
268
269
270   
271
272   
273 }