Skip to content

Apache Lucene open-source search software

move detectType to SortField

Mark Robert Millerb839a6539510
diff --git a/src/java/org/apache/lucene/search/FieldValueHitQueue.java b/src/java/org/apache/lucene/search/FieldValueHitQueue.java
index 54e2f68d146..5d330d654a4 100644
--- a/src/java/org/apache/lucene/search/FieldValueHitQueue.java
+++ b/src/java/org/apache/lucene/search/FieldValueHitQueue.java
@@ -20,8 +20,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.TermEnum;
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
* 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.
*
* <b>NOTE:</b> This API is experimental and might change in
@@ -231,53 +229,4 @@ public abstract class FieldValueHitQueue extends PriorityQueue {
SortField[] getFields() {
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();
- }
- }
}
diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.java
index d307b02c376..da92d0a2293 100644
--- a/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -211,7 +211,7 @@ public class IndexSearcher extends Searcher {
int type = field.getType();
// Resolve AUTO into its true type
if (type == SortField.AUTO) {
- int autotype = FieldValueHitQueue.detectFieldType(reader, fieldname);
+ int autotype = SortField.detectFieldType(reader, fieldname);
if (autotype == SortField.STRING) {
fields[i] = new SortField (fieldname, field.getLocale(), field.getReverse());
} else {
diff --git a/src/java/org/apache/lucene/search/SortField.java b/src/java/org/apache/lucene/search/SortField.java
index 9d65f78e3c9..7c5b31ab720 100644
--- a/src/java/org/apache/lucene/search/SortField.java
+++ b/src/java/org/apache/lucene/search/SortField.java
@@ -21,6 +21,10 @@ import java.io.IOException;
import java.io.Serializable;
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
* field. Fields must be indexed in order to sort by them.
@@ -488,4 +492,53 @@ implements Serializable {
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();
+ }
+ }
}