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 org.apache.lucene.search.BooleanClause.Occur;
21 import org.apache.lucene.search.spans.*;
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.
28 public class TestComplexExplanations extends TestExplanations {
31 * Override the Similarity used in our searcher with one that plays
32 * nice with boosts of 0.0
35 public void setUp() throws Exception {
37 searcher.setSimilarity(createQnorm1Similarity());
41 public void tearDown() throws Exception {
46 // must be static for weight serialization tests
47 private static DefaultSimilarity createQnorm1Similarity() {
48 return new DefaultSimilarity() {
50 public float queryNorm(float sumOfSquaredWeights) {
51 return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);
57 public void test1() throws Exception {
59 BooleanQuery q = new BooleanQuery();
61 q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
66 q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
69 Query t = new FilteredQuery(qp.parse("xx"),
70 new ItemizedFilter(new int[] {1,3}));
72 q.add(t, Occur.SHOULD);
74 t = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
76 q.add(t, Occur.SHOULD);
78 DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
79 dm.add(snear(st("w2"),
82 dm.add(qp.parse("QQ"));
83 dm.add(qp.parse("xx yy -zz"));
84 dm.add(qp.parse("-xx -w1"));
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"));
92 q.add(dm, Occur.SHOULD);
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);
100 q.add(b, Occur.SHOULD);
102 qtest(q, new int[] { 0,1,2 });
105 public void test2() throws Exception {
107 BooleanQuery q = new BooleanQuery();
109 q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
110 q.add(snear(st("w2"),
114 q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
117 Query t = new FilteredQuery(qp.parse("xx"),
118 new ItemizedFilter(new int[] {1,3}));
120 q.add(t, Occur.SHOULD);
122 t = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
124 q.add(t, Occur.SHOULD);
126 DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
127 dm.add(snear(st("w2"),
130 dm.add(qp.parse("QQ"));
131 dm.add(qp.parse("xx yy -zz"));
132 dm.add(qp.parse("-xx -w1"));
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"));
140 q.add(dm, Occur.SHOULD);
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);
149 q.add(b, Occur.SHOULD);
151 qtest(q, new int[] { 0,1,2 });
154 // :TODO: we really need more crazy complex cases.
157 // //////////////////////////////////////////////////////////////////
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
163 public void testT3() throws Exception {
164 bqtest("w1^0.0", new int[] { 0,1,2,3 });
167 public void testMA3() throws Exception {
168 Query q=new MatchAllDocsQuery();
170 bqtest(q, new int[] { 0,1,2,3 });
173 public void testFQ5() throws Exception {
174 bqtest(new FilteredQuery(qp.parse("xx^0"),
175 new ItemizedFilter(new int[] {1,3})),
179 public void testCSQ4() throws Exception {
180 Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {3}));
182 bqtest(q, new int[] {3});
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"));
190 bqtest(q, new int[] { 0,2,3 });
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"}));
199 bqtest(q, new int[] { 0,1,2 });
202 public void testBQ12() throws Exception {
203 // NOTE: using qtest not bqtest
204 qtest("w1 w2^0.0", new int[] { 0,1,2,3 });
206 public void testBQ13() throws Exception {
207 // NOTE: using qtest not bqtest
208 qtest("w1 -w5^0.0", new int[] { 1,2,3 });
210 public void testBQ18() throws Exception {
211 // NOTE: using qtest not bqtest
212 qtest("+w1^0.0 w2", new int[] { 0,1,2,3 });
214 public void testBQ21() throws Exception {
215 bqtest("(+w1 w2)^0.0", new int[] { 0,1,2,3 });
217 public void testBQ22() throws Exception {
218 bqtest("(+w1^0.0 w2)^0.0", new int[] { 0,1,2,3 });
221 public void testST3() throws Exception {
222 SpanQuery q = st("w1");
224 bqtest(q, new int[] {0,1,2,3});
226 public void testST6() throws Exception {
227 SpanQuery q = st("xx");
229 qtest(q, new int[] {2,3});
232 public void testSF3() throws Exception {
233 SpanQuery q = sf(("w1"),1);
235 bqtest(q, new int[] {0,1,2,3});
237 public void testSF7() throws Exception {
238 SpanQuery q = sf(("xx"),3);
240 bqtest(q, new int[] {2,3});
243 public void testSNot3() throws Exception {
244 SpanQuery q = snot(sf("w1",10),st("QQ"));
246 bqtest(q, new int[] {0,1,2,3});
248 public void testSNot6() throws Exception {
249 SpanQuery q = snot(sf("w1",10),st("xx"));
251 bqtest(q, new int[] {0,1,2,3});
254 public void testSNot8() throws Exception {
255 // NOTE: using qtest not bqtest
256 SpanQuery f = snear("w1","w3",10,true);
258 SpanQuery q = snot(f, st("xx"));
259 qtest(q, new int[] {0,1,3});
261 public void testSNot9() throws Exception {
262 // NOTE: using qtest not bqtest
263 SpanQuery t = st("xx");
265 SpanQuery q = snot(snear("w1","w3",10,true), t);
266 qtest(q, new int[] {0,1,3});