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.util.LuceneTestCase;
21 import org.apache.lucene.analysis.MockAnalyzer;
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.FieldInvertState;
26 import org.apache.lucene.index.RandomIndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.store.Directory;
30 import java.text.DecimalFormat;
31 import java.io.IOException;
34 * Test of the DisjunctionMaxQuery.
37 public class TestDisjunctionMaxQuery extends LuceneTestCase {
39 /** threshold for comparing floats */
40 public static final float SCORE_COMP_THRESH = 0.0000f;
43 * Similarity to eliminate tf, idf and lengthNorm effects to isolate test
47 * same as TestRankingSimilarity in TestRanking.zip from
48 * http://issues.apache.org/jira/browse/LUCENE-323
51 private static class TestSimilarity extends DefaultSimilarity {
53 public TestSimilarity() {}
56 public float tf(float freq) {
57 if (freq > 0.0f) return 1.0f;
62 public float computeNorm(String fieldName, FieldInvertState state) {
63 // Disable length norm
64 return state.getBoost();
68 public float idf(int docFreq, int numDocs) {
73 public Similarity sim = new TestSimilarity();
74 public Directory index;
76 public IndexSearcher s;
79 public void setUp() throws Exception {
82 index = newDirectory();
83 RandomIndexWriter writer = new RandomIndexWriter(random, index,
84 newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))
85 .setSimilarity(sim).setMergePolicy(newLogMergePolicy()));
87 // hed is the most important field, dek is secondary
89 // d1 is an "ok" match for: albino elephant
91 Document d1 = new Document();
92 d1.add(newField("id", "d1", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id",
95 .add(newField("hed", "elephant", Field.Store.YES,
96 Field.Index.ANALYZED));// Field.Text("hed", "elephant"));
98 .add(newField("dek", "elephant", Field.Store.YES,
99 Field.Index.ANALYZED));// Field.Text("dek", "elephant"));
100 writer.addDocument(d1);
103 // d2 is a "good" match for: albino elephant
105 Document d2 = new Document();
106 d2.add(newField("id", "d2", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id",
109 .add(newField("hed", "elephant", Field.Store.YES,
110 Field.Index.ANALYZED));// Field.Text("hed", "elephant"));
111 d2.add(newField("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("dek",
114 .add(newField("dek", "elephant", Field.Store.YES,
115 Field.Index.ANALYZED));// Field.Text("dek", "elephant"));
116 writer.addDocument(d2);
119 // d3 is a "better" match for: albino elephant
121 Document d3 = new Document();
122 d3.add(newField("id", "d3", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id",
124 d3.add(newField("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("hed",
127 .add(newField("hed", "elephant", Field.Store.YES,
128 Field.Index.ANALYZED));// Field.Text("hed", "elephant"));
129 writer.addDocument(d3);
132 // d4 is the "best" match for: albino elephant
134 Document d4 = new Document();
135 d4.add(newField("id", "d4", Field.Store.YES, Field.Index.NOT_ANALYZED));// Field.Keyword("id",
137 d4.add(newField("hed", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("hed",
140 .add(newField("hed", "elephant", Field.Store.YES,
141 Field.Index.ANALYZED));// Field.Text("hed", "elephant"));
142 d4.add(newField("dek", "albino", Field.Store.YES, Field.Index.ANALYZED));// Field.Text("dek",
144 writer.addDocument(d4);
147 writer.forceMerge(1);
148 r = writer.getReader();
151 s.setSimilarity(sim);
155 public void tearDown() throws Exception {
162 public void testSkipToFirsttimeMiss() throws IOException {
163 final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
164 dq.add(tq("id", "d1"));
165 dq.add(tq("dek", "DOES_NOT_EXIST"));
167 QueryUtils.check(random, dq, s);
169 final Weight dw = s.createNormalizedWeight(dq);
170 IndexReader sub = s.getIndexReader().getSequentialSubReaders() == null ?
171 s.getIndexReader() : s.getIndexReader().getSequentialSubReaders()[0];
172 final Scorer ds = dw.scorer(sub, true, false);
173 final boolean skipOk = ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS;
175 fail("firsttime skipTo found a match? ... "
176 + r.document(ds.docID()).get("id"));
180 public void testSkipToFirsttimeHit() throws IOException {
181 final DisjunctionMaxQuery dq = new DisjunctionMaxQuery(0.0f);
182 dq.add(tq("dek", "albino"));
183 dq.add(tq("dek", "DOES_NOT_EXIST"));
185 QueryUtils.check(random, dq, s);
187 final Weight dw = s.createNormalizedWeight(dq);
188 IndexReader sub = s.getIndexReader().getSequentialSubReaders() == null ?
189 s.getIndexReader() : s.getIndexReader().getSequentialSubReaders()[0];
190 final Scorer ds = dw.scorer(sub, true, false);
191 assertTrue("firsttime skipTo found no match",
192 ds.advance(3) != DocIdSetIterator.NO_MORE_DOCS);
193 assertEquals("found wrong docid", "d4", r.document(ds.docID()).get("id"));
196 public void testSimpleEqualScores1() throws Exception {
198 DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
199 q.add(tq("hed", "albino"));
200 q.add(tq("hed", "elephant"));
201 QueryUtils.check(random, q, s);
203 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
206 assertEquals("all docs should match " + q.toString(), 4, h.length);
208 float score = h[0].score;
209 for (int i = 1; i < h.length; i++) {
210 assertEquals("score #" + i + " is not the same", score, h[i].score,
214 printHits("testSimpleEqualScores1", h, s);
220 public void testSimpleEqualScores2() throws Exception {
222 DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
223 q.add(tq("dek", "albino"));
224 q.add(tq("dek", "elephant"));
225 QueryUtils.check(random, q, s);
227 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
230 assertEquals("3 docs should match " + q.toString(), 3, h.length);
231 float score = h[0].score;
232 for (int i = 1; i < h.length; i++) {
233 assertEquals("score #" + i + " is not the same", score, h[i].score,
237 printHits("testSimpleEqualScores2", h, s);
243 public void testSimpleEqualScores3() throws Exception {
245 DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
246 q.add(tq("hed", "albino"));
247 q.add(tq("hed", "elephant"));
248 q.add(tq("dek", "albino"));
249 q.add(tq("dek", "elephant"));
250 QueryUtils.check(random, q, s);
252 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
255 assertEquals("all docs should match " + q.toString(), 4, h.length);
256 float score = h[0].score;
257 for (int i = 1; i < h.length; i++) {
258 assertEquals("score #" + i + " is not the same", score, h[i].score,
262 printHits("testSimpleEqualScores3", h, s);
268 public void testSimpleTiebreaker() throws Exception {
270 DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.01f);
271 q.add(tq("dek", "albino"));
272 q.add(tq("dek", "elephant"));
273 QueryUtils.check(random, q, s);
275 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
278 assertEquals("3 docs should match " + q.toString(), 3, h.length);
279 assertEquals("wrong first", "d2", s.doc(h[0].doc).get("id"));
280 float score0 = h[0].score;
281 float score1 = h[1].score;
282 float score2 = h[2].score;
283 assertTrue("d2 does not have better score then others: " + score0
284 + " >? " + score1, score0 > score1);
285 assertEquals("d4 and d1 don't have equal scores", score1, score2,
288 printHits("testSimpleTiebreaker", h, s);
293 public void testBooleanRequiredEqualScores() throws Exception {
295 BooleanQuery q = new BooleanQuery();
297 DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
298 q1.add(tq("hed", "albino"));
299 q1.add(tq("dek", "albino"));
300 q.add(q1, BooleanClause.Occur.MUST);// true,false);
301 QueryUtils.check(random, q1, s);
305 DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
306 q2.add(tq("hed", "elephant"));
307 q2.add(tq("dek", "elephant"));
308 q.add(q2, BooleanClause.Occur.MUST);// true,false);
309 QueryUtils.check(random, q2, s);
312 QueryUtils.check(random, q, s);
314 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
317 assertEquals("3 docs should match " + q.toString(), 3, h.length);
318 float score = h[0].score;
319 for (int i = 1; i < h.length; i++) {
320 assertEquals("score #" + i + " is not the same", score, h[i].score,
324 printHits("testBooleanRequiredEqualScores1", h, s);
329 public void testBooleanOptionalNoTiebreaker() throws Exception {
331 BooleanQuery q = new BooleanQuery();
333 DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.0f);
334 q1.add(tq("hed", "albino"));
335 q1.add(tq("dek", "albino"));
336 q.add(q1, BooleanClause.Occur.SHOULD);// false,false);
339 DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.0f);
340 q2.add(tq("hed", "elephant"));
341 q2.add(tq("dek", "elephant"));
342 q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
344 QueryUtils.check(random, q, s);
346 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
349 assertEquals("4 docs should match " + q.toString(), 4, h.length);
350 float score = h[0].score;
351 for (int i = 1; i < h.length - 1; i++) { /* note: -1 */
352 assertEquals("score #" + i + " is not the same", score, h[i].score,
355 assertEquals("wrong last", "d1", s.doc(h[h.length - 1].doc).get("id"));
356 float score1 = h[h.length - 1].score;
357 assertTrue("d1 does not have worse score then others: " + score + " >? "
358 + score1, score > score1);
360 printHits("testBooleanOptionalNoTiebreaker", h, s);
365 public void testBooleanOptionalWithTiebreaker() throws Exception {
367 BooleanQuery q = new BooleanQuery();
369 DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
370 q1.add(tq("hed", "albino"));
371 q1.add(tq("dek", "albino"));
372 q.add(q1, BooleanClause.Occur.SHOULD);// false,false);
375 DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
376 q2.add(tq("hed", "elephant"));
377 q2.add(tq("dek", "elephant"));
378 q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
380 QueryUtils.check(random, q, s);
382 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
386 assertEquals("4 docs should match " + q.toString(), 4, h.length);
388 float score0 = h[0].score;
389 float score1 = h[1].score;
390 float score2 = h[2].score;
391 float score3 = h[3].score;
393 String doc0 = s.doc(h[0].doc).get("id");
394 String doc1 = s.doc(h[1].doc).get("id");
395 String doc2 = s.doc(h[2].doc).get("id");
396 String doc3 = s.doc(h[3].doc).get("id");
398 assertTrue("doc0 should be d2 or d4: " + doc0, doc0.equals("d2")
399 || doc0.equals("d4"));
400 assertTrue("doc1 should be d2 or d4: " + doc0, doc1.equals("d2")
401 || doc1.equals("d4"));
402 assertEquals("score0 and score1 should match", score0, score1,
404 assertEquals("wrong third", "d3", doc2);
405 assertTrue("d3 does not have worse score then d2 and d4: " + score1
406 + " >? " + score2, score1 > score2);
408 assertEquals("wrong fourth", "d1", doc3);
409 assertTrue("d1 does not have worse score then d3: " + score2 + " >? "
410 + score3, score2 > score3);
413 printHits("testBooleanOptionalWithTiebreaker", h, s);
419 public void testBooleanOptionalWithTiebreakerAndBoost() throws Exception {
421 BooleanQuery q = new BooleanQuery();
423 DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0.01f);
424 q1.add(tq("hed", "albino", 1.5f));
425 q1.add(tq("dek", "albino"));
426 q.add(q1, BooleanClause.Occur.SHOULD);// false,false);
429 DisjunctionMaxQuery q2 = new DisjunctionMaxQuery(0.01f);
430 q2.add(tq("hed", "elephant", 1.5f));
431 q2.add(tq("dek", "elephant"));
432 q.add(q2, BooleanClause.Occur.SHOULD);// false,false);
434 QueryUtils.check(random, q, s);
436 ScoreDoc[] h = s.search(q, null, 1000).scoreDocs;
440 assertEquals("4 docs should match " + q.toString(), 4, h.length);
442 float score0 = h[0].score;
443 float score1 = h[1].score;
444 float score2 = h[2].score;
445 float score3 = h[3].score;
447 String doc0 = s.doc(h[0].doc).get("id");
448 String doc1 = s.doc(h[1].doc).get("id");
449 String doc2 = s.doc(h[2].doc).get("id");
450 String doc3 = s.doc(h[3].doc).get("id");
452 assertEquals("doc0 should be d4: ", "d4", doc0);
453 assertEquals("doc1 should be d3: ", "d3", doc1);
454 assertEquals("doc2 should be d2: ", "d2", doc2);
455 assertEquals("doc3 should be d1: ", "d1", doc3);
457 assertTrue("d4 does not have a better score then d3: " + score0 + " >? "
458 + score1, score0 > score1);
459 assertTrue("d3 does not have a better score then d2: " + score1 + " >? "
460 + score2, score1 > score2);
461 assertTrue("d3 does not have a better score then d1: " + score2 + " >? "
462 + score3, score2 > score3);
465 printHits("testBooleanOptionalWithTiebreakerAndBoost", h, s);
471 protected Query tq(String f, String t) {
472 return new TermQuery(new Term(f, t));
476 protected Query tq(String f, String t, float b) {
482 protected void printHits(String test, ScoreDoc[] h, Searcher searcher)
485 System.err.println("------- " + test + " -------");
487 DecimalFormat f = new DecimalFormat("0.000000000");
489 for (int i = 0; i < h.length; i++) {
490 Document d = searcher.doc(h[i].doc);
491 float score = h[i].score;
493 .println("#" + i + ": " + f.format(score) + " - " + d.get("id"));