Attached is a patch that makes it possible to supply a user-specified parser to FieldCache. For example, one might use this to process a date field as ints even if was not indexed as a decimal integer.

Comments?

Doug
Index: src/java/org/apache/lucene/search/FieldCache.java
===================================================================
--- src/java/org/apache/lucene/search/FieldCache.java	(revision 169506)
+++ src/java/org/apache/lucene/search/FieldCache.java	(working copy)
@@ -52,7 +52,23 @@
     }
   }
 
+  /** Interface to parse ints from document fields.
+   * @see #getInts(IndexReader,String,IntParser)
+   */
+  public interface IntParser {
+    /** Return an integer representation of this field's value. */
+    public int parseInt(String string);
+  }
 
+
+  /** Interface to parse floats from document fields.
+   * @see #getFloats(IndexReader,String,FloatParser)
+   */
+  public interface FloatParser {
+    /** Return an float representation of this field's value. */
+    public float parseFloat(String string);
+  }
+
   /** Expert: The cache used internally by sorting and range query classes. */
   public static FieldCache DEFAULT = new FieldCacheImpl();
 
@@ -69,6 +85,19 @@
   public int[] getInts (IndexReader reader, String field)
   throws IOException;
 
+  /** Checks the internal cache for an appropriate entry, and if none is found,
+   * reads the terms in <code>field</code> as integers and returns an array of
+   * size <code>reader.maxDoc()</code> of the value each document has in the
+   * given field.
+   * @param reader  Used to get field values.
+   * @param field   Which field contains the integers.
+   * @param parser  Computes integer for string values.
+   * @return The values in the given field for each document.
+   * @throws IOException  If any error occurs.
+   */
+  public int[] getInts (IndexReader reader, String field, IntParser parser)
+  throws IOException;
+
   /** Checks the internal cache for an appropriate entry, and if
    * none is found, reads the terms in <code>field</code> as floats and returns an array
    * of size <code>reader.maxDoc()</code> of the value each document
@@ -81,6 +110,19 @@
   public float[] getFloats (IndexReader reader, String field)
   throws IOException;
 
+  /** Checks the internal cache for an appropriate entry, and if
+   * none is found, reads the terms in <code>field</code> as floats and returns an array
+   * of size <code>reader.maxDoc()</code> of the value each document
+   * has in the given field.
+   * @param reader  Used to get field values.
+   * @param field   Which field contains the floats.
+   * @param parser  Computes float for string values.
+   * @return The values in the given field for each document.
+   * @throws IOException  If any error occurs.
+   */
+  public float[] getFloats (IndexReader reader, String field,
+                            FloatParser parser) throws IOException;
+
   /** Checks the internal cache for an appropriate entry, and if none
    * is found, reads the term values in <code>field</code> and returns an array
    * of size <code>reader.maxDoc()</code> containing the value each document
Index: src/java/org/apache/lucene/search/FieldCacheImpl.java
===================================================================
--- src/java/org/apache/lucene/search/FieldCacheImpl.java	(revision 169506)
+++ src/java/org/apache/lucene/search/FieldCacheImpl.java	(working copy)
@@ -81,7 +81,18 @@
     }
   }
 
+  private static final IntParser INT_PARSER = new IntParser() {
+      public int parseInt(String value) {
+        return Integer.parseInt(value);
+      }
+    };
 
+  private static final FloatParser FLOAT_PARSER = new FloatParser() {
+      public float parseFloat(String value) {
+        return Float.parseFloat(value);
+      }
+    };
+
   /** The internal cache. Maps Entry to array of interpreted term values. **/
   final Map cache = new WeakHashMap();
 
@@ -132,10 +143,15 @@
   }
 
   // inherit javadocs
-  public int[] getInts (IndexReader reader, String field)
+  public int[] getInts (IndexReader reader, String field) throws IOException {
+    return getInts(reader, field, INT_PARSER);
+  }
+
+  // inherit javadocs
+  public int[] getInts (IndexReader reader, String field, IntParser parser)
   throws IOException {
     field = field.intern();
-    Object ret = lookup (reader, field, SortField.INT);
+    Object ret = lookup (reader, field, parser);
     if (ret == null) {
       final int[] retArray = new int[reader.maxDoc()];
       if (retArray.length > 0) {
@@ -159,7 +175,7 @@
           termEnum.close();
         }
       }
-      store (reader, field, SortField.INT, retArray);
+      store (reader, field, parser, retArray);
       return retArray;
     }
     return (int[]) ret;
@@ -167,9 +183,15 @@
 
   // inherit javadocs
   public float[] getFloats (IndexReader reader, String field)
-  throws IOException {
+    throws IOException {
+    return getFloats(reader, field, FLOAT_PARSER);
+  }
+
+  // inherit javadocs
+  public float[] getFloats (IndexReader reader, String field,
+                            FloatParser parser) throws IOException {
     field = field.intern();
-    Object ret = lookup (reader, field, SortField.FLOAT);
+    Object ret = lookup (reader, field, parser);
     if (ret == null) {
       final float[] retArray = new float[reader.maxDoc()];
       if (retArray.length > 0) {
@@ -193,7 +215,7 @@
           termEnum.close();
         }
       }
-      store (reader, field, SortField.FLOAT, retArray);
+      store (reader, field, parser, retArray);
       return retArray;
     }
     return (float[]) ret;
@@ -388,7 +410,7 @@
           termEnum.close();
         }
       }
-      store (reader, field, SortField.CUSTOM, retArray);
+      store (reader, field, comparator, retArray);
       return retArray;
     }
     return (Comparable[]) ret;

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to