LUCENE-3191: Fix SlowCollatedStringComparator and add tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1140713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-06-28 16:30:55 +00:00
parent 8e94c0c216
commit 71d4dc370b
2 changed files with 25 additions and 12 deletions

View File

@ -33,7 +33,7 @@ import org.apache.lucene.util.BytesRef;
* This class will be removed in Lucene 5.0
*/
@Deprecated
public final class SlowCollatedStringComparator extends FieldComparator<BytesRef> {
public final class SlowCollatedStringComparator extends FieldComparator<String> {
private final String[] values;
private DocTerms currentDocTerms;
@ -99,13 +99,12 @@ public final class SlowCollatedStringComparator extends FieldComparator<BytesRef
}
@Override
public BytesRef value(int slot) {
final String s = values[slot];
return s == null ? null : new BytesRef(values[slot]);
public String value(int slot) {
return values[slot];
}
@Override
public int compareValues(BytesRef first, BytesRef second) {
public int compareValues(String first, String second) {
if (first == null) {
if (second == null) {
return 0;

View File

@ -41,6 +41,7 @@ public class TestSlowCollationMethods extends LuceneTestCase {
private static IndexReader reader;
private static Directory dir;
private static int numDocs;
private static String splitDoc;
@BeforeClass
public static void beforeClass() throws Exception {
@ -59,6 +60,7 @@ public class TestSlowCollationMethods extends LuceneTestCase {
doc.add(field);
iw.addDocument(doc);
}
splitDoc = _TestUtil.randomUnicodeString(random);
reader = iw.getReader();
iw.close();
@ -76,6 +78,15 @@ public class TestSlowCollationMethods extends LuceneTestCase {
dir = null;
}
private void doCheckSorting(TopDocs docs) throws Exception {
String prev = "";
for (ScoreDoc doc : docs.scoreDocs) {
String value = reader.document(doc.doc).get("field");
assertTrue(collator.compare(value, prev) >= 0);
prev = value;
}
}
public void testSort() throws Exception {
SortField sf = new SortField("field", new FieldComparatorSource() {
@Override
@ -83,13 +94,16 @@ public class TestSlowCollationMethods extends LuceneTestCase {
return new SlowCollatedStringComparator(numHits, fieldname, collator);
}
});
TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), null, numDocs, new Sort(sf));
String prev = "";
for (ScoreDoc doc : docs.scoreDocs) {
String value = reader.document(doc.doc).get("field");
assertTrue(collator.compare(value, prev) >= 0);
prev = value;
}
final Sort sort = new Sort(sf);
final TopDocs docs1 = searcher.search(TermRangeQuery.newStringRange("field", null, splitDoc, true, true), null, numDocs/(1+random.nextInt(4)), sort);
doCheckSorting(docs1);
final TopDocs docs2 = searcher.search(TermRangeQuery.newStringRange("field", splitDoc, null, true, true), null, numDocs/(1+random.nextInt(4)), sort);
doCheckSorting(docs2);
final TopDocs docs = TopDocs.merge(sort, numDocs/(1+random.nextInt(4)), new TopDocs[]{docs1, docs2});
doCheckSorting(docs);
}
private void doTestRanges(String startPoint, String endPoint, Query query) throws Exception {