lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gsing...@apache.org
Subject svn commit: r617745 - in /lucene/java/trunk: ./ src/java/org/apache/lucene/search/ src/test/org/apache/lucene/search/
Date Sat, 02 Feb 2008 03:06:10 GMT
Author: gsingers
Date: Fri Feb  1 19:06:08 2008
New Revision: 617745

URL: http://svn.apache.org/viewvc?rev=617745&view=rev
Log:
LUCENE-1045: Added short, byte support

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java
    lucene/java/trunk/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
    lucene/java/trunk/src/java/org/apache/lucene/search/SortField.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestSort.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=617745&r1=617744&r2=617745&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Fri Feb  1 19:06:08 2008
@@ -27,6 +27,8 @@
     check only a specific segment or segments in your index.  (Mike
     McCandless)
 
+ 3. LUCENE-1045: Reopened this issue to add support for short and bytes. 
+
 Optimizations
 
  1. LUCENE-705: When building a compound file, use

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java?rev=617745&r1=617744&r2=617745&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java Fri Feb
 1 19:06:08 2008
@@ -160,6 +160,20 @@
 					if (d1 > d2) c = 1;
 					break;
         }
+        case SortField.BYTE:{
+					int i1 = ((Byte)docA.fields[i]).byteValue();
+					int i2 = ((Byte)docB.fields[i]).byteValue();
+					if (i1 < i2) c = -1;
+					if (i1 > i2) c = 1;
+					break;
+        }
+        case SortField.SHORT:{
+					int i1 = ((Short)docA.fields[i]).shortValue();
+					int i2 = ((Short)docB.fields[i]).shortValue();
+					if (i1 < i2) c = -1;
+					if (i1 > i2) c = 1;
+					break;
+        }
         case SortField.CUSTOM:{
 					c = docA.fields[i].compareTo (docB.fields[i]);
 					break;

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=617745&r1=617744&r2=617745&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 Feb 
1 19:06:08 2008
@@ -196,6 +196,12 @@
         case SortField.DOUBLE:
           comparator = comparatorDouble(reader, fieldname);
           break;
+        case SortField.SHORT:
+          comparator = comparatorShort(reader, fieldname);
+          break;
+        case SortField.BYTE:
+          comparator = comparatorByte(reader, fieldname);
+          break;
         case SortField.STRING:
           if (locale != null) comparator = comparatorStringLocale (reader, fieldname, locale);
           else comparator = comparatorString (reader, fieldname);
@@ -209,6 +215,68 @@
       return comparator;
     }
   };
+
+   /**
+   * Returns a comparator for sorting hits according to a field containing bytes.
+   * @param reader  Index to use.
+   * @param fieldname  Fieldable containg integer values.
+   * @return  Comparator for sorting hits.
+   * @throws IOException If an error occurs reading the index.
+   */
+  static ScoreDocComparator comparatorByte(final IndexReader reader, final String fieldname)
+  throws IOException {
+    final String field = fieldname.intern();
+    final byte[] fieldOrder = FieldCache.DEFAULT.getBytes(reader, field);
+    return new ScoreDocComparator() {
+
+      public final int compare (final ScoreDoc i, final ScoreDoc j) {
+        final int fi = fieldOrder[i.doc];
+        final int fj = fieldOrder[j.doc];
+        if (fi < fj) return -1;
+        if (fi > fj) return 1;
+        return 0;
+      }
+
+      public Comparable sortValue (final ScoreDoc i) {
+        return new Byte(fieldOrder[i.doc]);
+      }
+
+      public int sortType() {
+        return SortField.INT;
+      }
+    };
+  }
+
+  /**
+   * Returns a comparator for sorting hits according to a field containing shorts.
+   * @param reader  Index to use.
+   * @param fieldname  Fieldable containg integer values.
+   * @return  Comparator for sorting hits.
+   * @throws IOException If an error occurs reading the index.
+   */
+  static ScoreDocComparator comparatorShort(final IndexReader reader, final String fieldname)
+  throws IOException {
+    final String field = fieldname.intern();
+    final short[] fieldOrder = FieldCache.DEFAULT.getShorts(reader, field);
+    return new ScoreDocComparator() {
+
+      public final int compare (final ScoreDoc i, final ScoreDoc j) {
+        final int fi = fieldOrder[i.doc];
+        final int fj = fieldOrder[j.doc];
+        if (fi < fj) return -1;
+        if (fi > fj) return 1;
+        return 0;
+      }
+
+      public Comparable sortValue (final ScoreDoc i) {
+        return new Short(fieldOrder[i.doc]);
+      }
+
+      public int sortType() {
+        return SortField.SHORT;
+      }
+    };
+  }
 
   /**
    * Returns a comparator for sorting hits according to a field containing integers.

Modified: lucene/java/trunk/src/java/org/apache/lucene/search/SortField.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/SortField.java?rev=617745&r1=617744&r2=617745&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/SortField.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/SortField.java Fri Feb  1 19:06:08
2008
@@ -68,9 +68,20 @@
    * lower values are at the front. */
   public static final int DOUBLE = 7;
 
+    /**
+   * Sort using term values as encoded Shorts.  Sort values are shorts and lower values are
at the front
+   */
+  public static final int SHORT = 8;
+
+
   /** Sort using a custom Comparator.  Sort values are any Comparable and
    * sorting is done according to natural order. */
   public static final int CUSTOM = 9;
+  /**
+   * Sort using term values as encoded bytes.  Sort values are bytes and lower values are
at the front
+   */
+  public static final int BYTE = 10;
+
 
   // IMPLEMENTATION NOTE: the FieldCache.STRING_INDEX is in the same "namespace"
   // as the above static int values.  Any new values must not have the same value

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestSort.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestSort.java?rev=617745&r1=617744&r2=617745&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestSort.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestSort.java Fri Feb  1 19:06:08
2008
@@ -98,22 +98,22 @@
 	// the string field to sort by string
     // the i18n field includes accented characters for testing locale-specific sorting
 	private String[][] data = new String[][] {
-	// tracer  contents         int            float           string   custom   i18n      
        long            double
-	{   "A",   "x a",           "5",           "4f",           "c",     "A-3",   "p\u00EAche",
     "10",           "-4.0"},//A
-	{   "B",   "y a",           "5",           "3.4028235E38", "i",     "B-10",  "HAT",    
        "1000000000", "40.0"},//B
-	{   "C",   "x a b c",       "2147483647",  "1.0",          "j",     "A-2",   "p\u00E9ch\u00E9",
"99999999",   "40.00002343"},//C
-	{   "D",   "y a b c",       "-1",          "0.0f",         "a",     "C-0",   "HUT",    
        String.valueOf(Long.MAX_VALUE),           String.valueOf(Double.MIN_VALUE)},//D
-	{   "E",   "x a b c d",     "5",           "2f",           "h",     "B-8",   "peach",  
        String.valueOf(Long.MIN_VALUE),           String.valueOf(Double.MAX_VALUE)},//E
-	{   "F",   "y a b c d",     "2",           "3.14159f",     "g",     "B-1",   "H\u00C5T",
       "-44",           "343.034435444"},//F
-	{   "G",   "x a b c d",     "3",           "-1.0",         "f",     "C-100", "sin",    
        "323254543543", "4.043544"},//G
-  {   "H",   "y a b c d",     "0",           "1.4E-45",      "e",     "C-88",  "H\u00D8T",
       "1023423423005","4.043545"},//H
-	{   "I",   "x a b c d e f", "-2147483648", "1.0e+0",       "d",     "A-10",  "s\u00EDn",
       "332422459999", "4.043546"},//I
-	{   "J",   "y a b c d e f", "4",           ".5",           "b",     "C-7",   "HOT",    
        "34334543543",  "4.0000220343"},//J
-	{   "W",   "g",             "1",           null,           null,    null,    null,     
        null,           null},
-	{   "X",   "g",             "1",           "0.1",          null,    null,    null,     
        null,           null},
-	{   "Y",   "g",             "1",           "0.2",          null,    null,    null,     
        null,           null},
-	{   "Z",   "f g",           null,          null,           null,    null,    null,     
        null,           null}
-	};
+	// tracer  contents         int            float           string   custom   i18n      
        long            double, 'short', byte
+	{   "A",   "x a",           "5",           "4f",           "c",     "A-3",   "p\u00EAche",
     "10",           "-4.0", "3", "126"},//A, x
+	{   "B",   "y a",           "5",           "3.4028235E38", "i",     "B-10",  "HAT",    
        "1000000000", "40.0", "24", "1"},//B, y
+	{   "C",   "x a b c",       "2147483647",  "1.0",          "j",     "A-2",   "p\u00E9ch\u00E9",
"99999999",   "40.00002343", "125", "15"},//C, x
+	{   "D",   "y a b c",       "-1",          "0.0f",         "a",     "C-0",   "HUT",    
        String.valueOf(Long.MAX_VALUE),           String.valueOf(Double.MIN_VALUE), String.valueOf(Short.MIN_VALUE),
String.valueOf(Byte.MIN_VALUE)},//D, y
+	{   "E",   "x a b c d",     "5",           "2f",           "h",     "B-8",   "peach",  
        String.valueOf(Long.MIN_VALUE),           String.valueOf(Double.MAX_VALUE), String.valueOf(Short.MAX_VALUE),
          String.valueOf(Byte.MAX_VALUE)},//E,x
+	{   "F",   "y a b c d",     "2",           "3.14159f",     "g",     "B-1",   "H\u00C5T",
       "-44",           "343.034435444", "-3", "0"},//F,y
+	{   "G",   "x a b c d",     "3",           "-1.0",         "f",     "C-100", "sin",    
        "323254543543", "4.043544", "5", "100"},//G,x
+  {   "H",   "y a b c d",     "0",           "1.4E-45",      "e",     "C-88",  "H\u00D8T",
       "1023423423005","4.043545", "10", "-50"},//H,y
+	{   "I",   "x a b c d e f", "-2147483648", "1.0e+0",       "d",     "A-10",  "s\u00EDn",
       "332422459999", "4.043546", "-340", "51"},//I,x
+	{   "J",   "y a b c d e f", "4",           ".5",           "b",     "C-7",   "HOT",    
        "34334543543",  "4.0000220343", "300", "2"},//J,y
+	{   "W",   "g",             "1",           null,           null,    null,    null,     
        null,           null, null, null},
+	{   "X",   "g",             "1",           "0.1",          null,    null,    null,     
        null,           null, null, null},
+	{   "Y",   "g",             "1",           "0.2",          null,    null,    null,     
        null,           null, null, null},
+	{   "Z",   "f g",           null,          null,           null,    null,    null,     
        null,           null, null, null}
+ 	};
 
 	// create an index of all the documents, or just the x, or just the y documents
 	private Searcher getIndex (boolean even, boolean odd)
@@ -132,6 +132,8 @@
 				if (data[i][6] != null) doc.add (new Field ("i18n",     data[i][6], Field.Store.NO, Field.Index.UN_TOKENIZED));
         if (data[i][7] != null) doc.add (new Field ("long",     data[i][7], Field.Store.NO,
Field.Index.UN_TOKENIZED));
         if (data[i][8] != null) doc.add (new Field ("double",     data[i][8], Field.Store.NO,
Field.Index.UN_TOKENIZED));
+        if (data[i][8] != null) doc.add (new Field ("short",     data[i][9], Field.Store.NO,
Field.Index.UN_TOKENIZED));
+        if (data[i][8] != null) doc.add (new Field ("byte",     data[i][10], Field.Store.NO,
Field.Index.UN_TOKENIZED));
         doc.setBoost(2);  // produce some scores above 1.0
 				writer.addDocument (doc);
 			}
@@ -202,6 +204,14 @@
     sort.setSort (new SortField[] { new SortField ("double", SortField.DOUBLE), SortField.FIELD_DOC
});
 		assertMatches (full, queryX, sort, "AGICE");
 		assertMatches (full, queryY, sort, "DJHBF");
+
+    sort.setSort (new SortField[] { new SortField ("byte", SortField.BYTE), SortField.FIELD_DOC
});
+		assertMatches (full, queryX, sort, "CIGAE");
+		assertMatches (full, queryY, sort, "DHFBJ");
+
+    sort.setSort (new SortField[] { new SortField ("short", SortField.SHORT), SortField.FIELD_DOC
});
+		assertMatches (full, queryX, sort, "IAGCE");
+		assertMatches (full, queryY, sort, "DFHBJ");
 
     sort.setSort (new SortField[] { new SortField ("string", SortField.STRING), SortField.FIELD_DOC
});
 		assertMatches (full, queryX, sort, "AIGEC");



Mime
View raw message