diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSort.java b/lucene/core/src/test/org/apache/lucene/search/TestSort.java index e13170fe0f5..f0c8b7a3970 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSort.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSort.java @@ -1332,74 +1332,6 @@ public class TestSort extends LuceneTestCase { assertEquals(msg, expectedResult, buff.toString()); } - public void testEmptyStringVsNullStringSort() throws Exception { - Directory dir = newDirectory(); - IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( - TEST_VERSION_CURRENT, new MockAnalyzer(random()))); - Document doc = new Document(); - doc.add(newStringField("f", "", Field.Store.NO)); - doc.add(newStringField("t", "1", Field.Store.NO)); - w.addDocument(doc); - w.commit(); - doc = new Document(); - doc.add(newStringField("t", "1", Field.Store.NO)); - w.addDocument(doc); - - IndexReader r = DirectoryReader.open(w, true); - w.close(); - IndexSearcher s = newSearcher(r); - TopDocs hits = s.search(new TermQuery(new Term("t", "1")), null, 10, new Sort(new SortField("f", SortField.Type.STRING))); - assertEquals(2, hits.totalHits); - // null sorts first - assertEquals(1, hits.scoreDocs[0].doc); - assertEquals(0, hits.scoreDocs[1].doc); - r.close(); - dir.close(); - } - - public void testLUCENE2142() throws IOException { - Directory indexStore = newDirectory(); - IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig( - TEST_VERSION_CURRENT, new MockAnalyzer(random()))); - for(int i=0; i<5; i++) { - Document doc = new Document(); - doc.add(new StringField("string", "a"+i, Field.Store.NO)); - doc.add(new StringField("string", "b"+i, Field.Store.NO)); - writer.addDocument(doc); - } - writer.forceMerge(1); // enforce one segment to have a higher unique term count in all cases - writer.close(); - sort.setSort( - new SortField("string", SortField.Type.STRING), - SortField.FIELD_DOC); - // this should not throw AIOOBE or RuntimeEx - IndexReader reader = DirectoryReader.open(indexStore); - IndexSearcher searcher = new IndexSearcher(reader); - searcher.search(new MatchAllDocsQuery(), null, 500, sort); - reader.close(); - indexStore.close(); - } - - public void testCountingCollector() throws Exception { - Directory indexStore = newDirectory(); - RandomIndexWriter writer = new RandomIndexWriter(random(), indexStore); - for(int i=0; i<5; i++) { - Document doc = new Document(); - doc.add(new StringField("string", "a"+i, Field.Store.NO)); - doc.add(new StringField("string", "b"+i, Field.Store.NO)); - writer.addDocument(doc); - } - IndexReader reader = writer.getReader(); - writer.close(); - - IndexSearcher searcher = newSearcher(reader); - TotalHitCountCollector c = new TotalHitCountCollector(); - searcher.search(new MatchAllDocsQuery(), null, c); - assertEquals(5, c.getTotalHits()); - reader.close(); - indexStore.close(); - } - private static class RandomFilter extends Filter { private final Random random; private float density; diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSort2.java b/lucene/core/src/test/org/apache/lucene/search/TestSort2.java index 1ecc6bbe487..7cc45716900 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSort2.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSort2.java @@ -19,10 +19,15 @@ package org.apache.lucene.search; import java.io.IOException; +import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.StringField; +import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.RandomIndexWriter; +import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -69,4 +74,72 @@ public class TestSort2 extends LuceneTestCase { ir.close(); dir.close(); } + + public void testCountingCollector() throws Exception { + Directory indexStore = newDirectory(); + RandomIndexWriter writer = new RandomIndexWriter(random(), indexStore); + for(int i=0; i<5; i++) { + Document doc = new Document(); + doc.add(new StringField("string", "a"+i, Field.Store.NO)); + doc.add(new StringField("string", "b"+i, Field.Store.NO)); + writer.addDocument(doc); + } + IndexReader reader = writer.getReader(); + writer.close(); + + IndexSearcher searcher = newSearcher(reader); + TotalHitCountCollector c = new TotalHitCountCollector(); + searcher.search(new MatchAllDocsQuery(), null, c); + assertEquals(5, c.getTotalHits()); + reader.close(); + indexStore.close(); + } + + public void testEmptyStringVsNullStringSort() throws Exception { + Directory dir = newDirectory(); + IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + Document doc = new Document(); + doc.add(newStringField("f", "", Field.Store.NO)); + doc.add(newStringField("t", "1", Field.Store.NO)); + w.addDocument(doc); + w.commit(); + doc = new Document(); + doc.add(newStringField("t", "1", Field.Store.NO)); + w.addDocument(doc); + + IndexReader r = DirectoryReader.open(w, true); + w.close(); + IndexSearcher s = newSearcher(r); + TopDocs hits = s.search(new TermQuery(new Term("t", "1")), null, 10, new Sort(new SortField("f", SortField.Type.STRING))); + assertEquals(2, hits.totalHits); + // null sorts first + assertEquals(1, hits.scoreDocs[0].doc); + assertEquals(0, hits.scoreDocs[1].doc); + r.close(); + dir.close(); + } + + public void testLUCENE2142() throws IOException { + Directory indexStore = newDirectory(); + IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig( + TEST_VERSION_CURRENT, new MockAnalyzer(random()))); + for(int i=0; i<5; i++) { + Document doc = new Document(); + doc.add(new StringField("string", "a"+i, Field.Store.NO)); + doc.add(new StringField("string", "b"+i, Field.Store.NO)); + writer.addDocument(doc); + } + writer.forceMerge(1); // enforce one segment to have a higher unique term count in all cases + writer.close(); + Sort sort = new Sort( + new SortField("string", SortField.Type.STRING), + SortField.FIELD_DOC); + // this should not throw AIOOBE or RuntimeEx + IndexReader reader = DirectoryReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); + searcher.search(new MatchAllDocsQuery(), null, 500, sort); + reader.close(); + indexStore.close(); + } }