Apache Lucene open-source search software
LUCENE-10082: add detail to schema inconsistency error messages
Michael Sokolovee7a719dd8a3
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.javaindex 6db54f3a2de..a4d7fab4fdf 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexingChain.java@@ -1336,12 +1336,40 @@ final class IndexingChain implements Accountable {this.name = name;}- private void assertSame(boolean same) {- if (same == false) {- throw new IllegalArgumentException(errMsg + "[" + name + "] of doc [" + docID + "].");+ private void assertSame(String label, boolean expected, boolean given) {+ if (expected != given) {+ raiseNotSame(label, expected, given);}}+ private void assertSame(String label, int expected, int given) {+ if (expected != given) {+ raiseNotSame(label, expected, given);+ }+ }++ private void assertSame(String label, Object expected, Object given) {+ if (expected != given) {+ raiseNotSame(label, expected, given);+ }+ }++ private void raiseNotSame(String label, Object expected, Object given) {+ throw new IllegalArgumentException(+ errMsg+ + "["+ + name+ + "] of doc ["+ + docID+ + "]. "+ + label+ + ": expected '"+ + expected+ + "', but it has '"+ + given+ + "'.");+ }+void updateAttributes(Map<String, String> attrs) {attrs.forEach((k, v) -> this.attributes.put(k, v));}@@ -1353,10 +1381,9 @@ final class IndexingChain implements Accountable {omitNorms = newOmitNorms;storeTermVector = newStoreTermVector;} else {- assertSame(- indexOptions == newIndexOptions- && omitNorms == newOmitNorms- && storeTermVector == newStoreTermVector);+ assertSame("index options", indexOptions, newIndexOptions);+ assertSame("omit norms", omitNorms, newOmitNorms);+ assertSame("store term vector", storeTermVector, newStoreTermVector);}}@@ -1365,7 +1392,8 @@ final class IndexingChain implements Accountable {this.docValuesType = newDocValuesType;this.dvGen = newDvGen;} else {- assertSame(docValuesType == newDocValuesType && dvGen == newDvGen);+ assertSame("doc values type", docValuesType, newDocValuesType);+ assertSame("doc values generation", dvGen, newDvGen);}}@@ -1375,10 +1403,9 @@ final class IndexingChain implements Accountable {pointIndexDimensionCount = indexDimensionCount;pointNumBytes = numBytes;} else {- assertSame(- pointDimensionCount == dimensionCount- && pointIndexDimensionCount == indexDimensionCount- && pointNumBytes == numBytes);+ assertSame("point dimension", pointDimensionCount, dimensionCount);+ assertSame("point index dimension", pointIndexDimensionCount, indexDimensionCount);+ assertSame("point num bytes", pointNumBytes, numBytes);}}@@ -1387,7 +1414,8 @@ final class IndexingChain implements Accountable {this.vectorDimension = dimension;this.vectorSimilarityFunction = similarityFunction;} else {- assertSame(vectorSimilarityFunction == similarityFunction && vectorDimension == dimension);+ assertSame("vector similarity function", vectorSimilarityFunction, similarityFunction);+ assertSame("vector dimension", vectorDimension, dimension);}}@@ -1406,17 +1434,18 @@ final class IndexingChain implements Accountable {}void assertSameSchema(FieldInfo fi) {+ assertSame("index options", fi.getIndexOptions(), indexOptions);+ assertSame("omit norms", fi.omitsNorms(), omitNorms);+ assertSame("store term vector", fi.hasVectors(), storeTermVector);+ assertSame("doc values type", fi.getDocValuesType(), docValuesType);+ assertSame("doc values generation", fi.getDocValuesGen(), dvGen);+ assertSame(+ "vector similarity function", fi.getVectorSimilarityFunction(), vectorSimilarityFunction);+ assertSame("vector dimension", fi.getVectorDimension(), vectorDimension);+ assertSame("point dimension", fi.getPointDimensionCount(), pointDimensionCount);assertSame(- indexOptions == fi.getIndexOptions()- && omitNorms == fi.omitsNorms()- && storeTermVector == fi.hasVectors()- && docValuesType == fi.getDocValuesType()- && dvGen == fi.getDocValuesGen()- && pointDimensionCount == fi.getPointDimensionCount()- && pointIndexDimensionCount == fi.getPointIndexDimensionCount()- && pointNumBytes == fi.getPointNumBytes()- && vectorDimension == fi.getVectorDimension()- && vectorSimilarityFunction == fi.getVectorSimilarityFunction());+ "point index dimension", fi.getPointIndexDimensionCount(), pointIndexDimensionCount);+ assertSame("point num bytes", fi.getPointNumBytes(), pointNumBytes);}}}diff --git a/lucene/core/src/test/org/apache/lucene/document/TestPerFieldConsistency.java b/lucene/core/src/test/org/apache/lucene/document/TestPerFieldConsistency.javaindex 6826c545984..ae124500337 100644--- a/lucene/core/src/test/org/apache/lucene/document/TestPerFieldConsistency.java+++ b/lucene/core/src/test/org/apache/lucene/document/TestPerFieldConsistency.java@@ -115,7 +115,9 @@ public class TestPerFieldConsistency extends LuceneTestCase {}IllegalArgumentException exception =expectThrows(IllegalArgumentException.class, () -> writer.addDocument(doc));- assertTrue(exception.getMessage().contains(errorMsg));+ assertTrue(+ "'" + errorMsg + "' not found in '" + exception.getMessage() + "'",+ exception.getMessage().contains(errorMsg));}private static void doTestDocWithExtraSchemaOptionsThrowsError(@@ -125,7 +127,9 @@ public class TestPerFieldConsistency extends LuceneTestCase {doc.add(extra);IllegalArgumentException exception =expectThrows(IllegalArgumentException.class, () -> writer.addDocument(doc));- assertTrue(exception.getMessage().contains(errorMsg));+ assertTrue(+ "'" + errorMsg + "' not found in '" + exception.getMessage() + "'",+ exception.getMessage().contains(errorMsg));}public void testDocWithMissingSchemaOptionsThrowsError() throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexOptions.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexOptions.javaindex 36c5f4b2c7d..8342089d10f 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexOptions.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexOptions.java@@ -52,7 +52,12 @@ public class TestIndexOptions extends LuceneTestCase {IllegalArgumentException.class,() -> w.addDocument(Collections.singleton(new Field("foo", "bar", ft2))));assertEquals(- "Inconsistency of field data structures across documents for field [foo] of doc [1].",+ "Inconsistency of field data structures across documents for field [foo] of doc [1]."+ + " index options: expected '"+ + from+ + "', but it has '"+ + to+ + "'.",e.getMessage());}w.close();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.javaindex de1e900a3ef..52c4db3a576 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexSorting.java@@ -2824,7 +2824,11 @@ public class TestIndexSorting extends LuceneTestCase {doc.add(dvs.get(j));exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));assertEquals(- "Inconsistency of field data structures across documents for field [field] of doc [2].",+ "Inconsistency of field data structures across documents for field [field] of doc [2]. doc values type: expected '"+ + dvs.get(i).fieldType().docValuesType()+ + "', but it has '"+ + dvs.get(j).fieldType().docValuesType()+ + "'.",exc.getMessage());w.rollback();IOUtils.close(w);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java b/lucene/core/src/test/org/apache/lucene/index/TestPointValues.javaindex 8ca1e1a917d..3af9215be6b 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java@@ -74,7 +74,8 @@ public class TestPointValues extends LuceneTestCase {w.addDocument(doc);});assertEquals(- "Inconsistency of field data structures across documents for field [dim] of doc [0].",+ "Inconsistency of field data structures across documents for field [dim] of doc [0]."+ + " point dimension: expected '1', but it has '2'.",expected.getMessage());w.close();dir.close();@@ -97,7 +98,8 @@ public class TestPointValues extends LuceneTestCase {w.addDocument(doc2);});assertEquals(- "Inconsistency of field data structures across documents for field [dim] of doc [1].",+ "Inconsistency of field data structures across documents for field [dim] of doc [1]."+ + " point dimension: expected '1', but it has '2'.",expected.getMessage());w.close();dir.close();@@ -254,7 +256,8 @@ public class TestPointValues extends LuceneTestCase {w.addDocument(doc);});assertEquals(- "Inconsistency of field data structures across documents for field [dim] of doc [0].",+ "Inconsistency of field data structures across documents for field [dim] of doc [0]."+ + " point num bytes: expected '4', but it has '6'.",expected.getMessage());w.close();dir.close();@@ -277,7 +280,8 @@ public class TestPointValues extends LuceneTestCase {w.addDocument(doc2);});assertEquals(- "Inconsistency of field data structures across documents for field [dim] of doc [1].",+ "Inconsistency of field data structures across documents for field [dim] of doc [1]."+ + " point num bytes: expected '4', but it has '6'.",expected.getMessage());w.close();dir.close();diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseKnnVectorsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseKnnVectorsFormatTestCase.javaindex 22fc2e1ba3e..126d7973aec 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseKnnVectorsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseKnnVectorsFormatTestCase.java@@ -100,7 +100,8 @@ public abstract class BaseKnnVectorsFormatTestCase extends BaseIndexFileFormatTeIllegalArgumentException expected =expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc2));String errMsg =- "Inconsistency of field data structures across documents for field [f] of doc [1].";+ "Inconsistency of field data structures across documents for field [f] of doc [1]."+ + " vector dimension: expected '4', but it has '3'.";assertEquals(errMsg, expected.getMessage());}@@ -136,7 +137,8 @@ public abstract class BaseKnnVectorsFormatTestCase extends BaseIndexFileFormatTeIllegalArgumentException expected =expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc2));String errMsg =- "Inconsistency of field data structures across documents for field [f] of doc [1].";+ "Inconsistency of field data structures across documents for field [f] of doc [1]."+ + " vector similarity function: expected 'DOT_PRODUCT', but it has 'EUCLIDEAN'.";assertEquals(errMsg, expected.getMessage());}