Apache Solr open-source search software
LUCENE-1045: Added short, byte support
Grant Ingersoll1caf5cb9ce87
diff --git a/CHANGES.txt b/CHANGES.txtindex a44c603e757..ef1285cc8a5 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -27,6 +27,8 @@ New featurescheck only a specific segment or segments in your index. (MikeMcCandless)+ 3. LUCENE-1045: Reopened this issue to add support for short and bytes.+Optimizations1. LUCENE-705: When building a compound file, usediff --git a/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java b/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.javaindex 208ed860d23..7d99e3388b8 100644--- a/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java+++ b/src/java/org/apache/lucene/search/FieldDocSortedHitQueue.java@@ -160,6 +160,20 @@ extends PriorityQueue {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;diff --git a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java b/src/java/org/apache/lucene/search/FieldSortedHitQueue.javaindex a1d56ea20da..895bf880e47 100644--- a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java+++ b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java@@ -196,6 +196,12 @@ extends PriorityQueue {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);@@ -210,6 +216,68 @@ extends PriorityQueue {}};+ /**+ * 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.* @param reader Index to use.diff --git a/src/java/org/apache/lucene/search/SortField.java b/src/java/org/apache/lucene/search/SortField.javaindex 474bc4bbd14..8247ffbb7c1 100644--- a/src/java/org/apache/lucene/search/SortField.java+++ b/src/java/org/apache/lucene/search/SortField.java@@ -68,9 +68,20 @@ implements Serializable {* 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 valuediff --git a/src/test/org/apache/lucene/search/TestSort.java b/src/test/org/apache/lucene/search/TestSort.javaindex 8758f4db421..bb8ac51a570 100644--- a/src/test/org/apache/lucene/search/TestSort.java+++ b/src/test/org/apache/lucene/search/TestSort.java@@ -98,22 +98,22 @@ implements Serializable {// the string field to sort by string// the i18n field includes accented characters for testing locale-specific sortingprivate 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 documentsprivate Searcher getIndex (boolean even, boolean odd)@@ -132,6 +132,8 @@ implements Serializable {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.0writer.addDocument (doc);}@@ -203,6 +205,14 @@ implements Serializable {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");assertMatches (full, queryY, sort, "DJHFB");