put back auto resolution that LUCENE-1483 moved out - needed for those using fshq without indexsearcher

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@768275 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2009-04-24 12:30:10 +00:00
parent 5807d11b70
commit 2bed56df6a
1 changed files with 43 additions and 5 deletions

View File

@ -53,10 +53,18 @@ extends PriorityQueue {
this.fields = new SortField[n];
for (int i=0; i<n; ++i) {
String fieldname = fields[i].getField();
// AUTO is resolved before we are called
assert fields[i].getType() != SortField.AUTO;
comparators[i] = getCachedComparator (reader, fieldname, fields[i].getType(), fields[i].getParser(), fields[i].getLocale(), fields[i].getFactory());
this.fields[i] = fields[i];
// new SortField instances must only be created when auto-detection is in use
if (fields[i].getType() == SortField.AUTO) {
if (comparators[i].sortType() == SortField.STRING) {
this.fields[i] = new SortField (fieldname, fields[i].getLocale(), fields[i].getReverse());
} else {
this.fields[i] = new SortField (fieldname, comparators[i].sortType(), fields[i].getReverse());
}
} else {
assert comparators[i].sortType() == fields[i].getType();
this.fields[i] = fields[i];
}
}
initialize (size);
}
@ -187,6 +195,9 @@ extends PriorityQueue {
}
ScoreDocComparator comparator;
switch (type) {
case SortField.AUTO:
comparator = comparatorAuto (reader, fieldname);
break;
case SortField.INT:
comparator = comparatorInt (reader, fieldname, (FieldCache.IntParser)parser);
break;
@ -212,8 +223,6 @@ extends PriorityQueue {
case SortField.CUSTOM:
comparator = factory.newComparator (reader, fieldname);
break;
case SortField.AUTO:
throw new IllegalStateException("Auto should be resolved before now");
default:
throw new RuntimeException ("unknown field type: "+type);
}
@ -488,4 +497,33 @@ extends PriorityQueue {
}
};
}
/**
* Returns a comparator for sorting hits according to values in the given field.
* The terms in the field are looked at to determine whether they contain integers,
* floats or strings. Once the type is determined, one of the other static methods
* in this class is called to get the comparator.
* @param reader Index to use.
* @param fieldname Fieldable containg values.
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
static ScoreDocComparator comparatorAuto (final IndexReader reader, final String fieldname)
throws IOException {
final String field = fieldname.intern();
Object lookupArray = ExtendedFieldCache.EXT_DEFAULT.getAuto (reader, field);
if (lookupArray instanceof FieldCache.StringIndex) {
return comparatorString (reader, field);
} else if (lookupArray instanceof int[]) {
return comparatorInt (reader, field, null);
} else if (lookupArray instanceof long[]) {
return comparatorLong (reader, field, null);
} else if (lookupArray instanceof float[]) {
return comparatorFloat (reader, field, null);
} else if (lookupArray instanceof String[]) {
return comparatorString (reader, field);
} else {
throw new RuntimeException ("unknown data type in field '"+field+"'");
}
}
}