From c8e1c08cc8bf1d33d9796f9c114cb9852bc37195 Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Tue, 1 Feb 2022 10:21:48 -0500 Subject: [PATCH] Small fix for assertConsistentGraph (#631) TestKnnGraph.testMultipleVectorFields sometimes breaks with the following message: java.lang.NullPointerException: Cannot invoke "org.apache.lucene.codecs.lucene91.Lucene91HnswVectorsReader.getGraphValues(String)" because "vectorReader" is null This happens in assertConsistentGraph. This patch ensures that for a segment and a field where there is no vectors indexed, we don't run a check on consistent graph. --- .../src/test/org/apache/lucene/index/TestKnnGraph.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java b/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java index e2366aab1fb..05900243cc3 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestKnnGraph.java @@ -172,9 +172,11 @@ public class TestKnnGraph extends LuceneTestCase { int numDoc = atLeast(100); int[] dims = new int[numVectorFields]; float[][][] values = new float[numVectorFields][][]; + FieldType[] fieldTypes = new FieldType[numVectorFields]; for (int field = 0; field < numVectorFields; field++) { dims[field] = atLeast(3); values[field] = randomVectors(numDoc, dims[field]); + fieldTypes[field] = KnnVectorField.createFieldType(dims[field], similarityFunction); } try (Directory dir = newDirectory(); @@ -184,8 +186,7 @@ public class TestKnnGraph extends LuceneTestCase { for (int field = 0; field < numVectorFields; field++) { float[] vector = values[field][docID]; if (vector != null) { - FieldType fieldType = KnnVectorField.createFieldType(vector.length, similarityFunction); - doc.add(new KnnVectorField(KNN_GRAPH_FIELD + field, vector, fieldType)); + doc.add(new KnnVectorField(KNN_GRAPH_FIELD + field, vector, fieldTypes[field])); } } String idString = Integer.toString(docID); @@ -428,7 +429,6 @@ public class TestKnnGraph extends LuceneTestCase { try (DirectoryReader dr = DirectoryReader.open(iw)) { for (LeafReaderContext ctx : dr.leaves()) { LeafReader reader = ctx.reader(); - VectorValues vectorValues = reader.getVectorValues(vectorField); PerFieldKnnVectorsFormat.FieldsReader perFieldReader = (PerFieldKnnVectorsFormat.FieldsReader) ((CodecReader) reader).getVectorReader(); if (perFieldReader == null) { @@ -436,7 +436,11 @@ public class TestKnnGraph extends LuceneTestCase { } Lucene91HnswVectorsReader vectorReader = (Lucene91HnswVectorsReader) perFieldReader.getFieldReader(vectorField); + if (vectorReader == null) { + continue; + } KnnGraphValues graphValues = vectorReader.getGraphValues(vectorField); + VectorValues vectorValues = reader.getVectorValues(vectorField); if (vectorValues == null) { assert graphValues == null; continue;