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 java.io.IOException;
22
import org.apache.lucene.document.Document;
23
import org.apache.lucene.index.IndexReader;
24
import org.apache.lucene.index.RandomIndexWriter;
25
import org.apache.lucene.store.Directory;
26
import org.apache.lucene.util.LuceneTestCase;
28
public class TestTopDocsCollector extends LuceneTestCase {
30
private static final class MyTopsDocCollector extends TopDocsCollector<ScoreDoc> {
35
public MyTopsDocCollector(int size) {
36
super(new HitQueue(size, false));
40
protected TopDocs newTopDocs(ScoreDoc[] results, int start) {
41
if (results == null) {
45
float maxScore = Float.NaN;
47
maxScore = results[0].score;
49
for (int i = pq.size(); i > 1; i--) { pq.pop(); }
50
maxScore = pq.pop().score;
53
return new TopDocs(totalHits, results, maxScore);
57
public void collect(int doc) throws IOException {
59
pq.insertWithOverflow(new ScoreDoc(doc + base, scores[idx++]));
63
public void setNextReader(IndexReader reader, int docBase)
69
public void setScorer(Scorer scorer) throws IOException {
70
// Don't do anything. Assign scores in random
74
public boolean acceptsDocsOutOfOrder() {
80
// Scores array to be used by MyTopDocsCollector. If it is changed, MAX_SCORE
82
private static final float[] scores = new float[] {
83
0.7767749f, 1.7839992f, 8.9925785f, 7.9608946f, 0.07948637f, 2.6356435f,
84
7.4950366f, 7.1490803f, 8.108544f, 4.961808f, 2.2423935f, 7.285586f, 4.6699767f,
85
2.9655676f, 6.953706f, 5.383931f, 6.9916306f, 8.365894f, 7.888485f, 8.723962f,
86
3.1796896f, 0.39971232f, 1.3077754f, 6.8489285f, 9.17561f, 5.060466f, 7.9793315f,
87
8.601509f, 4.1858315f, 0.28146625f
90
private static final float MAX_SCORE = 9.17561f;
92
private Directory dir;
93
private IndexReader reader;
95
private TopDocsCollector<ScoreDoc> doSearch(int numResults) throws IOException {
96
Query q = new MatchAllDocsQuery();
97
IndexSearcher searcher = newSearcher(reader);
98
TopDocsCollector<ScoreDoc> tdc = new MyTopsDocCollector(numResults);
99
searcher.search(q, tdc);
105
public void setUp() throws Exception {
108
// populate an index with 30 documents, this should be enough for the test.
109
// The documents have no content - the test uses MatchAllDocsQuery().
110
dir = newDirectory();
111
RandomIndexWriter writer = new RandomIndexWriter(random, dir);
112
for (int i = 0; i < 30; i++) {
113
writer.addDocument(new Document());
115
reader = writer.getReader();
120
public void tearDown() throws Exception {
127
public void testInvalidArguments() throws Exception {
129
TopDocsCollector<ScoreDoc> tdc = doSearch(numResults);
132
assertEquals(0, tdc.topDocs(-1).scoreDocs.length);
135
assertEquals(0, tdc.topDocs(numResults + 1).scoreDocs.length);
137
// start == pq.size()
138
assertEquals(0, tdc.topDocs(numResults).scoreDocs.length);
141
assertEquals(0, tdc.topDocs(0, -1).scoreDocs.length);
144
assertEquals(0, tdc.topDocs(0, 0).scoreDocs.length);
148
public void testZeroResults() throws Exception {
149
TopDocsCollector<ScoreDoc> tdc = new MyTopsDocCollector(5);
150
assertEquals(0, tdc.topDocs(0, 1).scoreDocs.length);
153
public void testFirstResultsPage() throws Exception {
154
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
155
assertEquals(10, tdc.topDocs(0, 10).scoreDocs.length);
158
public void testSecondResultsPages() throws Exception {
159
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
160
// ask for more results than are available
161
assertEquals(5, tdc.topDocs(10, 10).scoreDocs.length);
163
// ask for 5 results (exactly what there should be
165
assertEquals(5, tdc.topDocs(10, 5).scoreDocs.length);
167
// ask for less results than there are
169
assertEquals(4, tdc.topDocs(10, 4).scoreDocs.length);
172
public void testGetAllResults() throws Exception {
173
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
174
assertEquals(15, tdc.topDocs().scoreDocs.length);
177
public void testGetResultsFromStart() throws Exception {
178
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
179
// should bring all results
180
assertEquals(15, tdc.topDocs(0).scoreDocs.length);
183
// get the last 5 only.
184
assertEquals(5, tdc.topDocs(10).scoreDocs.length);
187
public void testMaxScore() throws Exception {
188
// ask for all results
189
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
190
TopDocs td = tdc.topDocs();
191
assertEquals(MAX_SCORE, td.getMaxScore(), 0f);
193
// ask for 5 last results
195
td = tdc.topDocs(10);
196
assertEquals(MAX_SCORE, td.getMaxScore(), 0f);
199
// This does not test the PQ's correctness, but whether topDocs()
200
// implementations return the results in decreasing score order.
201
public void testResultsOrder() throws Exception {
202
TopDocsCollector<ScoreDoc> tdc = doSearch(15);
203
ScoreDoc[] sd = tdc.topDocs().scoreDocs;
205
assertEquals(MAX_SCORE, sd[0].score, 0f);
206
for (int i = 1; i < sd.length; i++) {
207
assertTrue(sd[i - 1].score >= sd[i].score);