move detectType to SortField

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@768385 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2009-04-24 17:59:04 +00:00
parent 91dcc5cfcb
commit b839a65395
3 changed files with 55 additions and 53 deletions

View File

@ -20,8 +20,6 @@ package org.apache.lucene.search;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.util.PriorityQueue; import org.apache.lucene.util.PriorityQueue;
/** /**
@ -31,7 +29,7 @@ import org.apache.lucene.util.PriorityQueue;
* *
* This class will not resolve SortField.AUTO types, and expects the type * This class will not resolve SortField.AUTO types, and expects the type
* of all SortFields used for construction to already have been resolved. * of all SortFields used for construction to already have been resolved.
* {@link #detectFieldType(IndexReader, String)} is a utility method which * {@link SortField#detectFieldType(IndexReader, String)} is a utility method which
* may be used for field type detection. * may be used for field type detection.
* *
* <b>NOTE:</b> This API is experimental and might change in * <b>NOTE:</b> This API is experimental and might change in
@ -231,53 +229,4 @@ public abstract class FieldValueHitQueue extends PriorityQueue {
SortField[] getFields() { SortField[] getFields() {
return fields; return fields;
} }
/** Attempts to detect the given field type for an IndexReader. */
static int detectFieldType(IndexReader reader, String fieldKey) throws IOException {
String field = fieldKey.intern();
TermEnum enumerator = reader.terms(new Term(field));
try {
Term term = enumerator.term();
if (term == null) {
throw new RuntimeException("no terms in field " + field + " - cannot determine sort type");
}
int ret = 0;
if (term.field() == field) {
String termtext = term.text().trim();
/**
* Java 1.4 level code:
if (pIntegers.matcher(termtext).matches())
return IntegerSortedHitQueue.comparator (reader, enumerator, field);
else if (pFloats.matcher(termtext).matches())
return FloatSortedHitQueue.comparator (reader, enumerator, field);
*/
// Java 1.3 level code:
try {
Integer.parseInt (termtext);
ret = SortField.INT;
} catch (NumberFormatException nfe1) {
try {
Long.parseLong(termtext);
ret = SortField.LONG;
} catch (NumberFormatException nfe2) {
try {
Float.parseFloat (termtext);
ret = SortField.FLOAT;
} catch (NumberFormatException nfe3) {
ret = SortField.STRING;
}
}
}
} else {
throw new RuntimeException("field \"" + field + "\" does not appear to be indexed");
}
return ret;
} finally {
enumerator.close();
}
}
} }

View File

@ -211,7 +211,7 @@ public class IndexSearcher extends Searcher {
int type = field.getType(); int type = field.getType();
// Resolve AUTO into its true type // Resolve AUTO into its true type
if (type == SortField.AUTO) { if (type == SortField.AUTO) {
int autotype = FieldValueHitQueue.detectFieldType(reader, fieldname); int autotype = SortField.detectFieldType(reader, fieldname);
if (autotype == SortField.STRING) { if (autotype == SortField.STRING) {
fields[i] = new SortField (fieldname, field.getLocale(), field.getReverse()); fields[i] = new SortField (fieldname, field.getLocale(), field.getReverse());
} else { } else {

View File

@ -21,6 +21,10 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.Locale; import java.util.Locale;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
/** /**
* Stores information about how to sort documents by terms in an individual * Stores information about how to sort documents by terms in an individual
* field. Fields must be indexed in order to sort by them. * field. Fields must be indexed in order to sort by them.
@ -488,4 +492,53 @@ implements Serializable {
throw new IllegalStateException("Illegal sort type: " + type); throw new IllegalStateException("Illegal sort type: " + type);
} }
} }
/** Attempts to detect the given field type for an IndexReader. */
static int detectFieldType(IndexReader reader, String fieldKey) throws IOException {
String field = fieldKey.intern();
TermEnum enumerator = reader.terms(new Term(field));
try {
Term term = enumerator.term();
if (term == null) {
throw new RuntimeException("no terms in field " + field + " - cannot determine sort type");
}
int ret = 0;
if (term.field() == field) {
String termtext = term.text().trim();
/**
* Java 1.4 level code:
if (pIntegers.matcher(termtext).matches())
return IntegerSortedHitQueue.comparator (reader, enumerator, field);
else if (pFloats.matcher(termtext).matches())
return FloatSortedHitQueue.comparator (reader, enumerator, field);
*/
// Java 1.3 level code:
try {
Integer.parseInt (termtext);
ret = SortField.INT;
} catch (NumberFormatException nfe1) {
try {
Long.parseLong(termtext);
ret = SortField.LONG;
} catch (NumberFormatException nfe2) {
try {
Float.parseFloat (termtext);
ret = SortField.FLOAT;
} catch (NumberFormatException nfe3) {
ret = SortField.STRING;
}
}
}
} else {
throw new RuntimeException("field \"" + field + "\" does not appear to be indexed");
}
return ret;
} finally {
enumerator.close();
}
}
} }