LUCENE-6766: fix parallel reader's detection of conflicting index sort

This commit is contained in:
Mike McCandless 2016-05-10 05:31:02 -04:00
parent 1e82c13184
commit 8361de87be
3 changed files with 63 additions and 5 deletions

View File

@ -210,7 +210,7 @@ public final class DocValues {
(expected.length == 1
? "(expected=" + expected[0]
: "(expected one of " + Arrays.toString(expected)) + "). " +
" Re-index with correct docvalues type.");
"Re-index with correct docvalues type.");
}
}

View File

@ -107,10 +107,11 @@ public class ParallelLeafReader extends LeafReader {
// build FieldInfos and fieldToReader map:
for (final LeafReader reader : this.parallelReaders) {
Sort leafIndexSort = reader.getIndexSort();
if (indexSort == null) {
indexSort = reader.getIndexSort();
} else if (indexSort.equals(reader.getIndexSort()) == false) {
throw new IllegalArgumentException("cannot combine LeafReaders that have different index sorts: saw both sort=" + indexSort + " and " + reader.getIndexSort());
indexSort = leafIndexSort;
} else if (leafIndexSort != null && indexSort.equals(leafIndexSort) == false) {
throw new IllegalArgumentException("cannot combine LeafReaders that have different index sorts: saw both sort=" + indexSort + " and " + leafIndexSort);
}
final FieldInfos readerFieldInfos = reader.getFieldInfos();

View File

@ -23,10 +23,11 @@ import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil;
@ -314,4 +315,60 @@ public class TestParallelLeafReader extends LuceneTestCase {
return dir2;
}
// not ok to have one leaf w/ index sort and another with a different index sort
public void testWithIndexSort1() throws Exception {
Directory dir1 = newDirectory();
IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random()));
iwc1.setIndexSort(new Sort(new SortField("foo", SortField.Type.INT)));
IndexWriter w1 = new IndexWriter(dir1, iwc1);
w1.addDocument(new Document());
w1.commit();
w1.addDocument(new Document());
w1.forceMerge(1);
w1.close();
IndexReader r1 = DirectoryReader.open(dir1);
Directory dir2 = newDirectory();
IndexWriterConfig iwc2 = newIndexWriterConfig(new MockAnalyzer(random()));
iwc2.setIndexSort(new Sort(new SortField("bar", SortField.Type.INT)));
IndexWriter w2 = new IndexWriter(dir2, iwc2);
w2.addDocument(new Document());
w2.commit();
w2.addDocument(new Document());
w2.forceMerge(1);
w2.close();
IndexReader r2 = DirectoryReader.open(dir2);
String message = expectThrows(IllegalArgumentException.class, () -> {
new ParallelLeafReader(getOnlyLeafReader(r1), getOnlyLeafReader(r2));
}).getMessage();
assertEquals("cannot combine LeafReaders that have different index sorts: saw both sort=<int: \"foo\"> and <int: \"bar\">", message);
IOUtils.close(r1, dir1, r2, dir2);
}
// ok to have one leaf w/ index sort and the other with no sort
public void testWithIndexSort2() throws Exception {
Directory dir1 = newDirectory();
IndexWriterConfig iwc1 = newIndexWriterConfig(new MockAnalyzer(random()));
iwc1.setIndexSort(new Sort(new SortField("foo", SortField.Type.INT)));
IndexWriter w1 = new IndexWriter(dir1, iwc1);
w1.addDocument(new Document());
w1.commit();
w1.addDocument(new Document());
w1.forceMerge(1);
w1.close();
IndexReader r1 = DirectoryReader.open(dir1);
Directory dir2 = newDirectory();
IndexWriterConfig iwc2 = newIndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w2 = new IndexWriter(dir2, iwc2);
w2.addDocument(new Document());
w2.addDocument(new Document());
w2.close();
IndexReader r2 = DirectoryReader.open(dir2);
new ParallelLeafReader(false, getOnlyLeafReader(r1), getOnlyLeafReader(r2)).close();
new ParallelLeafReader(false, getOnlyLeafReader(r2), getOnlyLeafReader(r1)).close();
IOUtils.close(r1, dir1, r2, dir2);
}
}