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