Apache Lucene open-source search software
Handle inconsistent schema on flush with index sorts (#15125)
Nhat Nguyen63ac5aca53f1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 4a9e41c0c09..aec32100577 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -98,6 +98,8 @@ Bug Fixes* GITHUB#15213: Don't normalize centroids with zero samples, which caused divide-by-zero. (Mike Sokolov)+* GITHUB#15125: Handle inconsistent schema on flush with index sorts (Nhat Nguyen)+Changes in Runtime Behavior---------------------* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.javaindex 95513e24814..29e7003012e 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java@@ -153,7 +153,7 @@ final class IndexingChain implements Accountable {@Overridepublic NumericDocValues getNumericDocValues(String field) {PerField pf = getPerField(field);- if (pf == null) {+ if (pf == null || pf.fieldInfo == null) {return null;}if (pf.fieldInfo.getDocValuesType() == DocValuesType.NUMERIC) {@@ -165,7 +165,7 @@ final class IndexingChain implements Accountable {@Overridepublic BinaryDocValues getBinaryDocValues(String field) {PerField pf = getPerField(field);- if (pf == null) {+ if (pf == null || pf.fieldInfo == null) {return null;}if (pf.fieldInfo.getDocValuesType() == DocValuesType.BINARY) {@@ -177,7 +177,7 @@ final class IndexingChain implements Accountable {@Overridepublic SortedDocValues getSortedDocValues(String field) throws IOException {PerField pf = getPerField(field);- if (pf == null) {+ if (pf == null || pf.fieldInfo == null) {return null;}if (pf.fieldInfo.getDocValuesType() == DocValuesType.SORTED) {@@ -189,7 +189,7 @@ final class IndexingChain implements Accountable {@Overridepublic SortedNumericDocValues getSortedNumericDocValues(String field) throws IOException {PerField pf = getPerField(field);- if (pf == null) {+ if (pf == null || pf.fieldInfo == null) {return null;}if (pf.fieldInfo.getDocValuesType() == DocValuesType.SORTED_NUMERIC) {@@ -201,7 +201,7 @@ final class IndexingChain implements Accountable {@Overridepublic SortedSetDocValues getSortedSetDocValues(String field) throws IOException {PerField pf = getPerField(field);- if (pf == null) {+ if (pf == null || pf.fieldInfo == null) {return null;}if (pf.fieldInfo.getDocValuesType() == DocValuesType.SORTED_SET) {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.javaindex 48c469f1a6f..7041f1332d1 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java@@ -54,6 +54,8 @@ import org.apache.lucene.document.Field.Store;import org.apache.lucene.document.FieldType;import org.apache.lucene.document.FloatDocValuesField;import org.apache.lucene.document.IntPoint;+import org.apache.lucene.document.KeywordField;+import org.apache.lucene.document.LongField;import org.apache.lucene.document.NumericDocValuesField;import org.apache.lucene.document.SortedDocValuesField;import org.apache.lucene.document.SortedNumericDocValuesField;@@ -3447,4 +3449,65 @@ public class TestIndexSorting extends LuceneTestCase {}}}++ public void testFlushAfterInconsistentSchema() throws IOException {+ IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));+ if (random().nextBoolean()) {+ Sort indexSort = new Sort(new SortField("host.name", SortField.Type.STRING));+ iwc.setIndexSort(indexSort);+ }+ try (Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, iwc)) {+ Consumer<Document> addExtraFields =+ doc -> {+ int extraFields = random().nextInt(100);+ for (int i = 0; i < extraFields; i++) {+ if (random().nextBoolean()) {+ doc.add(new NumericDocValuesField("extra-dv-" + i, i));+ }+ if (random().nextBoolean()) {+ doc.add(new LongField("extra-point-" + i, i, Store.NO));+ }+ if (random().nextBoolean()) {+ doc.add(+ new KeywordField(+ "extra-posting-" + i, new BytesRef(Integer.toString(i)), Store.NO));+ }+ }+ };+ // first segment+ {+ Document doc = new Document();+ doc.add(new SortedDocValuesField("host.name", newBytesRef("h2")));+ doc.add(new LongField("@timestamp", 1, Store.NO));+ addExtraFields.accept(doc);+ w.addDocument(doc);+ w.commit();+ }+ // second segment+ {+ var doc = new Document();+ doc.add(new NumericDocValuesField("@timestamp", 2));+ addExtraFields.accept(doc);+ doc.add(new SortedDocValuesField("host.name", newBytesRef("h1")));+ expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));+ w.commit();+ }+ try (DirectoryReader reader = DirectoryReader.open(w)) {+ assertEquals(1, reader.numDocs());+ }+ // third segment+ {+ var doc = new Document();+ addExtraFields.accept(doc);+ doc.add(new LongField("@timestamp", 3, Store.NO));+ doc.add(new SortedDocValuesField("host.name", newBytesRef("h1")));+ w.addDocument(doc);+ w.commit();+ }+ try (DirectoryReader reader = DirectoryReader.open(w)) {+ assertEquals(2, reader.numDocs());+ }+ }+ }}