lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r768275 - /lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
Date Fri, 24 Apr 2009 12:30:11 GMT
Author: markrmiller
Date: Fri Apr 24 12:30:10 2009
New Revision: 768275

URL: http://svn.apache.org/viewvc?rev=768275&view=rev
Log:
put back auto resolution that LUCENE-1483 moved out - needed for those using fshq without
indexsearcher

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java?rev=768275&r1=768274&r2=768275&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java Fri Apr 24
12:30:10 2009
@@ -53,10 +53,18 @@
     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 @@
       }
       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 @@
         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 @@
       }
     };
   }
+
+  /**
+   * 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+"'");
+    }
+  }
 }



Mime
View raw message