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.document.Document;
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.RandomIndexWriter;
23 import org.apache.lucene.search.BooleanClause.Occur;
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.util.LuceneTestCase;
27 public class TestTopScoreDocCollector extends LuceneTestCase {
29 public void testOutOfOrderCollection() throws Exception {
30 Directory dir = newDirectory();
31 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
32 for (int i = 0; i < 10; i++) {
33 writer.addDocument(new Document());
36 boolean[] inOrder = new boolean[] { false, true };
37 String[] actualTSDCClass = new String[] {
38 "OutOfOrderTopScoreDocCollector",
39 "InOrderTopScoreDocCollector"
42 BooleanQuery bq = new BooleanQuery();
43 // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
44 // which delegates to BS if there are no mandatory clauses.
45 bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
46 // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
47 // the clause instead of BQ.
48 bq.setMinimumNumberShouldMatch(1);
49 IndexReader reader = writer.getReader();
50 IndexSearcher searcher = newSearcher(reader);
51 for (int i = 0; i < inOrder.length; i++) {
52 TopDocsCollector<ScoreDoc> tdc = TopScoreDocCollector.create(3, inOrder[i]);
53 assertEquals("org.apache.lucene.search.TopScoreDocCollector$" + actualTSDCClass[i], tdc.getClass().getName());
55 searcher.search(new MatchAllDocsQuery(), tdc);
57 ScoreDoc[] sd = tdc.topDocs().scoreDocs;
58 assertEquals(3, sd.length);
59 for (int j = 0; j < sd.length; j++) {
60 assertEquals("expected doc Id " + j + " found " + sd[j].doc, j, sd[j].doc);