~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestTopDocsCollector.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search;
2
 
 
3
 
/**
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
20
 
import java.io.IOException;
21
 
 
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;
27
 
 
28
 
public class TestTopDocsCollector extends LuceneTestCase {
29
 
 
30
 
  private static final class MyTopsDocCollector extends TopDocsCollector<ScoreDoc> {
31
 
 
32
 
    private int idx = 0;
33
 
    private int base = 0;
34
 
    
35
 
    public MyTopsDocCollector(int size) {
36
 
      super(new HitQueue(size, false));
37
 
    }
38
 
    
39
 
    @Override
40
 
    protected TopDocs newTopDocs(ScoreDoc[] results, int start) {
41
 
      if (results == null) {
42
 
        return EMPTY_TOPDOCS;
43
 
      }
44
 
      
45
 
      float maxScore = Float.NaN;
46
 
      if (start == 0) {
47
 
        maxScore = results[0].score;
48
 
      } else {
49
 
        for (int i = pq.size(); i > 1; i--) { pq.pop(); }
50
 
        maxScore = pq.pop().score;
51
 
      }
52
 
      
53
 
      return new TopDocs(totalHits, results, maxScore);
54
 
    }
55
 
    
56
 
    @Override
57
 
    public void collect(int doc) throws IOException {
58
 
      ++totalHits;
59
 
      pq.insertWithOverflow(new ScoreDoc(doc + base, scores[idx++]));
60
 
    }
61
 
 
62
 
    @Override
63
 
    public void setNextReader(IndexReader reader, int docBase)
64
 
        throws IOException {
65
 
      base = docBase;
66
 
    }
67
 
 
68
 
    @Override
69
 
    public void setScorer(Scorer scorer) throws IOException {
70
 
      // Don't do anything. Assign scores in random
71
 
    }
72
 
    
73
 
    @Override
74
 
    public boolean acceptsDocsOutOfOrder() {
75
 
      return true;
76
 
    }
77
 
 
78
 
  }
79
 
 
80
 
  // Scores array to be used by MyTopDocsCollector. If it is changed, MAX_SCORE
81
 
  // must also change.
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
88
 
  };
89
 
  
90
 
  private static final float MAX_SCORE = 9.17561f;
91
 
  
92
 
  private Directory dir;
93
 
  private IndexReader reader;
94
 
 
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);
100
 
    searcher.close();
101
 
    return tdc;
102
 
  }
103
 
  
104
 
  @Override
105
 
  public void setUp() throws Exception {
106
 
    super.setUp();
107
 
    
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());
114
 
    }
115
 
    reader = writer.getReader();
116
 
    writer.close();
117
 
  }
118
 
  
119
 
  @Override
120
 
  public void tearDown() throws Exception {
121
 
    reader.close();
122
 
    dir.close();
123
 
    dir = null;
124
 
    super.tearDown();
125
 
  }
126
 
  
127
 
  public void testInvalidArguments() throws Exception {
128
 
    int numResults = 5;
129
 
    TopDocsCollector<ScoreDoc> tdc = doSearch(numResults);
130
 
    
131
 
    // start < 0
132
 
    assertEquals(0, tdc.topDocs(-1).scoreDocs.length);
133
 
    
134
 
    // start > pq.size()
135
 
    assertEquals(0, tdc.topDocs(numResults + 1).scoreDocs.length);
136
 
    
137
 
    // start == pq.size()
138
 
    assertEquals(0, tdc.topDocs(numResults).scoreDocs.length);
139
 
    
140
 
    // howMany < 0
141
 
    assertEquals(0, tdc.topDocs(0, -1).scoreDocs.length);
142
 
    
143
 
    // howMany == 0
144
 
    assertEquals(0, tdc.topDocs(0, 0).scoreDocs.length);
145
 
    
146
 
  }
147
 
  
148
 
  public void testZeroResults() throws Exception {
149
 
    TopDocsCollector<ScoreDoc> tdc = new MyTopsDocCollector(5);
150
 
    assertEquals(0, tdc.topDocs(0, 1).scoreDocs.length);
151
 
  }
152
 
  
153
 
  public void testFirstResultsPage() throws Exception {
154
 
    TopDocsCollector<ScoreDoc> tdc = doSearch(15);
155
 
    assertEquals(10, tdc.topDocs(0, 10).scoreDocs.length);
156
 
  }
157
 
  
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);
162
 
    
163
 
    // ask for 5 results (exactly what there should be
164
 
    tdc = doSearch(15);
165
 
    assertEquals(5, tdc.topDocs(10, 5).scoreDocs.length);
166
 
    
167
 
    // ask for less results than there are
168
 
    tdc = doSearch(15);
169
 
    assertEquals(4, tdc.topDocs(10, 4).scoreDocs.length);
170
 
  }
171
 
  
172
 
  public void testGetAllResults() throws Exception {
173
 
    TopDocsCollector<ScoreDoc> tdc = doSearch(15);
174
 
    assertEquals(15, tdc.topDocs().scoreDocs.length);
175
 
  }
176
 
  
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);
181
 
    
182
 
    tdc = doSearch(15);
183
 
    // get the last 5 only.
184
 
    assertEquals(5, tdc.topDocs(10).scoreDocs.length);
185
 
  }
186
 
  
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);
192
 
    
193
 
    // ask for 5 last results
194
 
    tdc = doSearch(15);
195
 
    td = tdc.topDocs(10);
196
 
    assertEquals(MAX_SCORE, td.getMaxScore(), 0f);
197
 
  }
198
 
  
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;
204
 
    
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);
208
 
    }
209
 
  }
210
 
  
211
 
}