LUCENE-5666: fix test and bug

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5666@1594327 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-05-13 18:55:27 +00:00
parent 85e4dc3300
commit 1829663cc7
4 changed files with 28 additions and 10 deletions

View File

@ -41,6 +41,7 @@ import org.apache.lucene.util.AttributeSource.State;
import org.apache.solr.analysis.SolrAnalyzer;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.QParser;
import org.apache.solr.search.Sorting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -121,7 +122,7 @@ public class PreAnalyzedField extends FieldType {
@Override
public SortField getSortField(SchemaField field, boolean top) {
field.checkSortability();
return new SortedSetSortField(field.getName(), top);
return Sorting.getTextSortField(field.getName(), top, field.sortMissingLast(), field.sortMissingFirst());
}
@Override

View File

@ -30,6 +30,7 @@ import org.apache.lucene.util.QueryBuilder;
import org.apache.solr.common.SolrException;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.QParser;
import org.apache.solr.search.Sorting;
import java.util.Map;
import java.io.IOException;
@ -97,7 +98,7 @@ public class TextField extends FieldType {
public SortField getSortField(SchemaField field, boolean reverse) {
/* :TODO: maybe warn if isTokenized(), but doesn't use LimitTokenCountFilter in it's chain? */
field.checkSortability();
return new SortedSetSortField(field.getName(), reverse);
return Sorting.getTextSortField(field.getName(), reverse, field.sortMissingLast(), field.sortMissingFirst());
}
@Override

View File

@ -40,12 +40,23 @@ public class Sorting {
* @return SortField
*/
public static SortField getStringSortField(String fieldName, boolean reverse, boolean nullLast, boolean nullFirst) {
SortField sortField = new SortField(fieldName, SortField.Type.STRING, reverse);
applyMissingFirstLast(sortField, reverse, nullLast, nullFirst);
return sortField;
}
/** Like {@link #getStringSortField}) except safe for tokenized fields */
public static SortField getTextSortField(String fieldName, boolean reverse, boolean nullLast, boolean nullFirst) {
SortField sortField = new SortedSetSortField(fieldName, reverse);
applyMissingFirstLast(sortField, reverse, nullLast, nullFirst);
return sortField;
}
private static void applyMissingFirstLast(SortField in, boolean reverse, boolean nullLast, boolean nullFirst) {
if (nullFirst && nullLast) {
throw new IllegalArgumentException("Cannot specify missing values as both first and last");
}
SortField sortField = new SortField(fieldName, SortField.Type.STRING, reverse);
// 4 cases:
// missingFirst / forward: default lucene behavior
// missingFirst / reverse: set sortMissingLast
@ -53,12 +64,11 @@ public class Sorting {
// missingLast / reverse: default lucene behavior
if (nullFirst && reverse) {
sortField.setMissingValue(SortField.STRING_LAST);
in.setMissingValue(SortField.STRING_LAST);
} else if (nullLast && !reverse) {
sortField.setMissingValue(SortField.STRING_LAST);
in.setMissingValue(SortField.STRING_LAST);
}
return sortField;
}
}

View File

@ -21,7 +21,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
@ -50,6 +52,7 @@ import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.uninverting.UninvertingReader;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.TestUtil;
@ -221,8 +224,11 @@ public class TestSort extends SolrTestCaseJ4 {
}
iw.shutdown();
Map<String,UninvertingReader.Type> mapping = new HashMap<>();
mapping.put("f", UninvertingReader.Type.SORTED);
mapping.put("f2", UninvertingReader.Type.SORTED);
DirectoryReader reader = DirectoryReader.open(dir);
DirectoryReader reader = UninvertingReader.wrap(DirectoryReader.open(dir), mapping);
IndexSearcher searcher = new IndexSearcher(reader);
// System.out.println("segments="+searcher.getIndexReader().getSequentialSubReaders().length);
assertTrue(reader.leaves().size() > 1);