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

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestSort.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
 
import java.io.Serializable;
22
 
import java.text.Collator;
23
 
import java.util.ArrayList;
24
 
import java.util.BitSet;
25
 
import java.util.HashMap;
26
 
import java.util.Iterator;
27
 
import java.util.Locale;
28
 
import java.util.concurrent.ExecutorService;
29
 
import java.util.concurrent.Executors;
30
 
import java.util.concurrent.TimeUnit;
31
 
 
32
 
import org.apache.lucene.analysis.MockAnalyzer;
33
 
import org.apache.lucene.document.Document;
34
 
import org.apache.lucene.document.Field;
35
 
import org.apache.lucene.index.CorruptIndexException;
36
 
import org.apache.lucene.index.IndexReader;
37
 
import org.apache.lucene.index.IndexWriter;
38
 
import org.apache.lucene.index.IndexWriterConfig;
39
 
import org.apache.lucene.index.RandomIndexWriter;
40
 
import org.apache.lucene.index.Term;
41
 
import org.apache.lucene.queryParser.ParseException;
42
 
import org.apache.lucene.search.BooleanClause.Occur;
43
 
import org.apache.lucene.search.FieldValueHitQueue.Entry;
44
 
import org.apache.lucene.store.LockObtainFailedException;
45
 
import org.apache.lucene.store.Directory;
46
 
import org.apache.lucene.util.DocIdBitSet;
47
 
import org.apache.lucene.util.LuceneTestCase;
48
 
import org.apache.lucene.util._TestUtil;
49
 
import org.junit.BeforeClass;
50
 
 
51
 
/**
52
 
 * Unit tests for sorting code.
53
 
 *
54
 
 * <p>Created: Feb 17, 2004 4:55:10 PM
55
 
 *
56
 
 * @since   lucene 1.4
57
 
 */
58
 
 
59
 
public class TestSort extends LuceneTestCase implements Serializable {
60
 
 
61
 
  private static int NUM_STRINGS;
62
 
  private IndexSearcher full;
63
 
  private IndexSearcher searchX;
64
 
  private IndexSearcher searchY;
65
 
  private Query queryX;
66
 
  private Query queryY;
67
 
  private Query queryA;
68
 
  private Query queryE;
69
 
  private Query queryF;
70
 
  private Query queryG;
71
 
  private Query queryM;
72
 
  private Sort sort;
73
 
 
74
 
  @BeforeClass
75
 
  public static void beforeClass() throws Exception {
76
 
    NUM_STRINGS = atLeast(6000);
77
 
  }
78
 
  // document data:
79
 
  // the tracer field is used to determine which document was hit
80
 
  // the contents field is used to search and sort by relevance
81
 
  // the int field to sort by int
82
 
  // the float field to sort by float
83
 
  // the string field to sort by string
84
 
    // the i18n field includes accented characters for testing locale-specific sorting
85
 
  private String[][] data = new String[][] {
86
 
  // tracer  contents         int            float           string   custom   i18n               long            double,          short,     byte, 'custom parser encoding'
87
 
  {   "A",   "x a",           "5",           "4f",           "c",     "A-3",   "p\u00EAche",      "10",           "-4.0",            "3",    "126", "J"},//A, x
88
 
  {   "B",   "y a",           "5",           "3.4028235E38", "i",     "B-10",  "HAT",             "1000000000",   "40.0",           "24",      "1", "I"},//B, y
89
 
  {   "C",   "x a b c",       "2147483647",  "1.0",          "j",     "A-2",   "p\u00E9ch\u00E9", "99999999","40.00002343",        "125",     "15", "H"},//C, x
90
 
  {   "D",   "y a b c",       "-1",          "0.0f",         "a",     "C-0",   "HUT",   String.valueOf(Long.MAX_VALUE),String.valueOf(Double.MIN_VALUE), String.valueOf(Short.MIN_VALUE), String.valueOf(Byte.MIN_VALUE), "G"},//D, y
91
 
  {   "E",   "x a b c d",     "5",           "2f",           "h",     "B-8",   "peach", String.valueOf(Long.MIN_VALUE),String.valueOf(Double.MAX_VALUE), String.valueOf(Short.MAX_VALUE),           String.valueOf(Byte.MAX_VALUE), "F"},//E,x
92
 
  {   "F",   "y a b c d",     "2",           "3.14159f",     "g",     "B-1",   "H\u00C5T",        "-44",          "343.034435444",  "-3",      "0", "E"},//F,y
93
 
  {   "G",   "x a b c d",     "3",           "-1.0",         "f",     "C-100", "sin",             "323254543543", "4.043544",        "5",    "100", "D"},//G,x
94
 
  {   "H",   "y a b c d",     "0",           "1.4E-45",      "e",     "C-88",  "H\u00D8T",        "1023423423005","4.043545",       "10",    "-50", "C"},//H,y
95
 
  {   "I",   "x a b c d e f", "-2147483648", "1.0e+0",       "d",     "A-10",  "s\u00EDn",        "332422459999", "4.043546",     "-340",     "51", "B"},//I,x
96
 
  {   "J",   "y a b c d e f", "4",           ".5",           "b",     "C-7",   "HOT",             "34334543543",  "4.0000220343",  "300",      "2", "A"},//J,y
97
 
  {   "W",   "g",             "1",           null,           null,    null,    null,              null,           null, null, null, null},
98
 
  {   "X",   "g",             "1",           "0.1",          null,    null,    null,              null,           null, null, null, null},
99
 
  {   "Y",   "g",             "1",           "0.2",          null,    null,    null,              null,           null, null, null, null},
100
 
  {   "Z",   "f g",           null,          null,           null,    null,    null,              null,           null, null, null, null},
101
 
  
102
 
  // Sort Missing first/last
103
 
  {   "a",   "m",            null,          null,           null,    null,    null,              null,           null, null, null, null},
104
 
  {   "b",   "m",            "4",           "4.0",           "4",    null,    null,              "4",           "4", "4", "4", null},
105
 
  {   "c",   "m",            "5",           "5.0",           "5",    null,    null,              "5",           "5", "5", "5", null},
106
 
  {   "d",   "m",            null,          null,           null,    null,    null,              null,           null, null, null, null}
107
 
  }; 
108
 
  
109
 
  // the sort order of Ø versus U depends on the version of the rules being used
110
 
  // for the inherited root locale: Ø's order isnt specified in Locale.US since 
111
 
  // its not used in english.
112
 
  private boolean oStrokeFirst = Collator.getInstance(new Locale("")).compare("Ø", "U") < 0;
113
 
  
114
 
  // create an index of all the documents, or just the x, or just the y documents
115
 
  private IndexSearcher getIndex (boolean even, boolean odd)
116
 
  throws IOException {
117
 
    Directory indexStore = newDirectory();
118
 
    dirs.add(indexStore);
119
 
    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
120
 
 
121
 
    for (int i=0; i<data.length; ++i) {
122
 
      if (((i%2)==0 && even) || ((i%2)==1 && odd)) {
123
 
        Document doc = new Document();
124
 
        doc.add (new Field ("tracer",   data[i][0], Field.Store.YES, Field.Index.NO));
125
 
        doc.add (new Field ("contents", data[i][1], Field.Store.NO, Field.Index.ANALYZED));
126
 
        if (data[i][2] != null) doc.add (new Field ("int",      data[i][2], Field.Store.NO, Field.Index.NOT_ANALYZED));
127
 
        if (data[i][3] != null) doc.add (new Field ("float",    data[i][3], Field.Store.NO, Field.Index.NOT_ANALYZED));
128
 
        if (data[i][4] != null) doc.add (new Field ("string",   data[i][4], Field.Store.NO, Field.Index.NOT_ANALYZED));
129
 
        if (data[i][5] != null) doc.add (new Field ("custom",   data[i][5], Field.Store.NO, Field.Index.NOT_ANALYZED));
130
 
        if (data[i][6] != null) doc.add (new Field ("i18n",     data[i][6], Field.Store.NO, Field.Index.NOT_ANALYZED));
131
 
        if (data[i][7] != null) doc.add (new Field ("long",     data[i][7], Field.Store.NO, Field.Index.NOT_ANALYZED));
132
 
        if (data[i][8] != null) doc.add (new Field ("double",     data[i][8], Field.Store.NO, Field.Index.NOT_ANALYZED));
133
 
        if (data[i][9] != null) doc.add (new Field ("short",     data[i][9], Field.Store.NO, Field.Index.NOT_ANALYZED));
134
 
        if (data[i][10] != null) doc.add (new Field ("byte",     data[i][10], Field.Store.NO, Field.Index.NOT_ANALYZED));
135
 
        if (data[i][11] != null) doc.add (new Field ("parser",     data[i][11], Field.Store.NO, Field.Index.NOT_ANALYZED));
136
 
        doc.setBoost(2);  // produce some scores above 1.0
137
 
        writer.addDocument (doc);
138
 
      }
139
 
    }
140
 
    IndexReader reader = writer.getReader();
141
 
    writer.close ();
142
 
    IndexSearcher s = newSearcher(reader);
143
 
    s.setDefaultFieldSortScoring(true, true);
144
 
    return s;
145
 
  }
146
 
 
147
 
  private IndexSearcher getFullIndex()
148
 
  throws IOException {
149
 
    return getIndex (true, true);
150
 
  }
151
 
  
152
 
  private IndexSearcher getFullStrings() throws CorruptIndexException, LockObtainFailedException, IOException {
153
 
    Directory indexStore = newDirectory();
154
 
    dirs.add(indexStore);
155
 
    IndexWriter writer = new IndexWriter(
156
 
        indexStore,
157
 
        new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
158
 
            setMaxBufferedDocs(4).
159
 
            setMergePolicy(newLogMergePolicy(97))
160
 
    );
161
 
    for (int i=0; i<NUM_STRINGS; i++) {
162
 
        Document doc = new Document();
163
 
        String num = getRandomCharString(getRandomNumber(2, 8), 48, 52);
164
 
        doc.add (new Field ("tracer", num, Field.Store.YES, Field.Index.NO));
165
 
        //doc.add (new Field ("contents", Integer.toString(i), Field.Store.NO, Field.Index.ANALYZED));
166
 
        doc.add (new Field ("string", num, Field.Store.NO, Field.Index.NOT_ANALYZED));
167
 
        String num2 = getRandomCharString(getRandomNumber(1, 4), 48, 50);
168
 
        doc.add (new Field ("string2", num2, Field.Store.NO, Field.Index.NOT_ANALYZED));
169
 
        doc.add (new Field ("tracer2", num2, Field.Store.YES, Field.Index.NO));
170
 
        doc.setBoost(2);  // produce some scores above 1.0
171
 
        writer.addDocument (doc);
172
 
      
173
 
    }
174
 
    //writer.forceMerge(1);
175
 
    //System.out.println(writer.getSegmentCount());
176
 
    writer.close();
177
 
    IndexReader reader = IndexReader.open(indexStore);
178
 
    return new IndexSearcher (reader);
179
 
  }
180
 
  
181
 
  public String getRandomNumberString(int num, int low, int high) {
182
 
    StringBuilder sb = new StringBuilder();
183
 
    for (int i = 0; i < num; i++) {
184
 
      sb.append(getRandomNumber(low, high));
185
 
    }
186
 
    return sb.toString();
187
 
  }
188
 
  
189
 
  public String getRandomCharString(int num) {
190
 
    return getRandomCharString(num, 48, 122);
191
 
  }
192
 
  
193
 
  public String getRandomCharString(int num, int start, int end) {
194
 
    StringBuilder sb = new StringBuilder();
195
 
    for (int i = 0; i < num; i++) {
196
 
      sb.append(new Character((char) getRandomNumber(start, end)));
197
 
    }
198
 
    return sb.toString();
199
 
  }
200
 
  
201
 
  public int getRandomNumber(final int low, final int high) {
202
 
  
203
 
    int randInt = (Math.abs(random.nextInt()) % (high - low)) + low;
204
 
 
205
 
    return randInt;
206
 
  }
207
 
 
208
 
  private IndexSearcher getXIndex()
209
 
  throws IOException {
210
 
    return getIndex (true, false);
211
 
  }
212
 
 
213
 
  private IndexSearcher getYIndex()
214
 
  throws IOException {
215
 
    return getIndex (false, true);
216
 
  }
217
 
 
218
 
  private IndexSearcher getEmptyIndex()
219
 
  throws IOException {
220
 
    return getIndex (false, false);
221
 
  }
222
 
 
223
 
  @Override
224
 
  public void setUp() throws Exception {
225
 
    super.setUp();
226
 
    full = getFullIndex();
227
 
    searchX = getXIndex();
228
 
    searchY = getYIndex();
229
 
    queryX = new TermQuery (new Term ("contents", "x"));
230
 
    queryY = new TermQuery (new Term ("contents", "y"));
231
 
    queryA = new TermQuery (new Term ("contents", "a"));
232
 
    queryE = new TermQuery (new Term ("contents", "e"));
233
 
    queryF = new TermQuery (new Term ("contents", "f"));
234
 
    queryG = new TermQuery (new Term ("contents", "g"));
235
 
    queryM = new TermQuery (new Term ("contents", "m"));
236
 
    sort = new Sort();
237
 
  }
238
 
  
239
 
  private ArrayList<Directory> dirs = new ArrayList<Directory>();
240
 
  
241
 
  @Override
242
 
  public void tearDown() throws Exception {
243
 
    full.reader.close();
244
 
    searchX.reader.close();
245
 
    searchY.reader.close();
246
 
    full.close();
247
 
    searchX.close();
248
 
    searchY.close();
249
 
    for (Directory dir : dirs)
250
 
      dir.close();
251
 
    super.tearDown();
252
 
  }
253
 
 
254
 
  // test the sorts by score and document number
255
 
  public void testBuiltInSorts() throws Exception {
256
 
    sort = new Sort();
257
 
    assertMatches (full, queryX, sort, "ACEGI");
258
 
    assertMatches (full, queryY, sort, "BDFHJ");
259
 
 
260
 
    sort.setSort(SortField.FIELD_DOC);
261
 
    assertMatches (full, queryX, sort, "ACEGI");
262
 
    assertMatches (full, queryY, sort, "BDFHJ");
263
 
  }
264
 
 
265
 
  // test sorts where the type of field is specified
266
 
  public void testTypedSort() throws Exception {
267
 
    sort.setSort (new SortField ("int", SortField.INT), SortField.FIELD_DOC );
268
 
    assertMatches (full, queryX, sort, "IGAEC");
269
 
    assertMatches (full, queryY, sort, "DHFJB");
270
 
 
271
 
    sort.setSort (new SortField ("float", SortField.FLOAT), SortField.FIELD_DOC );
272
 
    assertMatches (full, queryX, sort, "GCIEA");
273
 
    assertMatches (full, queryY, sort, "DHJFB");
274
 
 
275
 
    sort.setSort (new SortField ("long", SortField.LONG), SortField.FIELD_DOC );
276
 
    assertMatches (full, queryX, sort, "EACGI");
277
 
    assertMatches (full, queryY, sort, "FBJHD");
278
 
 
279
 
    sort.setSort (new SortField ("double", SortField.DOUBLE), SortField.FIELD_DOC );
280
 
    assertMatches (full, queryX, sort, "AGICE");
281
 
    assertMatches (full, queryY, sort, "DJHBF");
282
 
 
283
 
    sort.setSort (new SortField ("byte", SortField.BYTE), SortField.FIELD_DOC );
284
 
    assertMatches (full, queryX, sort, "CIGAE");
285
 
    assertMatches (full, queryY, sort, "DHFBJ");
286
 
 
287
 
    sort.setSort (new SortField ("short", SortField.SHORT), SortField.FIELD_DOC );
288
 
    assertMatches (full, queryX, sort, "IAGCE");
289
 
    assertMatches (full, queryY, sort, "DFHBJ");
290
 
 
291
 
    sort.setSort (new SortField ("string", SortField.STRING), SortField.FIELD_DOC );
292
 
    assertMatches (full, queryX, sort, "AIGEC");
293
 
    assertMatches (full, queryY, sort, "DJHFB");
294
 
  }
295
 
  
296
 
  private static class SortMissingLastTestHelper {
297
 
    final SortField sortField;
298
 
    final Object min;
299
 
    final Object max;
300
 
    
301
 
    SortMissingLastTestHelper( SortField sortField, Object min, Object max ) {
302
 
      this.sortField = sortField;
303
 
      this.min = min;
304
 
      this.max = max;
305
 
    }
306
 
  }
307
 
 
308
 
  // test sorts where the type of field is specified
309
 
  public void testSortMissingLast() throws Exception {
310
 
    
311
 
    @SuppressWarnings("boxing")
312
 
    SortMissingLastTestHelper[] ascendTesters = new SortMissingLastTestHelper[] {
313
 
        new SortMissingLastTestHelper( new SortField(   "byte",   SortField.BYTE ), Byte.MIN_VALUE,    Byte.MAX_VALUE ),
314
 
        new SortMissingLastTestHelper( new SortField(  "short",  SortField.SHORT ), Short.MIN_VALUE,   Short.MAX_VALUE ),
315
 
        new SortMissingLastTestHelper( new SortField(    "int",    SortField.INT ), Integer.MIN_VALUE, Integer.MAX_VALUE ),
316
 
        new SortMissingLastTestHelper( new SortField(   "long",   SortField.LONG ), Long.MIN_VALUE,    Long.MAX_VALUE ),
317
 
        new SortMissingLastTestHelper( new SortField(  "float",  SortField.FLOAT ), Float.MIN_VALUE,   Float.MAX_VALUE ),
318
 
        new SortMissingLastTestHelper( new SortField( "double", SortField.DOUBLE ), Double.MIN_VALUE,  Double.MAX_VALUE ),
319
 
    };
320
 
    
321
 
    @SuppressWarnings("boxing")
322
 
    SortMissingLastTestHelper[] descendTesters = new SortMissingLastTestHelper[] {
323
 
      new SortMissingLastTestHelper( new SortField(   "byte",   SortField.BYTE, true ), Byte.MIN_VALUE,    Byte.MAX_VALUE ),
324
 
      new SortMissingLastTestHelper( new SortField(  "short",  SortField.SHORT, true ), Short.MIN_VALUE,   Short.MAX_VALUE ),
325
 
      new SortMissingLastTestHelper( new SortField(    "int",    SortField.INT, true ), Integer.MIN_VALUE, Integer.MAX_VALUE ),
326
 
      new SortMissingLastTestHelper( new SortField(   "long",   SortField.LONG, true ), Long.MIN_VALUE,    Long.MAX_VALUE ),
327
 
      new SortMissingLastTestHelper( new SortField(  "float",  SortField.FLOAT, true ), Float.MIN_VALUE,   Float.MAX_VALUE ),
328
 
      new SortMissingLastTestHelper( new SortField( "double", SortField.DOUBLE, true ), Double.MIN_VALUE,  Double.MAX_VALUE ),
329
 
    };
330
 
    
331
 
    // Default order: ascending
332
 
    for( SortMissingLastTestHelper t : ascendTesters ) {
333
 
      sort.setSort (t.sortField, SortField.FIELD_DOC );
334
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "adbc" );
335
 
 
336
 
      sort.setSort (t.sortField.setMissingValue( t.max ), SortField.FIELD_DOC );
337
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "bcad" );
338
 
 
339
 
      sort.setSort (t.sortField.setMissingValue( t.min ), SortField.FIELD_DOC );
340
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "adbc" );
341
 
    }
342
 
    
343
 
    // Reverse order: descending (Note: Order for un-valued documents remains the same due to tie breaker: a,d)
344
 
    for( SortMissingLastTestHelper t : descendTesters ) {
345
 
      sort.setSort (t.sortField, SortField.FIELD_DOC );
346
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "cbad" );
347
 
      
348
 
      sort.setSort (t.sortField.setMissingValue( t.max ), SortField.FIELD_DOC );
349
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "adcb" );
350
 
      
351
 
      sort.setSort (t.sortField.setMissingValue( t.min ), SortField.FIELD_DOC );
352
 
      assertMatches("sortField:"+t.sortField, full, queryM, sort, "cbad" );
353
 
    }
354
 
    
355
 
    
356
 
  }
357
 
  
358
 
  /**
359
 
   * Test String sorting: small queue to many matches, multi field sort, reverse sort
360
 
   */
361
 
  public void testStringSort() throws IOException, ParseException {
362
 
    ScoreDoc[] result = null;
363
 
    IndexSearcher searcher = getFullStrings();
364
 
    sort.setSort(
365
 
        new SortField("string", SortField.STRING),
366
 
        new SortField("string2", SortField.STRING, true),
367
 
        SortField.FIELD_DOC );
368
 
 
369
 
    result = searcher.search(new MatchAllDocsQuery(), null, 500, sort).scoreDocs;
370
 
 
371
 
    StringBuilder buff = new StringBuilder();
372
 
    int n = result.length;
373
 
    String last = null;
374
 
    String lastSub = null;
375
 
    int lastDocId = 0;
376
 
    boolean fail = false;
377
 
    for (int x = 0; x < n; ++x) {
378
 
      Document doc2 = searcher.doc(result[x].doc);
379
 
      String[] v = doc2.getValues("tracer");
380
 
      String[] v2 = doc2.getValues("tracer2");
381
 
      for (int j = 0; j < v.length; ++j) {
382
 
        if (last != null) {
383
 
          int cmp = v[j].compareTo(last);
384
 
          if (!(cmp >= 0)) { // ensure first field is in order
385
 
            fail = true;
386
 
            System.out.println("fail:" + v[j] + " < " + last);
387
 
          }
388
 
          if (cmp == 0) { // ensure second field is in reverse order
389
 
            cmp = v2[j].compareTo(lastSub);
390
 
            if (cmp > 0) {
391
 
              fail = true;
392
 
              System.out.println("rev field fail:" + v2[j] + " > " + lastSub);
393
 
            } else if(cmp == 0) { // ensure docid is in order
394
 
              if (result[x].doc < lastDocId) {
395
 
                fail = true;
396
 
                System.out.println("doc fail:" + result[x].doc + " > " + lastDocId);
397
 
              }
398
 
            }
399
 
          }
400
 
        }
401
 
        last = v[j];
402
 
        lastSub = v2[j];
403
 
        lastDocId = result[x].doc;
404
 
        buff.append(v[j] + "(" + v2[j] + ")(" + result[x].doc+") ");
405
 
      }
406
 
    }
407
 
    if(fail) {
408
 
      System.out.println("topn field1(field2)(docID):" + buff);
409
 
    }
410
 
    assertFalse("Found sort results out of order", fail);
411
 
    searcher.getIndexReader().close();
412
 
    searcher.close();
413
 
  }
414
 
  
415
 
  /** 
416
 
   * test sorts where the type of field is specified and a custom field parser 
417
 
   * is used, that uses a simple char encoding. The sorted string contains a 
418
 
   * character beginning from 'A' that is mapped to a numeric value using some 
419
 
   * "funny" algorithm to be different for each data type.
420
 
   */
421
 
  public void testCustomFieldParserSort() throws Exception {
422
 
    // since tests explicilty uses different parsers on the same fieldname
423
 
    // we explicitly check/purge the FieldCache between each assertMatch
424
 
    FieldCache fc = FieldCache.DEFAULT;
425
 
 
426
 
 
427
 
    sort.setSort (new SortField ("parser", new FieldCache.IntParser(){
428
 
      public final int parseInt(final String val) {
429
 
        return (val.charAt(0)-'A') * 123456;
430
 
      }
431
 
    }), SortField.FIELD_DOC );
432
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
433
 
    assertSaneFieldCaches(getName() + " IntParser");
434
 
    fc.purgeAllCaches();
435
 
 
436
 
    sort.setSort (new SortField ("parser", new FieldCache.FloatParser(){
437
 
      public final float parseFloat(final String val) {
438
 
        return (float) Math.sqrt( val.charAt(0) );
439
 
      }
440
 
    }), SortField.FIELD_DOC );
441
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
442
 
    assertSaneFieldCaches(getName() + " FloatParser");
443
 
    fc.purgeAllCaches();
444
 
 
445
 
    sort.setSort (new SortField ("parser", new FieldCache.LongParser(){
446
 
      public final long parseLong(final String val) {
447
 
        return (val.charAt(0)-'A') * 1234567890L;
448
 
      }
449
 
    }), SortField.FIELD_DOC );
450
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
451
 
    assertSaneFieldCaches(getName() + " LongParser");
452
 
    fc.purgeAllCaches();
453
 
 
454
 
    sort.setSort (new SortField ("parser", new FieldCache.DoubleParser(){
455
 
      public final double parseDouble(final String val) {
456
 
        return Math.pow( val.charAt(0), (val.charAt(0)-'A') );
457
 
      }
458
 
    }), SortField.FIELD_DOC );
459
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
460
 
    assertSaneFieldCaches(getName() + " DoubleParser");
461
 
    fc.purgeAllCaches();
462
 
 
463
 
    sort.setSort (new SortField ("parser", new FieldCache.ByteParser(){
464
 
      public final byte parseByte(final String val) {
465
 
        return (byte) (val.charAt(0)-'A');
466
 
      }
467
 
    }), SortField.FIELD_DOC );
468
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
469
 
    assertSaneFieldCaches(getName() + " ByteParser");
470
 
    fc.purgeAllCaches();
471
 
 
472
 
    sort.setSort (new SortField ("parser", new FieldCache.ShortParser(){
473
 
      public final short parseShort(final String val) {
474
 
        return (short) (val.charAt(0)-'A');
475
 
      }
476
 
    }), SortField.FIELD_DOC );
477
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
478
 
    assertSaneFieldCaches(getName() + " ShortParser");
479
 
    fc.purgeAllCaches();
480
 
  }
481
 
 
482
 
  // test sorts when there's nothing in the index
483
 
  public void testEmptyIndex() throws Exception {
484
 
    Searcher empty = getEmptyIndex();
485
 
 
486
 
    sort = new Sort();
487
 
    assertMatches (empty, queryX, sort, "");
488
 
 
489
 
    sort.setSort(SortField.FIELD_DOC);
490
 
    assertMatches (empty, queryX, sort, "");
491
 
 
492
 
    sort.setSort (new SortField ("int", SortField.INT), SortField.FIELD_DOC );
493
 
    assertMatches (empty, queryX, sort, "");
494
 
 
495
 
    sort.setSort (new SortField ("string", SortField.STRING, true), SortField.FIELD_DOC );
496
 
    assertMatches (empty, queryX, sort, "");
497
 
 
498
 
    sort.setSort (new SortField ("float", SortField.FLOAT), new SortField ("string", SortField.STRING) );
499
 
    assertMatches (empty, queryX, sort, "");
500
 
  }
501
 
 
502
 
  static class MyFieldComparator extends FieldComparator<Integer> {
503
 
    int[] docValues;
504
 
    int[] slotValues;
505
 
    int bottomValue;
506
 
 
507
 
    MyFieldComparator(int numHits) {
508
 
      slotValues = new int[numHits];
509
 
    }
510
 
 
511
 
    @Override
512
 
    public void copy(int slot, int doc) {
513
 
      slotValues[slot] = docValues[doc];
514
 
    }
515
 
 
516
 
    @Override
517
 
    public int compare(int slot1, int slot2) {
518
 
      // values are small enough that overflow won't happen
519
 
      return slotValues[slot1] - slotValues[slot2];
520
 
    }
521
 
 
522
 
    @Override
523
 
    public int compareBottom(int doc) {
524
 
      return bottomValue - docValues[doc];
525
 
    }
526
 
 
527
 
    @Override
528
 
    public void setBottom(int bottom) {
529
 
      bottomValue = slotValues[bottom];
530
 
    }
531
 
 
532
 
    private static final FieldCache.IntParser testIntParser = new FieldCache.IntParser() {
533
 
      public final int parseInt(final String val) {
534
 
        return (val.charAt(0)-'A') * 123456;
535
 
      }
536
 
    };
537
 
 
538
 
    @Override
539
 
    public void setNextReader(IndexReader reader, int docBase) throws IOException {
540
 
      docValues = FieldCache.DEFAULT.getInts(reader, "parser", testIntParser);
541
 
    }
542
 
 
543
 
    @Override
544
 
    public Integer value(int slot) {
545
 
      return Integer.valueOf(slotValues[slot]);
546
 
    }
547
 
  }
548
 
 
549
 
  static class MyFieldComparatorSource extends FieldComparatorSource {
550
 
    @Override
551
 
    public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
552
 
      return new MyFieldComparator(numHits);
553
 
    }
554
 
  }
555
 
 
556
 
  // Test sorting w/ custom FieldComparator
557
 
  public void testNewCustomFieldParserSort() throws Exception {
558
 
    sort.setSort (new SortField ("parser", new MyFieldComparatorSource()));
559
 
    assertMatches (full, queryA, sort, "JIHGFEDCBA");
560
 
  }
561
 
 
562
 
  // test sorts in reverse
563
 
  public void testReverseSort() throws Exception {
564
 
    sort.setSort (new SortField (null, SortField.SCORE, true), SortField.FIELD_DOC );
565
 
    assertMatches (full, queryX, sort, "IEGCA");
566
 
    assertMatches (full, queryY, sort, "JFHDB");
567
 
 
568
 
    sort.setSort (new SortField (null, SortField.DOC, true));
569
 
    assertMatches (full, queryX, sort, "IGECA");
570
 
    assertMatches (full, queryY, sort, "JHFDB");
571
 
 
572
 
    sort.setSort (new SortField ("int", SortField.INT, true) );
573
 
    assertMatches (full, queryX, sort, "CAEGI");
574
 
    assertMatches (full, queryY, sort, "BJFHD");
575
 
 
576
 
    sort.setSort (new SortField ("float", SortField.FLOAT, true) );
577
 
    assertMatches (full, queryX, sort, "AECIG");
578
 
    assertMatches (full, queryY, sort, "BFJHD");
579
 
 
580
 
    sort.setSort (new SortField ("string", SortField.STRING, true) );
581
 
    assertMatches (full, queryX, sort, "CEGIA");
582
 
    assertMatches (full, queryY, sort, "BFHJD");
583
 
  }
584
 
 
585
 
  // test sorting when the sort field is empty (undefined) for some of the documents
586
 
  public void testEmptyFieldSort() throws Exception {
587
 
    sort.setSort (new SortField ("string", SortField.STRING) );
588
 
    assertMatches (full, queryF, sort, "ZJI");
589
 
 
590
 
    sort.setSort (new SortField ("string", SortField.STRING, true) );
591
 
    assertMatches (full, queryF, sort, "IJZ");
592
 
    
593
 
    sort.setSort (new SortField ("i18n", Locale.ENGLISH));
594
 
    assertMatches (full, queryF, sort, "ZJI");
595
 
    
596
 
    sort.setSort (new SortField ("i18n", Locale.ENGLISH, true));
597
 
    assertMatches (full, queryF, sort, "IJZ");
598
 
 
599
 
    sort.setSort (new SortField ("int", SortField.INT) );
600
 
    assertMatches (full, queryF, sort, "IZJ");
601
 
 
602
 
    sort.setSort (new SortField ("int", SortField.INT, true) );
603
 
    assertMatches (full, queryF, sort, "JZI");
604
 
 
605
 
    sort.setSort (new SortField ("float", SortField.FLOAT) );
606
 
    assertMatches (full, queryF, sort, "ZJI");
607
 
 
608
 
    // using a nonexisting field as first sort key shouldn't make a difference:
609
 
    sort.setSort (new SortField ("nosuchfield", SortField.STRING),
610
 
        new SortField ("float", SortField.FLOAT) );
611
 
    assertMatches (full, queryF, sort, "ZJI");
612
 
 
613
 
    sort.setSort (new SortField ("float", SortField.FLOAT, true) );
614
 
    assertMatches (full, queryF, sort, "IJZ");
615
 
 
616
 
    // When a field is null for both documents, the next SortField should be used.
617
 
                // Works for
618
 
    sort.setSort (new SortField ("int", SortField.INT),
619
 
                                new SortField ("string", SortField.STRING),
620
 
        new SortField ("float", SortField.FLOAT) );
621
 
    assertMatches (full, queryG, sort, "ZWXY");
622
 
 
623
 
    // Reverse the last criterium to make sure the test didn't pass by chance
624
 
    sort.setSort (new SortField ("int", SortField.INT),
625
 
                                new SortField ("string", SortField.STRING),
626
 
        new SortField ("float", SortField.FLOAT, true) );
627
 
    assertMatches (full, queryG, sort, "ZYXW");
628
 
 
629
 
    // Do the same for a MultiSearcher
630
 
    Searcher multiSearcher=new MultiSearcher (new Searchable[] { full });
631
 
 
632
 
    sort.setSort (new SortField ("int", SortField.INT),
633
 
                                new SortField ("string", SortField.STRING),
634
 
        new SortField ("float", SortField.FLOAT) );
635
 
    assertMatches (multiSearcher, queryG, sort, "ZWXY");
636
 
 
637
 
    sort.setSort (new SortField ("int", SortField.INT),
638
 
                                new SortField ("string", SortField.STRING),
639
 
        new SortField ("float", SortField.FLOAT, true) );
640
 
    assertMatches (multiSearcher, queryG, sort, "ZYXW");
641
 
    // Don't close the multiSearcher. it would close the full searcher too!
642
 
 
643
 
    // Do the same for a ParallelMultiSearcher
644
 
    ExecutorService exec = Executors.newFixedThreadPool(_TestUtil.nextInt(random, 2, 8));
645
 
    Searcher parallelSearcher=new ParallelMultiSearcher (exec, full);
646
 
 
647
 
    sort.setSort (new SortField ("int", SortField.INT),
648
 
                                new SortField ("string", SortField.STRING),
649
 
        new SortField ("float", SortField.FLOAT) );
650
 
    assertMatches (parallelSearcher, queryG, sort, "ZWXY");
651
 
 
652
 
    sort.setSort (new SortField ("int", SortField.INT),
653
 
                                new SortField ("string", SortField.STRING),
654
 
        new SortField ("float", SortField.FLOAT, true) );
655
 
    assertMatches (parallelSearcher, queryG, sort, "ZYXW");
656
 
    parallelSearcher.close();
657
 
    exec.awaitTermination(1000, TimeUnit.MILLISECONDS);
658
 
  }
659
 
 
660
 
  // test sorts using a series of fields
661
 
  public void testSortCombos() throws Exception {
662
 
    sort.setSort (new SortField ("int", SortField.INT), new SortField ("float", SortField.FLOAT) );
663
 
    assertMatches (full, queryX, sort, "IGEAC");
664
 
 
665
 
    sort.setSort (new SortField ("int", SortField.INT, true), new SortField (null, SortField.DOC, true) );
666
 
    assertMatches (full, queryX, sort, "CEAGI");
667
 
 
668
 
    sort.setSort (new SortField ("float", SortField.FLOAT), new SortField ("string", SortField.STRING) );
669
 
    assertMatches (full, queryX, sort, "GICEA");
670
 
  }
671
 
 
672
 
  // test using a Locale for sorting strings
673
 
  public void testLocaleSort() throws Exception {
674
 
    sort.setSort (new SortField ("string", Locale.US) );
675
 
    assertMatches (full, queryX, sort, "AIGEC");
676
 
    assertMatches (full, queryY, sort, "DJHFB");
677
 
 
678
 
    sort.setSort (new SortField ("string", Locale.US, true) );
679
 
    assertMatches (full, queryX, sort, "CEGIA");
680
 
    assertMatches (full, queryY, sort, "BFHJD");
681
 
  }
682
 
 
683
 
  // test using various international locales with accented characters
684
 
  // (which sort differently depending on locale)
685
 
  public void testInternationalSort() throws Exception {
686
 
    sort.setSort (new SortField ("i18n", Locale.US));
687
 
    assertMatches (full, queryY, sort, oStrokeFirst ? "BFJHD" : "BFJDH");
688
 
 
689
 
    sort.setSort (new SortField ("i18n", new Locale("sv", "se")));
690
 
    assertMatches (full, queryY, sort, "BJDFH");
691
 
 
692
 
    sort.setSort (new SortField ("i18n", new Locale("da", "dk")));
693
 
    assertMatches (full, queryY, sort, "BJDHF");
694
 
 
695
 
    sort.setSort (new SortField ("i18n", Locale.US));
696
 
    assertMatches (full, queryX, sort, "ECAGI");
697
 
 
698
 
    sort.setSort (new SortField ("i18n", Locale.FRANCE));
699
 
    assertMatches (full, queryX, sort, "EACGI");
700
 
  }
701
 
    
702
 
    // Test the MultiSearcher's ability to preserve locale-sensitive ordering
703
 
    // by wrapping it around a single searcher
704
 
  public void testInternationalMultiSearcherSort() throws Exception {
705
 
    Searcher multiSearcher = new MultiSearcher (new Searchable[] { full });
706
 
    
707
 
    sort.setSort (new SortField ("i18n", new Locale("sv", "se")));
708
 
    assertMatches (multiSearcher, queryY, sort, "BJDFH");
709
 
    
710
 
    sort.setSort (new SortField ("i18n", Locale.US));
711
 
    assertMatches (multiSearcher, queryY, sort, oStrokeFirst ? "BFJHD" : "BFJDH");
712
 
    
713
 
    sort.setSort (new SortField ("i18n", new Locale("da", "dk")));
714
 
    assertMatches (multiSearcher, queryY, sort, "BJDHF");
715
 
  } 
716
 
 
717
 
  // test a variety of sorts using more than one searcher
718
 
  public void testMultiSort() throws Exception {
719
 
    MultiSearcher searcher = new MultiSearcher (new Searchable[] { searchX, searchY });
720
 
    runMultiSorts(searcher, false);
721
 
  }
722
 
 
723
 
  // test a variety of sorts using a parallel multisearcher
724
 
  public void testParallelMultiSort() throws Exception {
725
 
    ExecutorService exec = Executors.newFixedThreadPool(_TestUtil.nextInt(random, 2, 8));
726
 
    Searcher searcher = new ParallelMultiSearcher (exec, searchX, searchY);
727
 
    runMultiSorts(searcher, false);
728
 
    searcher.close();
729
 
    exec.awaitTermination(1000, TimeUnit.MILLISECONDS);
730
 
  }
731
 
 
732
 
  // test that the relevancy scores are the same even if
733
 
  // hits are sorted
734
 
  public void testNormalizedScores() throws Exception {
735
 
 
736
 
    // capture relevancy scores
737
 
    HashMap<String,Float> scoresX = getScores (full.search (queryX, null, 1000).scoreDocs, full);
738
 
    HashMap<String,Float> scoresY = getScores (full.search (queryY, null, 1000).scoreDocs, full);
739
 
    HashMap<String,Float> scoresA = getScores (full.search (queryA, null, 1000).scoreDocs, full);
740
 
 
741
 
    // we'll test searching locally, remote and multi
742
 
    
743
 
    MultiSearcher multi  = new MultiSearcher (new Searchable[] { searchX, searchY });
744
 
 
745
 
    // change sorting and make sure relevancy stays the same
746
 
 
747
 
    sort = new Sort();
748
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
749
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
750
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
751
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
752
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
753
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
754
 
 
755
 
    sort.setSort(SortField.FIELD_DOC);
756
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
757
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
758
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
759
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
760
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
761
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
762
 
 
763
 
    sort.setSort (new SortField("int", SortField.INT));
764
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
765
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
766
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
767
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
768
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
769
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
770
 
 
771
 
    sort.setSort (new SortField("float", SortField.FLOAT));
772
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
773
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
774
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
775
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
776
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
777
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
778
 
 
779
 
    sort.setSort (new SortField("string", SortField.STRING));
780
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
781
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
782
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
783
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
784
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
785
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
786
 
 
787
 
    sort.setSort (new SortField("int", SortField.INT),new SortField("float", SortField.FLOAT));
788
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
789
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
790
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
791
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
792
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
793
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
794
 
 
795
 
    sort.setSort (new SortField ("int", SortField.INT, true), new SortField (null, SortField.DOC, true) );
796
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
797
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
798
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
799
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
800
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
801
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
802
 
 
803
 
    sort.setSort (new SortField("int", SortField.INT),new SortField("string", SortField.STRING));
804
 
    assertSameValues (scoresX, getScores (full.search (queryX, null, 1000, sort).scoreDocs, full));
805
 
    assertSameValues (scoresX, getScores (multi.search (queryX, null, 1000, sort).scoreDocs, multi));
806
 
    assertSameValues (scoresY, getScores (full.search (queryY, null, 1000, sort).scoreDocs, full));
807
 
    assertSameValues (scoresY, getScores (multi.search (queryY, null, 1000, sort).scoreDocs, multi));
808
 
    assertSameValues (scoresA, getScores (full.search (queryA, null, 1000, sort).scoreDocs, full));
809
 
    assertSameValues (scoresA, getScores (multi.search (queryA, null, 1000, sort).scoreDocs, multi));
810
 
 
811
 
  }
812
 
 
813
 
  public void testTopDocsScores() throws Exception {
814
 
 
815
 
    // There was previously a bug in FieldSortedHitQueue.maxscore when only a single
816
 
    // doc was added.  That is what the following tests for.
817
 
    Sort sort = new Sort();
818
 
    int nDocs=10;
819
 
 
820
 
    // try to pick a query that will result in an unnormalized
821
 
    // score greater than 1 to test for correct normalization
822
 
    final TopDocs docs1 = full.search(queryE,null,nDocs,sort);
823
 
 
824
 
    // a filter that only allows through the first hit
825
 
    Filter filt = new Filter() {
826
 
      @Override
827
 
      public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
828
 
        BitSet bs = new BitSet(reader.maxDoc());
829
 
        bs.set(0, reader.maxDoc());
830
 
        bs.set(docs1.scoreDocs[0].doc);
831
 
        return new DocIdBitSet(bs);
832
 
      }
833
 
    };
834
 
 
835
 
    TopDocs docs2 = full.search(queryE, filt, nDocs, sort);
836
 
    
837
 
    assertEquals(docs1.scoreDocs[0].score, docs2.scoreDocs[0].score, 1e-6);
838
 
  }
839
 
  
840
 
  public void testSortWithoutFillFields() throws Exception {
841
 
    
842
 
    // There was previously a bug in TopFieldCollector when fillFields was set
843
 
    // to false - the same doc and score was set in ScoreDoc[] array. This test
844
 
    // asserts that if fillFields is false, the documents are set properly. It
845
 
    // does not use Searcher's default search methods (with Sort) since all set
846
 
    // fillFields to true.
847
 
    Sort[] sort = new Sort[] { new Sort(SortField.FIELD_DOC), new Sort() };
848
 
    for (int i = 0; i < sort.length; i++) {
849
 
      Query q = new MatchAllDocsQuery();
850
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, false,
851
 
          false, false, true);
852
 
      
853
 
      full.search(q, tdc);
854
 
      
855
 
      ScoreDoc[] sd = tdc.topDocs().scoreDocs;
856
 
      for (int j = 1; j < sd.length; j++) {
857
 
        assertTrue(sd[j].doc != sd[j - 1].doc);
858
 
      }
859
 
      
860
 
    }
861
 
  }
862
 
 
863
 
  public void testSortWithoutScoreTracking() throws Exception {
864
 
 
865
 
    // Two Sort criteria to instantiate the multi/single comparators.
866
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC), new Sort() };
867
 
    for (int i = 0; i < sort.length; i++) {
868
 
      Query q = new MatchAllDocsQuery();
869
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, true, false,
870
 
          false, true);
871
 
      
872
 
      full.search(q, tdc);
873
 
      
874
 
      TopDocs td = tdc.topDocs();
875
 
      ScoreDoc[] sd = td.scoreDocs;
876
 
      for (int j = 0; j < sd.length; j++) {
877
 
        assertTrue(Float.isNaN(sd[j].score));
878
 
      }
879
 
      assertTrue(Float.isNaN(td.getMaxScore()));
880
 
    }
881
 
  }
882
 
  
883
 
  public void testSortWithScoreNoMaxScoreTracking() throws Exception {
884
 
    
885
 
    // Two Sort criteria to instantiate the multi/single comparators.
886
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC), new Sort() };
887
 
    for (int i = 0; i < sort.length; i++) {
888
 
      Query q = new MatchAllDocsQuery();
889
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, true, true,
890
 
          false, true);
891
 
      
892
 
      full.search(q, tdc);
893
 
      
894
 
      TopDocs td = tdc.topDocs();
895
 
      ScoreDoc[] sd = td.scoreDocs;
896
 
      for (int j = 0; j < sd.length; j++) {
897
 
        assertTrue(!Float.isNaN(sd[j].score));
898
 
      }
899
 
      assertTrue(Float.isNaN(td.getMaxScore()));
900
 
    }
901
 
  }
902
 
  
903
 
  // MultiComparatorScoringNoMaxScoreCollector
904
 
  public void testSortWithScoreNoMaxScoreTrackingMulti() throws Exception {
905
 
    
906
 
    // Two Sort criteria to instantiate the multi/single comparators.
907
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC, SortField.FIELD_SCORE) };
908
 
    for (int i = 0; i < sort.length; i++) {
909
 
      Query q = new MatchAllDocsQuery();
910
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, true, true,
911
 
          false, true);
912
 
 
913
 
      full.search(q, tdc);
914
 
      
915
 
      TopDocs td = tdc.topDocs();
916
 
      ScoreDoc[] sd = td.scoreDocs;
917
 
      for (int j = 0; j < sd.length; j++) {
918
 
        assertTrue(!Float.isNaN(sd[j].score));
919
 
      }
920
 
      assertTrue(Float.isNaN(td.getMaxScore()));
921
 
    }
922
 
  }
923
 
  
924
 
  public void testSortWithScoreAndMaxScoreTracking() throws Exception {
925
 
    
926
 
    // Two Sort criteria to instantiate the multi/single comparators.
927
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC), new Sort() };
928
 
    for (int i = 0; i < sort.length; i++) {
929
 
      Query q = new MatchAllDocsQuery();
930
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, true, true,
931
 
          true, true);
932
 
      
933
 
      full.search(q, tdc);
934
 
      
935
 
      TopDocs td = tdc.topDocs();
936
 
      ScoreDoc[] sd = td.scoreDocs;
937
 
      for (int j = 0; j < sd.length; j++) {
938
 
        assertTrue(!Float.isNaN(sd[j].score));
939
 
      }
940
 
      assertTrue(!Float.isNaN(td.getMaxScore()));
941
 
    }
942
 
  }
943
 
  
944
 
  public void testOutOfOrderDocsScoringSort() throws Exception {
945
 
 
946
 
    // Two Sort criteria to instantiate the multi/single comparators.
947
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC), new Sort() };
948
 
    boolean[][] tfcOptions = new boolean[][] {
949
 
        new boolean[] { false, false, false },
950
 
        new boolean[] { false, false, true },
951
 
        new boolean[] { false, true, false },
952
 
        new boolean[] { false, true, true },
953
 
        new boolean[] { true, false, false },
954
 
        new boolean[] { true, false, true },
955
 
        new boolean[] { true, true, false },
956
 
        new boolean[] { true, true, true },
957
 
    };
958
 
    String[] actualTFCClasses = new String[] {
959
 
        "OutOfOrderOneComparatorNonScoringCollector", 
960
 
        "OutOfOrderOneComparatorScoringMaxScoreCollector", 
961
 
        "OutOfOrderOneComparatorScoringNoMaxScoreCollector", 
962
 
        "OutOfOrderOneComparatorScoringMaxScoreCollector", 
963
 
        "OutOfOrderOneComparatorNonScoringCollector", 
964
 
        "OutOfOrderOneComparatorScoringMaxScoreCollector", 
965
 
        "OutOfOrderOneComparatorScoringNoMaxScoreCollector", 
966
 
        "OutOfOrderOneComparatorScoringMaxScoreCollector" 
967
 
    };
968
 
    
969
 
    BooleanQuery bq = new BooleanQuery();
970
 
    // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
971
 
    // which delegates to BS if there are no mandatory clauses.
972
 
    bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
973
 
    // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
974
 
    // the clause instead of BQ.
975
 
    bq.setMinimumNumberShouldMatch(1);
976
 
    for (int i = 0; i < sort.length; i++) {
977
 
      for (int j = 0; j < tfcOptions.length; j++) {
978
 
        TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10,
979
 
            tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);
980
 
 
981
 
        assertTrue(tdc.getClass().getName().endsWith("$"+actualTFCClasses[j]));
982
 
        
983
 
        full.search(bq, tdc);
984
 
        
985
 
        TopDocs td = tdc.topDocs();
986
 
        ScoreDoc[] sd = td.scoreDocs;
987
 
        assertEquals(10, sd.length);
988
 
      }
989
 
    }
990
 
  }
991
 
  
992
 
  // OutOfOrderMulti*Collector
993
 
  public void testOutOfOrderDocsScoringSortMulti() throws Exception {
994
 
 
995
 
    // Two Sort criteria to instantiate the multi/single comparators.
996
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC, SortField.FIELD_SCORE) };
997
 
    boolean[][] tfcOptions = new boolean[][] {
998
 
        new boolean[] { false, false, false },
999
 
        new boolean[] { false, false, true },
1000
 
        new boolean[] { false, true, false },
1001
 
        new boolean[] { false, true, true },
1002
 
        new boolean[] { true, false, false },
1003
 
        new boolean[] { true, false, true },
1004
 
        new boolean[] { true, true, false },
1005
 
        new boolean[] { true, true, true },
1006
 
    };
1007
 
    String[] actualTFCClasses = new String[] {
1008
 
        "OutOfOrderMultiComparatorNonScoringCollector", 
1009
 
        "OutOfOrderMultiComparatorScoringMaxScoreCollector", 
1010
 
        "OutOfOrderMultiComparatorScoringNoMaxScoreCollector", 
1011
 
        "OutOfOrderMultiComparatorScoringMaxScoreCollector", 
1012
 
        "OutOfOrderMultiComparatorNonScoringCollector", 
1013
 
        "OutOfOrderMultiComparatorScoringMaxScoreCollector", 
1014
 
        "OutOfOrderMultiComparatorScoringNoMaxScoreCollector", 
1015
 
        "OutOfOrderMultiComparatorScoringMaxScoreCollector" 
1016
 
    };
1017
 
    
1018
 
    BooleanQuery bq = new BooleanQuery();
1019
 
    // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
1020
 
    // which delegates to BS if there are no mandatory clauses.
1021
 
    bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
1022
 
    // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
1023
 
    // the clause instead of BQ.
1024
 
    bq.setMinimumNumberShouldMatch(1);
1025
 
    for (int i = 0; i < sort.length; i++) {
1026
 
      for (int j = 0; j < tfcOptions.length; j++) {
1027
 
        TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10,
1028
 
            tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);
1029
 
 
1030
 
        assertTrue(tdc.getClass().getName().endsWith("$"+actualTFCClasses[j]));
1031
 
        
1032
 
        full.search(bq, tdc);
1033
 
        
1034
 
        TopDocs td = tdc.topDocs();
1035
 
        ScoreDoc[] sd = td.scoreDocs;
1036
 
        assertEquals(10, sd.length);
1037
 
      }
1038
 
    }
1039
 
  }
1040
 
  
1041
 
  public void testSortWithScoreAndMaxScoreTrackingNoResults() throws Exception {
1042
 
    
1043
 
    // Two Sort criteria to instantiate the multi/single comparators.
1044
 
    Sort[] sort = new Sort[] {new Sort(SortField.FIELD_DOC), new Sort() };
1045
 
    for (int i = 0; i < sort.length; i++) {
1046
 
      TopDocsCollector<Entry> tdc = TopFieldCollector.create(sort[i], 10, true, true, true, true);
1047
 
      TopDocs td = tdc.topDocs();
1048
 
      assertEquals(0, td.totalHits);
1049
 
      assertTrue(Float.isNaN(td.getMaxScore()));
1050
 
    }
1051
 
  }
1052
 
  
1053
 
  // runs a variety of sorts useful for multisearchers
1054
 
  private void runMultiSorts(Searcher multi, boolean isFull) throws Exception {
1055
 
    sort.setSort(SortField.FIELD_DOC);
1056
 
    String expected = isFull ? "ABCDEFGHIJ" : "ACEGIBDFHJ";
1057
 
    assertMatches(multi, queryA, sort, expected);
1058
 
 
1059
 
    sort.setSort(new SortField ("int", SortField.INT));
1060
 
    expected = isFull ? "IDHFGJABEC" : "IDHFGJAEBC";
1061
 
    assertMatches(multi, queryA, sort, expected);
1062
 
 
1063
 
    sort.setSort(new SortField ("int", SortField.INT), SortField.FIELD_DOC);
1064
 
    expected = isFull ? "IDHFGJABEC" : "IDHFGJAEBC";
1065
 
    assertMatches(multi, queryA, sort, expected);
1066
 
 
1067
 
    sort.setSort(new SortField("int", SortField.INT));
1068
 
    expected = isFull ? "IDHFGJABEC" : "IDHFGJAEBC";
1069
 
    assertMatches(multi, queryA, sort, expected);
1070
 
 
1071
 
    sort.setSort(new SortField ("float", SortField.FLOAT), SortField.FIELD_DOC);
1072
 
    assertMatches(multi, queryA, sort, "GDHJCIEFAB");
1073
 
 
1074
 
    sort.setSort(new SortField("float", SortField.FLOAT));
1075
 
    assertMatches(multi, queryA, sort, "GDHJCIEFAB");
1076
 
 
1077
 
    sort.setSort(new SortField("string", SortField.STRING));
1078
 
    assertMatches(multi, queryA, sort, "DJAIHGFEBC");
1079
 
 
1080
 
    sort.setSort(new SortField("int", SortField.INT, true));
1081
 
    expected = isFull ? "CABEJGFHDI" : "CAEBJGFHDI";
1082
 
    assertMatches(multi, queryA, sort, expected);
1083
 
 
1084
 
    sort.setSort(new SortField("float", SortField.FLOAT, true));
1085
 
    assertMatches(multi, queryA, sort, "BAFECIJHDG");
1086
 
 
1087
 
    sort.setSort(new SortField("string", SortField.STRING, true));
1088
 
    assertMatches(multi, queryA, sort, "CBEFGHIAJD");
1089
 
 
1090
 
    sort.setSort(new SortField("int", SortField.INT),new SortField("float", SortField.FLOAT));
1091
 
    assertMatches(multi, queryA, sort, "IDHFGJEABC");
1092
 
 
1093
 
    sort.setSort(new SortField("float", SortField.FLOAT),new SortField("string", SortField.STRING));
1094
 
    assertMatches(multi, queryA, sort, "GDHJICEFAB");
1095
 
 
1096
 
    sort.setSort(new SortField ("int", SortField.INT));
1097
 
    assertMatches(multi, queryF, sort, "IZJ");
1098
 
 
1099
 
    sort.setSort(new SortField ("int", SortField.INT, true));
1100
 
    assertMatches(multi, queryF, sort, "JZI");
1101
 
 
1102
 
    sort.setSort(new SortField ("float", SortField.FLOAT));
1103
 
    assertMatches(multi, queryF, sort, "ZJI");
1104
 
 
1105
 
    sort.setSort(new SortField ("string", SortField.STRING));
1106
 
    assertMatches(multi, queryF, sort, "ZJI");
1107
 
 
1108
 
    sort.setSort(new SortField ("string", SortField.STRING, true));
1109
 
    assertMatches(multi, queryF, sort, "IJZ");
1110
 
 
1111
 
    // up to this point, all of the searches should have "sane" 
1112
 
    // FieldCache behavior, and should have reused hte cache in several cases
1113
 
    assertSaneFieldCaches(getName() + " various");
1114
 
    // next we'll check Locale based (String[]) for 'string', so purge first
1115
 
    FieldCache.DEFAULT.purgeAllCaches();
1116
 
 
1117
 
    sort.setSort(new SortField ("string", Locale.US) );
1118
 
    assertMatches(multi, queryA, sort, "DJAIHGFEBC");
1119
 
 
1120
 
    sort.setSort(new SortField ("string", Locale.US, true) );
1121
 
    assertMatches(multi, queryA, sort, "CBEFGHIAJD");
1122
 
 
1123
 
    sort.setSort(new SortField ("string", Locale.UK) );
1124
 
    assertMatches(multi, queryA, sort, "DJAIHGFEBC");
1125
 
 
1126
 
    assertSaneFieldCaches(getName() + " Locale.US + Locale.UK");
1127
 
    FieldCache.DEFAULT.purgeAllCaches();
1128
 
 
1129
 
  }
1130
 
 
1131
 
  private void assertMatches(Searcher searcher, Query query, Sort sort, String expectedResult) throws IOException {
1132
 
    assertMatches( null, searcher, query, sort, expectedResult );
1133
 
  }
1134
 
 
1135
 
  // make sure the documents returned by the search match the expected list
1136
 
  private void assertMatches(String msg, Searcher searcher, Query query, Sort sort,
1137
 
      String expectedResult) throws IOException {
1138
 
    //ScoreDoc[] result = searcher.search (query, null, 1000, sort).scoreDocs;
1139
 
    TopDocs hits = searcher.search (query, null, Math.max(1, expectedResult.length()), sort);
1140
 
    ScoreDoc[] result = hits.scoreDocs;
1141
 
    assertEquals(expectedResult.length(),hits.totalHits);
1142
 
    StringBuilder buff = new StringBuilder(10);
1143
 
    int n = result.length;
1144
 
    for (int i=0; i<n; ++i) {
1145
 
      Document doc = searcher.doc(result[i].doc);
1146
 
      String[] v = doc.getValues("tracer");
1147
 
      for (int j=0; j<v.length; ++j) {
1148
 
        buff.append (v[j]);
1149
 
      }
1150
 
    }
1151
 
    assertEquals (msg, expectedResult, buff.toString());
1152
 
  }
1153
 
 
1154
 
  private HashMap<String,Float> getScores (ScoreDoc[] hits, Searcher searcher)
1155
 
  throws IOException {
1156
 
    HashMap<String,Float> scoreMap = new HashMap<String,Float>();
1157
 
    int n = hits.length;
1158
 
    for (int i=0; i<n; ++i) {
1159
 
      Document doc = searcher.doc(hits[i].doc);
1160
 
      String[] v = doc.getValues("tracer");
1161
 
      assertEquals (v.length, 1);
1162
 
      scoreMap.put (v[0], Float.valueOf(hits[i].score));
1163
 
    }
1164
 
    return scoreMap;
1165
 
  }
1166
 
 
1167
 
  // make sure all the values in the maps match
1168
 
  private <K, V> void assertSameValues (HashMap<K,V> m1, HashMap<K,V> m2) {
1169
 
    int n = m1.size();
1170
 
    int m = m2.size();
1171
 
    assertEquals (n, m);
1172
 
    Iterator<K> iter = m1.keySet().iterator();
1173
 
    while (iter.hasNext()) {
1174
 
      K key = iter.next();
1175
 
      V o1 = m1.get(key);
1176
 
      V o2 = m2.get(key);
1177
 
      if (o1 instanceof Float) {
1178
 
        assertEquals(((Float)o1).floatValue(), ((Float)o2).floatValue(), 1e-6);
1179
 
      } else {
1180
 
        assertEquals (m1.get(key), m2.get(key));
1181
 
      }
1182
 
    }
1183
 
  }
1184
 
 
1185
 
  public void testEmptyStringVsNullStringSort() throws Exception {
1186
 
    Directory dir = newDirectory();
1187
 
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(
1188
 
                        TEST_VERSION_CURRENT, new MockAnalyzer(random)));
1189
 
    Document doc = new Document();
1190
 
    doc.add(newField("f", "", Field.Store.NO, Field.Index.NOT_ANALYZED));
1191
 
    doc.add(newField("t", "1", Field.Store.NO, Field.Index.NOT_ANALYZED));
1192
 
    w.addDocument(doc);
1193
 
    w.commit();
1194
 
    doc = new Document();
1195
 
    doc.add(newField("t", "1", Field.Store.NO, Field.Index.NOT_ANALYZED));
1196
 
    w.addDocument(doc);
1197
 
 
1198
 
    IndexReader r = IndexReader.open(w, true);
1199
 
    w.close();
1200
 
    IndexSearcher s = newSearcher(r);
1201
 
    TopDocs hits = s.search(new TermQuery(new Term("t", "1")), null, 10, new Sort(new SortField("f", SortField.STRING)));
1202
 
    assertEquals(2, hits.totalHits);
1203
 
    // null sorts first
1204
 
    assertEquals(1, hits.scoreDocs[0].doc);
1205
 
    assertEquals(0, hits.scoreDocs[1].doc);
1206
 
    s.close();
1207
 
    r.close();
1208
 
    dir.close();
1209
 
  }
1210
 
 
1211
 
  public void testLUCENE2142() throws IOException {
1212
 
    Directory indexStore = newDirectory();
1213
 
    IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
1214
 
        TEST_VERSION_CURRENT, new MockAnalyzer(random)));
1215
 
    for (int i=0; i<5; i++) {
1216
 
        Document doc = new Document();
1217
 
        doc.add (new Field ("string", "a"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
1218
 
        doc.add (new Field ("string", "b"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
1219
 
        writer.addDocument (doc);
1220
 
    }
1221
 
    writer.forceMerge(1); // enforce one segment to have a higher unique term count in all cases
1222
 
    writer.close();
1223
 
    sort.setSort(
1224
 
        new SortField("string", SortField.STRING),
1225
 
        SortField.FIELD_DOC );
1226
 
    // this should not throw AIOOBE or RuntimeEx
1227
 
    IndexReader reader = IndexReader.open(indexStore);
1228
 
    IndexSearcher searcher = new IndexSearcher(reader);
1229
 
    searcher.search(new MatchAllDocsQuery(), null, 500, sort);
1230
 
    searcher.close();
1231
 
    reader.close();
1232
 
    indexStore.close();
1233
 
  }
1234
 
 
1235
 
  public void testCountingCollector() throws Exception {
1236
 
    Directory indexStore = newDirectory();
1237
 
    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
1238
 
    for (int i=0; i<5; i++) {
1239
 
      Document doc = new Document();
1240
 
      doc.add (new Field ("string", "a"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
1241
 
      doc.add (new Field ("string", "b"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
1242
 
      writer.addDocument (doc);
1243
 
    }
1244
 
    IndexReader reader = writer.getReader();
1245
 
    writer.close();
1246
 
 
1247
 
    IndexSearcher searcher = newSearcher(reader);
1248
 
    TotalHitCountCollector c = new TotalHitCountCollector();
1249
 
    searcher.search(new MatchAllDocsQuery(), null, c);
1250
 
    assertEquals(5, c.getTotalHits());
1251
 
    searcher.close();
1252
 
    reader.close();
1253
 
    indexStore.close();
1254
 
  }
1255
 
  
1256
 
  /** LUCENE-3390: expose bug in first round of that issue */
1257
 
  public void testSimultaneousSorts() throws IOException {
1258
 
    Sort sortMin = new Sort(new SortField ("int", SortField.INT, false).setMissingValue(new Integer(Integer.MIN_VALUE)));
1259
 
    Sort sortMax = new Sort(new SortField ("int", SortField.INT, false).setMissingValue(new Integer(Integer.MAX_VALUE)));
1260
 
    Sort sortMinRev = new Sort(new SortField ("int", SortField.INT, true).setMissingValue(new Integer(Integer.MIN_VALUE)));
1261
 
    Sort sortMaxRev = new Sort(new SortField ("int", SortField.INT, true).setMissingValue(new Integer(Integer.MAX_VALUE)));
1262
 
    
1263
 
    int ndocs = full.maxDoc();
1264
 
    TopFieldCollector collectorMin = TopFieldCollector.create(sortMin, ndocs, false, false, false, true);
1265
 
    TopFieldCollector collectorMax = TopFieldCollector.create(sortMax, ndocs, false, false, false, true);
1266
 
    TopFieldCollector collectorMinRev = TopFieldCollector.create(sortMinRev, ndocs, false, false, false, true);
1267
 
    TopFieldCollector collectorMaxRev = TopFieldCollector.create(sortMaxRev, ndocs, false, false, false, true);
1268
 
    full.search(new MatchAllDocsQuery(), MultiCollector.wrap(collectorMin, collectorMax, collectorMinRev, collectorMaxRev));
1269
 
    
1270
 
    assertIntResultsOrder(collectorMin, ndocs, false, Integer.MIN_VALUE);
1271
 
    assertIntResultsOrder(collectorMax, ndocs, false, Integer.MAX_VALUE);
1272
 
    assertIntResultsOrder(collectorMinRev, ndocs, true, Integer.MIN_VALUE);
1273
 
    assertIntResultsOrder(collectorMaxRev, ndocs, true, Integer.MAX_VALUE);
1274
 
  }
1275
 
 
1276
 
  private void assertIntResultsOrder(TopFieldCollector collector, int ndocs, boolean reverse, int missingVal) {
1277
 
    ScoreDoc[] fdocs = collector.topDocs().scoreDocs;
1278
 
    assertEquals("wrong number of docs collected", ndocs, fdocs.length);
1279
 
    int b = dataIntVal(fdocs[0].doc, missingVal);
1280
 
    for (int i=1; i<fdocs.length; i++) {
1281
 
      int a = b;
1282
 
      b = dataIntVal(fdocs[i].doc, missingVal);
1283
 
      if (reverse) {
1284
 
        // reverse of natural int order: descending
1285
 
        assertTrue(a >= b);
1286
 
      } else {
1287
 
        // natural int order: ascending
1288
 
        assertTrue( b >= a);
1289
 
      }
1290
 
    }
1291
 
  }
1292
 
 
1293
 
  private int dataIntVal(int doc, int missingVal) {
1294
 
    return data[doc][2]==null ? missingVal : Integer.parseInt(data[doc][2]);
1295
 
  }
1296
 
 
1297
 
}