mirror of https://github.com/apache/lucene.git
LUCENE-1045: Added short, byte support
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@617745 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
65ec80028c
commit
1caf5cb9ce
|
@ -27,6 +27,8 @@ New features
|
||||||
check only a specific segment or segments in your index. (Mike
|
check only a specific segment or segments in your index. (Mike
|
||||||
McCandless)
|
McCandless)
|
||||||
|
|
||||||
|
3. LUCENE-1045: Reopened this issue to add support for short and bytes.
|
||||||
|
|
||||||
Optimizations
|
Optimizations
|
||||||
|
|
||||||
1. LUCENE-705: When building a compound file, use
|
1. LUCENE-705: When building a compound file, use
|
||||||
|
|
|
@ -160,6 +160,20 @@ extends PriorityQueue {
|
||||||
if (d1 > d2) c = 1;
|
if (d1 > d2) c = 1;
|
||||||
break;
|
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:{
|
case SortField.CUSTOM:{
|
||||||
c = docA.fields[i].compareTo (docB.fields[i]);
|
c = docA.fields[i].compareTo (docB.fields[i]);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -196,6 +196,12 @@ extends PriorityQueue {
|
||||||
case SortField.DOUBLE:
|
case SortField.DOUBLE:
|
||||||
comparator = comparatorDouble(reader, fieldname);
|
comparator = comparatorDouble(reader, fieldname);
|
||||||
break;
|
break;
|
||||||
|
case SortField.SHORT:
|
||||||
|
comparator = comparatorShort(reader, fieldname);
|
||||||
|
break;
|
||||||
|
case SortField.BYTE:
|
||||||
|
comparator = comparatorByte(reader, fieldname);
|
||||||
|
break;
|
||||||
case SortField.STRING:
|
case SortField.STRING:
|
||||||
if (locale != null) comparator = comparatorStringLocale (reader, fieldname, locale);
|
if (locale != null) comparator = comparatorStringLocale (reader, fieldname, locale);
|
||||||
else comparator = comparatorString (reader, fieldname);
|
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.
|
* Returns a comparator for sorting hits according to a field containing integers.
|
||||||
* @param reader Index to use.
|
* @param reader Index to use.
|
||||||
|
|
|
@ -68,9 +68,20 @@ implements Serializable {
|
||||||
* lower values are at the front. */
|
* lower values are at the front. */
|
||||||
public static final int DOUBLE = 7;
|
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
|
/** Sort using a custom Comparator. Sort values are any Comparable and
|
||||||
* sorting is done according to natural order. */
|
* sorting is done according to natural order. */
|
||||||
public static final int CUSTOM = 9;
|
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"
|
// 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
|
// as the above static int values. Any new values must not have the same value
|
||||||
|
|
|
@ -98,22 +98,22 @@ implements Serializable {
|
||||||
// the string field to sort by string
|
// the string field to sort by string
|
||||||
// the i18n field includes accented characters for testing locale-specific sorting
|
// the i18n field includes accented characters for testing locale-specific sorting
|
||||||
private String[][] data = new String[][] {
|
private String[][] data = new String[][] {
|
||||||
// tracer contents int float string custom i18n long double
|
// tracer contents int float string custom i18n long double, 'short', byte
|
||||||
{ "A", "x a", "5", "4f", "c", "A-3", "p\u00EAche", "10", "-4.0"},//A
|
{ "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"},//B
|
{ "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"},//C
|
{ "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)},//D
|
{ "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)},//E
|
{ "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"},//F
|
{ "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"},//G
|
{ "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"},//H
|
{ "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"},//I
|
{ "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"},//J
|
{ "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},
|
{ "W", "g", "1", null, null, null, null, null, null, null, null},
|
||||||
{ "X", "g", "1", "0.1", 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},
|
{ "Y", "g", "1", "0.2", null, null, null, null, null, null, null},
|
||||||
{ "Z", "f g", 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
|
// create an index of all the documents, or just the x, or just the y documents
|
||||||
private Searcher getIndex (boolean even, boolean odd)
|
private 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][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][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 ("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
|
doc.setBoost(2); // produce some scores above 1.0
|
||||||
writer.addDocument (doc);
|
writer.addDocument (doc);
|
||||||
}
|
}
|
||||||
|
@ -203,6 +205,14 @@ implements Serializable {
|
||||||
assertMatches (full, queryX, sort, "AGICE");
|
assertMatches (full, queryX, sort, "AGICE");
|
||||||
assertMatches (full, queryY, sort, "DJHBF");
|
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 });
|
sort.setSort (new SortField[] { new SortField ("string", SortField.STRING), SortField.FIELD_DOC });
|
||||||
assertMatches (full, queryX, sort, "AIGEC");
|
assertMatches (full, queryX, sort, "AIGEC");
|
||||||
assertMatches (full, queryY, sort, "DJHFB");
|
assertMatches (full, queryY, sort, "DJHFB");
|
||||||
|
|
Loading…
Reference in New Issue