diff --git a/solr/src/test/org/apache/solr/search/TestSort.java b/solr/src/test/org/apache/solr/search/TestSort.java index 498723fafdf..f666d3f34ba 100755 --- a/solr/src/test/org/apache/solr/search/TestSort.java +++ b/solr/src/test/org/apache/solr/search/TestSort.java @@ -24,6 +24,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.OpenBitSet; import org.apache.solr.util.AbstractSolrTestCase; @@ -37,7 +38,7 @@ public class TestSort extends AbstractSolrTestCase { Random r = new Random(); int ndocs = 77; - int iter = 100; + int iter = 50; int qiter = 1000; int commitCount = ndocs/5 + 1; int maxval = ndocs*2; @@ -45,37 +46,45 @@ public class TestSort extends AbstractSolrTestCase { static class MyDoc { int doc; String val; + String val2; + + public String toString() { + return "{id=" +doc + " val1="+val + " val2="+val2 + "}"; + } } public void testSort() throws Exception { - Directory dir = newDirectory(); - Document smallDoc = new Document(); - // Field id = new Field("id","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS); + Directory dir = new RAMDirectory(); Field f = new Field("f","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS); - smallDoc.add(f); - - Document emptyDoc = new Document(); + Field f2 = new Field("f2","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS); for (int iterCnt = 0; iterCnt() { - public int compare(MyDoc o1, MyDoc o2) { - String v1 = o1.val==null ? "zzz" : o1.val; - String v2 = o2.val==null ? "zzz" : o2.val; - int cmp = v1.compareTo(v2); - cmp = cmp==0 ? o1.doc-o2.doc : cmp; - return cmp; - } - }); - ***/ IndexSearcher searcher = new IndexSearcher(dir, true); // System.out.println("segments="+searcher.getIndexReader().getSequentialSubReaders().length); @@ -108,19 +106,30 @@ public class TestSort extends AbstractSolrTestCase { }; int top = r.nextInt((ndocs>>3)+1)+1; - final boolean sortMissingLast = r.nextBoolean(); - final boolean reverse = !sortMissingLast; + final boolean luceneSort = r.nextBoolean(); + final boolean sortMissingLast = !luceneSort && r.nextBoolean(); + final boolean sortMissingFirst = !luceneSort && !sortMissingLast; + final boolean reverse = r.nextBoolean(); List sfields = new ArrayList(); + final boolean secondary = r.nextBoolean(); + final boolean luceneSort2 = r.nextBoolean(); + final boolean sortMissingLast2 = !luceneSort2 && r.nextBoolean(); + final boolean sortMissingFirst2 = !luceneSort2 && !sortMissingLast2; + final boolean reverse2 = r.nextBoolean(); + if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE)); // hit both use-cases of sort-missing-last - sfields.add( Sorting.getStringSortField("f", reverse, sortMissingLast, !sortMissingLast) ); + sfields.add( Sorting.getStringSortField("f", reverse, sortMissingLast, sortMissingFirst) ); + if (secondary) { + sfields.add( Sorting.getStringSortField("f2", reverse2, sortMissingLast2, sortMissingFirst2) ); + } if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE)); Sort sort = new Sort(sfields.toArray(new SortField[sfields.size()])); - // final String nullRep = sortMissingLast ? "zzz" : ""; - final String nullRep = "zzz"; + final String nullRep = luceneSort || sortMissingFirst && !reverse || sortMissingLast && reverse ? "" : "zzz"; + final String nullRep2 = luceneSort2 || sortMissingFirst2 && !reverse2 || sortMissingLast2 && reverse2 ? "" : "zzz"; boolean trackScores = r.nextBoolean(); boolean trackMaxScores = r.nextBoolean(); @@ -163,6 +172,15 @@ public class TestSort extends AbstractSolrTestCase { String v2 = o2.val==null ? nullRep : o2.val; int cmp = v1.compareTo(v2); if (reverse) cmp = -cmp; + if (cmp != 0) return cmp; + + if (secondary) { + v1 = o1.val2==null ? nullRep2 : o1.val2; + v2 = o2.val2==null ? nullRep2 : o2.val2; + cmp = v1.compareTo(v2); + if (reverse2) cmp = -cmp; + } + cmp = cmp==0 ? o1.doc-o2.doc : cmp; return cmp; } @@ -174,7 +192,10 @@ public class TestSort extends AbstractSolrTestCase { for (int j=0; j