Index: E:/My Documents/dev/eclipseProjects/lucene/src/test/org/apache/lucene/search/TestSort.java =================================================================== --- E:/My Documents/dev/eclipseProjects/lucene/src/test/org/apache/lucene/search/TestSort.java (revision 164171) +++ E:/My Documents/dev/eclipseProjects/lucene/src/test/org/apache/lucene/search/TestSort.java (working copy) @@ -1,3 +1,4 @@ + package org.apache.lucene.search; /** @@ -19,8 +20,11 @@ import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.index.*; import org.apache.lucene.analysis.SimpleAnalyzer; +import org.apache.lucene.document.DateField; +import org.apache.lucene.document.DateTools; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.DateTools.Resolution; import java.rmi.Naming; import java.rmi.registry.LocateRegistry; @@ -28,6 +32,7 @@ import java.io.IOException; import java.io.Serializable; import java.util.regex.Pattern; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; @@ -91,19 +96,20 @@ // the int field to sort by int // the float field to sort by float // the string field to sort by string + private long timestamp = new Date().getTime(); private String[][] data = new String[][] { - // tracer contents int float string custom - { "A", "x a", "5", "4f", "c", "A-3" }, - { "B", "y a", "5", "3.4028235E38", "i", "B-10" }, - { "C", "x a b c", "2147483647", "1.0", "j", "A-2" }, - { "D", "y a b c", "-1", "0.0f", "a", "C-0" }, - { "E", "x a b c d", "5", "2f", "h", "B-8" }, - { "F", "y a b c d", "2", "3.14159f", "g", "B-1" }, - { "G", "x a b c d", "3", "-1.0", "f", "C-100" }, - { "H", "y a b c d", "0", "1.4E-45", "e", "C-88" }, - { "I", "x a b c d e f", "-2147483648", "1.0e+0", "d", "A-10" }, - { "J", "y a b c d e f", "4", ".5", "b", "C-7" }, - { "Z", "f", null, null, null, null } + // tracer contents int float string custom custom(for DateField/DateTools decoded sort by 'long' timestamp value) + { "A", "x a", "5", "4f", "c", "A-3", Long.toString(timestamp + 7000) }, + { "B", "y a", "5", "3.4028235E38", "i", "B-10", Long.toString(timestamp + 7000) }, + { "C", "x a b c", "2147483647", "1.0", "j", "A-2", Long.toString(timestamp + 8000) }, + { "D", "y a b c", "-1", "0.0f", "a", "C-0", Long.toString(timestamp + 2000) }, + { "E", "x a b c d", "5", "2f", "h", "B-8", Long.toString(timestamp + 7000) }, + { "F", "y a b c d", "2", "3.14159f", "g", "B-1", Long.toString(timestamp + 4000) }, + { "G", "x a b c d", "3", "-1.0", "f", "C-100", Long.toString(timestamp + 5000) }, + { "H", "y a b c d", "0", "1.4E-45", "e", "C-88", Long.toString(timestamp + 3000) }, + { "I", "x a b c d e f", "-2147483648", "1.0e+0", "d", "A-10", Long.toString(timestamp + 1000) }, + { "J", "y a b c d e f", "4", ".5", "b", "C-7", Long.toString(timestamp + 6000) }, + { "Z", "f", null, null, null, null, null } }; // create an index of all the documents, or just the x, or just the y documents @@ -116,10 +122,13 @@ Document doc = new Document(); doc.add (new Field ("tracer", data[i][0], Field.Store.YES, Field.Index.NO)); doc.add (new Field ("contents", data[i][1], Field.Store.NO, Field.Index.TOKENIZED)); - if (data[i][2] != null) doc.add (new Field ("int", data[i][2], Field.Store.NO, Field.Index.UN_TOKENIZED)); - if (data[i][3] != null) doc.add (new Field ("float", data[i][3], Field.Store.NO, Field.Index.UN_TOKENIZED)); - if (data[i][4] != null) doc.add (new Field ("string", data[i][4], Field.Store.NO, Field.Index.UN_TOKENIZED)); - if (data[i][5] != null) doc.add (new Field ("custom", data[i][5], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][2] != null) doc.add (new Field ("int", data[i][2], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][3] != null) doc.add (new Field ("float", data[i][3], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][4] != null) doc.add (new Field ("string", data[i][4], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][5] != null) doc.add (new Field ("custom", data[i][5], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][6] != null) doc.add (new Field ("custom.long", data[i][6], Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][6] != null) doc.add (new Field ("custom.datefield", DateField.timeToString(Long.parseLong(data[i][6])), Field.Store.NO, Field.Index.UN_TOKENIZED)); + if (data[i][6] != null) doc.add (new Field ("custom.datetools", DateTools.timeToString(Long.parseLong(data[i][6]), DateTools.Resolution.MILLISECOND), Field.Store.NO, Field.Index.UN_TOKENIZED)); writer.addDocument (doc); } } @@ -147,7 +156,7 @@ throws IOException { return getIndex (false, false); } - + public void setUp() throws Exception { full = getFullIndex(); searchX = getXIndex(); @@ -298,6 +307,20 @@ assertMatches (full, queryX, sort, "CAIEG"); sort.setSort (new SortField ("custom", custom, true)); assertMatches (full, queryY, sort, "HJDBF"); + //custom sort for long values + sort.setSort (new SortField ("custom.long", new LongSortComparatorSource())); + assertMatches (full, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.long", new LongSortComparatorSource(), true)); + assertMatches (full, queryY, sort, "BJFHD"); + //custom sorts for encoded timestamps + sort.setSort (new SortField ("custom.datefield", TimeSortComparatorSource.DATEFIELD)); + assertMatches (full, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.datefield", TimeSortComparatorSource.DATEFIELD, true)); + assertMatches (full, queryY, sort, "BJFHD"); + sort.setSort (new SortField ("custom.datetools", TimeSortComparatorSource.DATETOOLS)); + assertMatches (full, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.datetools", TimeSortComparatorSource.DATETOOLS, true)); + assertMatches (full, queryY, sort, "BJFHD"); } // test a variety of sorts using more than one searcher @@ -332,6 +355,21 @@ assertMatches (multi, queryX, sort, "CAIEG"); sort.setSort (new SortField ("custom", custom, true)); assertMatches (multi, queryY, sort, "HJDBF"); + //custom sort for long values + sort.setSort (new SortField ("custom.long", new LongSortComparatorSource())); + assertMatches (full, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.long", new LongSortComparatorSource(), true)); + assertMatches (full, queryY, sort, "BJFHD"); + //custom sorts for encoded timestamps + sort.setSort (new SortField ("custom.datefield", TimeSortComparatorSource.DATEFIELD)); + assertMatches (multi, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.datefield", TimeSortComparatorSource.DATEFIELD, true)); + assertMatches (multi, queryY, sort, "BJFHD"); + sort.setSort (new SortField ("custom.datetools", TimeSortComparatorSource.DATETOOLS)); + assertMatches (multi, queryX, sort, "IGAEC"); + sort.setSort (new SortField ("custom.datetools", TimeSortComparatorSource.DATETOOLS, true)); + assertMatches (multi, queryY, sort, "BJFHD"); + } // test that the relevancy scores are the same even if