Apache Solr open-source search software
LUCENE-6766: add another random test case; move early terminating collector to core
Mike McCandless87690f8b13b1
diff --git a/lucene/core/src/java/org/apache/lucene/index/DocIDMerger.java b/lucene/core/src/java/org/apache/lucene/index/DocIDMerger.javaindex 2d4198b0e2b..eec3301036f 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocIDMerger.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocIDMerger.java@@ -58,6 +58,7 @@ public class DocIDMerger<T extends DocIDMerger.Sub> {this.subs = subs;if (indexIsSorted) {+ // nocommit: we could optimize the (silly) single-sub case and pretend it's a concatenation insteadqueue = new PriorityQueue<T>(maxCount) {@Overrideprotected boolean lessThan(Sub a, Sub b) {diff --git a/lucene/core/src/java/org/apache/lucene/index/MergeState.java b/lucene/core/src/java/org/apache/lucene/index/MergeState.javaindex 95609837ff5..63eab98e548 100644--- a/lucene/core/src/java/org/apache/lucene/index/MergeState.java+++ b/lucene/core/src/java/org/apache/lucene/index/MergeState.java@@ -209,20 +209,16 @@ public class MergeState {final Sorter sorter = new Sorter(indexSort);List<CodecReader> readers = new ArrayList<>(originalReaders.size());- //System.out.println("MergeState.maybeSortReaders indexSort=" + indexSort);-for (CodecReader leaf : originalReaders) {Sort segmentSort = leaf.getIndexSort();- //System.out.println(" leaf=" + leaf + " sort=" + segmentSort);if (segmentSort == null) {// TODO: fix IW to also sort when flushing? It's somewhat tricky because of stored fields and term vectors, which write "live"- // to the files on each indexed document:+ // to their index files on each indexed document:// This segment was written by flush, so documents are not yet sorted, so we sort them now:Sorter.DocMap sortDocMap = sorter.sort(leaf);if (sortDocMap != null) {- //System.out.println(" sort!");leaf = SlowCodecReaderWrapper.wrap(SortingLeafReader.wrap(new MergeReaderWrapper(leaf), sortDocMap));leafDocMaps[readers.size()] = new DocMap() {@Overridediff --git a/lucene/core/src/java/org/apache/lucene/index/MultiSorter.java b/lucene/core/src/java/org/apache/lucene/index/MultiSorter.javaindex 1c67fd5cb34..3448c9064c5 100644--- a/lucene/core/src/java/org/apache/lucene/index/MultiSorter.java+++ b/lucene/core/src/java/org/apache/lucene/index/MultiSorter.java@@ -36,6 +36,8 @@ final class MultiSorter {* documents into the merged segment. The documents for each incoming leaf reader must already be sorted by the same sort! */static MergeState.DocMap[] sort(Sort sort, List<CodecReader> readers) throws IOException {+ // nocommit optimize if only 1 reader is incoming+SortField fields[] = sort.getSort();final CrossReaderComparator[] comparators = new CrossReaderComparator[fields.length];for(int i=0;i<fields.length;i++) {diff --git a/lucene/misc/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.java b/lucene/core/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.javasimilarity index 99%rename from lucene/misc/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.javarename to lucene/core/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.javaindex 731c33551ee..1af1b9f35e2 100644--- a/lucene/misc/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.java+++ b/lucene/core/src/java/org/apache/lucene/search/EarlyTerminatingSortingCollector.java@@ -53,8 +53,6 @@ import org.apache.lucene.search.TotalHitCountCollector;* @lucene.experimental*/-// nocommit move to core too-public class EarlyTerminatingSortingCollector extends FilterCollector {/** Returns whether collection can be early-terminated if it sorts with thediff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.javaindex 3eb30ec77c3..e14606ee2be 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java@@ -54,13 +54,18 @@ import org.apache.lucene.index.PointValues.Relation;import org.apache.lucene.index.TermsEnum.SeekStatus;import org.apache.lucene.search.CollectionStatistics;import org.apache.lucene.search.DocIdSetIterator;+import org.apache.lucene.search.EarlyTerminatingSortingCollector;+import org.apache.lucene.search.FieldDoc;import org.apache.lucene.search.IndexSearcher;+import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.Query;+import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.Sort;import org.apache.lucene.search.SortField;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TermStatistics;import org.apache.lucene.search.TopDocs;+import org.apache.lucene.search.TopFieldCollector;import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.store.Directory;import org.apache.lucene.util.Bits;@@ -1113,4 +1118,185 @@ public class TestIndexSorting extends LuceneTestCase {assertReaderEquals("left: sorted by hand; right: sorted by Lucene", r1, r2);IOUtils.close(w1, w2, r1, r2, dir1, dir2);}++ private static final class RandomDoc {+ public final int id;+ public final int intValue;+ public final long longValue;+ public final float floatValue;+ public final double doubleValue;+ public final byte[] bytesValue;+ // nocommit postings, points, term vectors++ public RandomDoc(int id) {+ this.id = id;+ intValue = random().nextInt();+ longValue = random().nextLong();+ floatValue = random().nextFloat();+ doubleValue = random().nextDouble();+ bytesValue = new byte[TestUtil.nextInt(random(), 1, 50)];+ random().nextBytes(bytesValue);+ }+ }++ private static Sort randomSort() {+ int numFields = TestUtil.nextInt(random(), 1, 3);+ SortField[] sortFields = new SortField[numFields];+ for(int i=0;i<numFields-1;i++) {+ boolean reversed = random().nextBoolean();+ SortField sortField;+ switch(random().nextInt(5)) {+ case 0:+ sortField = new SortField("int", SortField.Type.INT, reversed);+ if (random().nextBoolean()) {+ sortField.setMissingValue(random().nextInt());+ }+ break;+ case 1:+ sortField = new SortField("long", SortField.Type.LONG, reversed);+ if (random().nextBoolean()) {+ sortField.setMissingValue(random().nextLong());+ }+ break;+ case 2:+ sortField = new SortField("float", SortField.Type.FLOAT, reversed);+ if (random().nextBoolean()) {+ sortField.setMissingValue(random().nextFloat());+ }+ break;+ case 3:+ sortField = new SortField("double", SortField.Type.DOUBLE, reversed);+ if (random().nextBoolean()) {+ sortField.setMissingValue(random().nextDouble());+ }+ break;+ case 4:+ sortField = new SortField("bytes", SortField.Type.STRING, reversed);+ if (random().nextBoolean()) {+ sortField.setMissingValue(SortField.STRING_LAST);+ }+ break;+ default:+ throw new AssertionError();+ }+ sortFields[i] = sortField;+ }++ // tie-break by id:+ sortFields[numFields-1] = new SortField("id", SortField.Type.INT);++ return new Sort(sortFields);+ }++ // pits index time sorting against query time sorting+ public void testRandom3() throws Exception {+ int numDocs;+ if (TEST_NIGHTLY) {+ numDocs = atLeast(100000);+ } else {+ numDocs = atLeast(1000);+ }+ List<RandomDoc> docs = new ArrayList<>();++ Sort sort = randomSort();+ if (VERBOSE) {+ System.out.println("TEST: numDocs=" + numDocs + " use sort=" + sort);+ }++ // no index sorting, all search-time sorting:+ Directory dir1 = newFSDirectory(createTempDir());+ IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random()));+ IndexWriter w1 = new IndexWriter(dir1, iwc1);++ // use index sorting:+ Directory dir2 = newFSDirectory(createTempDir());+ IndexWriterConfig iwc2 = newIndexWriterConfig(new MockAnalyzer(random()));+ iwc2.setIndexSort(sort);+ IndexWriter w2 = new IndexWriter(dir2, iwc2);++ for(int id=0;id<numDocs;id++) {+ RandomDoc docValues = new RandomDoc(id);+ docs.add(docValues);+ if (VERBOSE) {+ System.out.println("TEST: doc id=" + id);+ System.out.println(" int=" + docValues.intValue);+ System.out.println(" long=" + docValues.longValue);+ System.out.println(" float=" + docValues.floatValue);+ System.out.println(" double=" + docValues.doubleValue);+ System.out.println(" bytes=" + new BytesRef(docValues.bytesValue));+ }++ Document doc = new Document();+ doc.add(new StringField("id", Integer.toString(id), Field.Store.YES));+ doc.add(new NumericDocValuesField("id", id));+ doc.add(new NumericDocValuesField("int", docValues.intValue));+ doc.add(new NumericDocValuesField("long", docValues.longValue));+ doc.add(new DoubleDocValuesField("double", docValues.doubleValue));+ doc.add(new FloatDocValuesField("float", docValues.floatValue));+ doc.add(new SortedDocValuesField("bytes", new BytesRef(docValues.bytesValue)));+ w1.addDocument(doc);+ w2.addDocument(doc);+ // nocommit do some deletions+ }+ DirectoryReader r1 = DirectoryReader.open(w1);+ IndexSearcher s1 = newSearcher(r1);++ if (random().nextBoolean()) {+ int maxSegmentCount = TestUtil.nextInt(random(), 1, 5);+ if (VERBOSE) {+ System.out.println("TEST: now forceMerge(" + maxSegmentCount + ")");+ }+ w2.forceMerge(maxSegmentCount);+ }++ DirectoryReader r2 = DirectoryReader.open(w2);+ IndexSearcher s2 = newSearcher(r2);++ /*+ System.out.println("TEST: full index:");+ SortedDocValues docValues = MultiDocValues.getSortedValues(r2, "bytes");+ for(int i=0;i<r2.maxDoc();i++) {+ System.out.println(" doc " + i + " id=" + r2.document(i).get("id") + " bytes=" + docValues.get(i));+ }+ */++ for(int iter=0;iter<100;iter++) {+ int numHits = TestUtil.nextInt(random(), 1, numDocs);+ if (VERBOSE) {+ System.out.println("TEST: iter=" + iter + " numHits=" + numHits);+ }++ TopFieldCollector c1 = TopFieldCollector.create(sort, numHits, true, true, true);+ s1.search(new MatchAllDocsQuery(), c1);+ TopDocs hits1 = c1.topDocs();++ TopFieldCollector c2 = TopFieldCollector.create(sort, numHits, true, true, true);+ EarlyTerminatingSortingCollector c3 = new EarlyTerminatingSortingCollector(c2, sort, numHits);+ s2.search(new MatchAllDocsQuery(), c3);++ TopDocs hits2 = c2.topDocs();++ if (VERBOSE) {+ System.out.println(" topDocs query-time sort: totalHits=" + hits1.totalHits);+ for(ScoreDoc scoreDoc : hits1.scoreDocs) {+ System.out.println(" " + scoreDoc.doc);+ }+ System.out.println(" topDocs index-time sort: totalHits=" + hits2.totalHits);+ for(ScoreDoc scoreDoc : hits2.scoreDocs) {+ System.out.println(" " + scoreDoc.doc);+ }+ }++ assertTrue(hits2.totalHits <= hits1.totalHits);+ assertEquals(hits2.scoreDocs.length, hits1.scoreDocs.length);+ for(int i=0;i<hits2.scoreDocs.length;i++) {+ ScoreDoc hit1 = hits1.scoreDocs[i];+ ScoreDoc hit2 = hits2.scoreDocs[i];+ assertEquals(r1.document(hit1.doc).get("id"), r2.document(hit2.doc).get("id"));+ assertEquals(((FieldDoc) hit1).fields, ((FieldDoc) hit2).fields);+ }+ }++ IOUtils.close(r1, r2, w1, w2, dir1, dir2);+ }}diff --git a/lucene/misc/src/test/org/apache/lucene/search/TestEarlyTerminatingSortingCollector.java b/lucene/core/src/test/org/apache/lucene/search/TestEarlyTerminatingSortingCollector.javasimilarity index 100%rename from lucene/misc/src/test/org/apache/lucene/search/TestEarlyTerminatingSortingCollector.javarename to lucene/core/src/test/org/apache/lucene/search/TestEarlyTerminatingSortingCollector.java