001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: /**
021: * TestExplanations subclass focusing on basic query types
022: */
023: public class TestSimpleExplanations extends TestExplanations {
024:
025: // we focus on queries that don't rewrite to other queries.
026: // if we get those covered well, then the ones that rewrite should
027: // also be covered.
028:
029: /* simple term tests */
030:
031: public void testT1() throws Exception {
032: qtest("w1", new int[] { 0, 1, 2, 3 });
033: }
034:
035: public void testT2() throws Exception {
036: qtest("w1^1000", new int[] { 0, 1, 2, 3 });
037: }
038:
039: /* MatchAllDocs */
040:
041: public void testMA1() throws Exception {
042: qtest(new MatchAllDocsQuery(), new int[] { 0, 1, 2, 3 });
043: }
044:
045: public void testMA2() throws Exception {
046: Query q = new MatchAllDocsQuery();
047: q.setBoost(1000);
048: qtest(q, new int[] { 0, 1, 2, 3 });
049: }
050:
051: /* some simple phrase tests */
052:
053: public void testP1() throws Exception {
054: qtest("\"w1 w2\"", new int[] { 0 });
055: }
056:
057: public void testP2() throws Exception {
058: qtest("\"w1 w3\"", new int[] { 1, 3 });
059: }
060:
061: public void testP3() throws Exception {
062: qtest("\"w1 w2\"~1", new int[] { 0, 1, 2 });
063: }
064:
065: public void testP4() throws Exception {
066: qtest("\"w2 w3\"~1", new int[] { 0, 1, 2, 3 });
067: }
068:
069: public void testP5() throws Exception {
070: qtest("\"w3 w2\"~1", new int[] { 1, 3 });
071: }
072:
073: public void testP6() throws Exception {
074: qtest("\"w3 w2\"~2", new int[] { 0, 1, 3 });
075: }
076:
077: public void testP7() throws Exception {
078: qtest("\"w3 w2\"~3", new int[] { 0, 1, 2, 3 });
079: }
080:
081: /* some simple filtered query tests */
082:
083: public void testFQ1() throws Exception {
084: qtest(new FilteredQuery(qp.parse("w1"), new ItemizedFilter(
085: new int[] { 0, 1, 2, 3 })), new int[] { 0, 1, 2, 3 });
086: }
087:
088: public void testFQ2() throws Exception {
089: qtest(new FilteredQuery(qp.parse("w1"), new ItemizedFilter(
090: new int[] { 0, 2, 3 })), new int[] { 0, 2, 3 });
091: }
092:
093: public void testFQ3() throws Exception {
094: qtest(new FilteredQuery(qp.parse("xx"), new ItemizedFilter(
095: new int[] { 1, 3 })), new int[] { 3 });
096: }
097:
098: public void testFQ4() throws Exception {
099: qtest(new FilteredQuery(qp.parse("xx^1000"),
100: new ItemizedFilter(new int[] { 1, 3 })),
101: new int[] { 3 });
102: }
103:
104: public void testFQ6() throws Exception {
105: Query q = new FilteredQuery(qp.parse("xx"), new ItemizedFilter(
106: new int[] { 1, 3 }));
107: q.setBoost(1000);
108: qtest(q, new int[] { 3 });
109: }
110:
111: /* ConstantScoreQueries */
112:
113: public void testCSQ1() throws Exception {
114: Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {
115: 0, 1, 2, 3 }));
116: qtest(q, new int[] { 0, 1, 2, 3 });
117: }
118:
119: public void testCSQ2() throws Exception {
120: Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {
121: 1, 3 }));
122: qtest(q, new int[] { 1, 3 });
123: }
124:
125: public void testCSQ3() throws Exception {
126: Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {
127: 0, 2 }));
128: q.setBoost(1000);
129: qtest(q, new int[] { 0, 2 });
130: }
131:
132: /* DisjunctionMaxQuery */
133:
134: public void testDMQ1() throws Exception {
135: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
136: q.add(qp.parse("w1"));
137: q.add(qp.parse("w5"));
138: qtest(q, new int[] { 0, 1, 2, 3 });
139: }
140:
141: public void testDMQ2() throws Exception {
142: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
143: q.add(qp.parse("w1"));
144: q.add(qp.parse("w5"));
145: qtest(q, new int[] { 0, 1, 2, 3 });
146: }
147:
148: public void testDMQ3() throws Exception {
149: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
150: q.add(qp.parse("QQ"));
151: q.add(qp.parse("w5"));
152: qtest(q, new int[] { 0 });
153: }
154:
155: public void testDMQ4() throws Exception {
156: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
157: q.add(qp.parse("QQ"));
158: q.add(qp.parse("xx"));
159: qtest(q, new int[] { 2, 3 });
160: }
161:
162: public void testDMQ5() throws Exception {
163: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
164: q.add(qp.parse("yy -QQ"));
165: q.add(qp.parse("xx"));
166: qtest(q, new int[] { 2, 3 });
167: }
168:
169: public void testDMQ6() throws Exception {
170: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
171: q.add(qp.parse("-yy w3"));
172: q.add(qp.parse("xx"));
173: qtest(q, new int[] { 0, 1, 2, 3 });
174: }
175:
176: public void testDMQ7() throws Exception {
177: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
178: q.add(qp.parse("-yy w3"));
179: q.add(qp.parse("w2"));
180: qtest(q, new int[] { 0, 1, 2, 3 });
181: }
182:
183: public void testDMQ8() throws Exception {
184: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
185: q.add(qp.parse("yy w5^100"));
186: q.add(qp.parse("xx^100000"));
187: qtest(q, new int[] { 0, 2, 3 });
188: }
189:
190: public void testDMQ9() throws Exception {
191: DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
192: q.add(qp.parse("yy w5^100"));
193: q.add(qp.parse("xx^0"));
194: qtest(q, new int[] { 0, 2, 3 });
195: }
196:
197: /* MultiPhraseQuery */
198:
199: public void testMPQ1() throws Exception {
200: MultiPhraseQuery q = new MultiPhraseQuery();
201: q.add(ta(new String[] { "w1" }));
202: q.add(ta(new String[] { "w2", "w3", "xx" }));
203: qtest(q, new int[] { 0, 1, 2, 3 });
204: }
205:
206: public void testMPQ2() throws Exception {
207: MultiPhraseQuery q = new MultiPhraseQuery();
208: q.add(ta(new String[] { "w1" }));
209: q.add(ta(new String[] { "w2", "w3" }));
210: qtest(q, new int[] { 0, 1, 3 });
211: }
212:
213: public void testMPQ3() throws Exception {
214: MultiPhraseQuery q = new MultiPhraseQuery();
215: q.add(ta(new String[] { "w1", "xx" }));
216: q.add(ta(new String[] { "w2", "w3" }));
217: qtest(q, new int[] { 0, 1, 2, 3 });
218: }
219:
220: public void testMPQ4() throws Exception {
221: MultiPhraseQuery q = new MultiPhraseQuery();
222: q.add(ta(new String[] { "w1" }));
223: q.add(ta(new String[] { "w2" }));
224: qtest(q, new int[] { 0 });
225: }
226:
227: public void testMPQ5() throws Exception {
228: MultiPhraseQuery q = new MultiPhraseQuery();
229: q.add(ta(new String[] { "w1" }));
230: q.add(ta(new String[] { "w2" }));
231: q.setSlop(1);
232: qtest(q, new int[] { 0, 1, 2 });
233: }
234:
235: public void testMPQ6() throws Exception {
236: MultiPhraseQuery q = new MultiPhraseQuery();
237: q.add(ta(new String[] { "w1", "w3" }));
238: q.add(ta(new String[] { "w2" }));
239: q.setSlop(1);
240: qtest(q, new int[] { 0, 1, 2, 3 });
241: }
242:
243: /* some simple tests of boolean queries containing term queries */
244:
245: public void testBQ1() throws Exception {
246: qtest("+w1 +w2", new int[] { 0, 1, 2, 3 });
247: }
248:
249: public void testBQ2() throws Exception {
250: qtest("+yy +w3", new int[] { 2, 3 });
251: }
252:
253: public void testBQ3() throws Exception {
254: qtest("yy +w3", new int[] { 0, 1, 2, 3 });
255: }
256:
257: public void testBQ4() throws Exception {
258: qtest("w1 (-xx w2)", new int[] { 0, 1, 2, 3 });
259: }
260:
261: public void testBQ5() throws Exception {
262: qtest("w1 (+qq w2)", new int[] { 0, 1, 2, 3 });
263: }
264:
265: public void testBQ6() throws Exception {
266: qtest("w1 -(-qq w5)", new int[] { 1, 2, 3 });
267: }
268:
269: public void testBQ7() throws Exception {
270: qtest("+w1 +(qq (xx -w2) (+w3 +w4))", new int[] { 0 });
271: }
272:
273: public void testBQ8() throws Exception {
274: qtest("+w1 (qq (xx -w2) (+w3 +w4))", new int[] { 0, 1, 2, 3 });
275: }
276:
277: public void testBQ9() throws Exception {
278: qtest("+w1 (qq (-xx w2) -(+w3 +w4))", new int[] { 0, 1, 2, 3 });
279: }
280:
281: public void testBQ10() throws Exception {
282: qtest("+w1 +(qq (-xx w2) -(+w3 +w4))", new int[] { 1 });
283: }
284:
285: public void testBQ11() throws Exception {
286: qtest("w1 w2^1000.0", new int[] { 0, 1, 2, 3 });
287: }
288:
289: public void testBQ14() throws Exception {
290: BooleanQuery q = new BooleanQuery(true);
291: q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
292: q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
293: qtest(q, new int[] { 0, 1, 2, 3 });
294: }
295:
296: public void testBQ15() throws Exception {
297: BooleanQuery q = new BooleanQuery(true);
298: q.add(qp.parse("QQQQQ"), BooleanClause.Occur.MUST_NOT);
299: q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
300: qtest(q, new int[] { 0, 1, 2, 3 });
301: }
302:
303: public void testBQ16() throws Exception {
304: BooleanQuery q = new BooleanQuery(true);
305: q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
306: q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
307: qtest(q, new int[] { 0, 1 });
308: }
309:
310: public void testBQ17() throws Exception {
311: BooleanQuery q = new BooleanQuery(true);
312: q.add(qp.parse("w2"), BooleanClause.Occur.SHOULD);
313: q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
314: qtest(q, new int[] { 0, 1, 2, 3 });
315: }
316:
317: public void testBQ19() throws Exception {
318: qtest("-yy w3", new int[] { 0, 1 });
319: }
320:
321: public void testBQ20() throws Exception {
322: BooleanQuery q = new BooleanQuery();
323: q.setMinimumNumberShouldMatch(2);
324: q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
325: q.add(qp.parse("yy"), BooleanClause.Occur.SHOULD);
326: q.add(qp.parse("zz"), BooleanClause.Occur.SHOULD);
327: q.add(qp.parse("w5"), BooleanClause.Occur.SHOULD);
328: q.add(qp.parse("w4"), BooleanClause.Occur.SHOULD);
329:
330: qtest(q, new int[] { 0, 3 });
331:
332: }
333:
334: }
|