Skip to content

Apache Lucene open-source search software

Forward port "Fix 9.12.0 backcompat break" to main (#13912)

Luca Cavannab0170148a135
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/BackwardsCompatibilityTestBase.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/BackwardsCompatibilityTestBase.java
index 758c7cef2e9..2a4bf7916e7 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/BackwardsCompatibilityTestBase.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/BackwardsCompatibilityTestBase.java
@@ -106,8 +106,8 @@ public abstract class BackwardsCompatibilityTestBase extends LuceneTestCase {
* This is a base constructor for parameterized BWC tests. The constructor arguments are provided
* by {@link com.carrotsearch.randomizedtesting.RandomizedRunner} during test execution. A {@link
* com.carrotsearch.randomizedtesting.annotations.ParametersFactory} specified in a subclass
- * provides a list lists of arguments for the tests and RandomizedRunner will execute the test for
- * each of the argument list.
+ * provides a list of arguments for the tests and RandomizedRunner will execute the test for each
+ * of the argument list.
*
* @param version the version this test should run for
* @param indexPattern an index pattern in order to open an index of see {@link
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestGenerateBwcIndices.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestGenerateBwcIndices.java
index 936a4c28cf2..6989731ae14 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestGenerateBwcIndices.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestGenerateBwcIndices.java
@@ -39,7 +39,7 @@ public class TestGenerateBwcIndices extends LuceneTestCase {
// To generate backcompat indexes with the current default codec, run the following gradle
// command:
// gradlew test -Ptests.bwcdir=/path/to/store/indexes -Ptests.codec=default
- // -Ptests.useSecurityManager=false --tests TestGenerateBwcIndices
+ // -Ptests.useSecurityManager=false --tests TestGenerateBwcIndices --max-workers=1
//
// Also add testmethod with one of the index creation methods below, for example:
// -Ptestmethod=testCreateCFS
@@ -82,14 +82,14 @@ public class TestGenerateBwcIndices extends LuceneTestCase {
sortedTest.createBWCIndex();
}
- public void testCreateInt8HNSWIndices() throws IOException {
- TestInt8HnswBackwardsCompatibility int8HnswBackwardsCompatibility =
- new TestInt8HnswBackwardsCompatibility(
+ public void testCreateInt7HNSWIndices() throws IOException {
+ TestInt7HnswBackwardsCompatibility int7HnswBackwardsCompatibility =
+ new TestInt7HnswBackwardsCompatibility(
Version.LATEST,
createPattern(
- TestInt8HnswBackwardsCompatibility.INDEX_NAME,
- TestInt8HnswBackwardsCompatibility.SUFFIX));
- int8HnswBackwardsCompatibility.createBWCIndex();
+ TestInt7HnswBackwardsCompatibility.INDEX_NAME,
+ TestInt7HnswBackwardsCompatibility.SUFFIX));
+ int7HnswBackwardsCompatibility.createBWCIndex();
}
private boolean isInitialMajorVersionRelease() {
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt8HnswBackwardsCompatibility.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt7HnswBackwardsCompatibility.java
similarity index 75%
rename from lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt8HnswBackwardsCompatibility.java
rename to lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt7HnswBackwardsCompatibility.java
index 7ab23ce162d..7e4b59542fa 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt8HnswBackwardsCompatibility.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/TestInt7HnswBackwardsCompatibility.java
@@ -23,17 +23,22 @@ import java.io.IOException;
import org.apache.lucene.backward_codecs.lucene99.Lucene99Codec;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.KnnVectorsFormat;
+import org.apache.lucene.codecs.KnnVectorsReader;
import org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat;
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
+import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsReader;
+import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.KnnFloatVectorField;
import org.apache.lucene.document.StringField;
+import org.apache.lucene.index.CodecReader;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.IndexSearcher;
@@ -41,23 +46,23 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.Version;
+import org.apache.lucene.util.quantization.QuantizedByteVectorValues;
-public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTestBase {
+public class TestInt7HnswBackwardsCompatibility extends BackwardsCompatibilityTestBase {
- static final String INDEX_NAME = "int8_hnsw";
+ static final String INDEX_NAME = "int7_hnsw";
static final String SUFFIX = "";
- private static final Version FIRST_INT8_HNSW_VERSION = Version.fromBits(9, 10, 0);
+ private static final Version FIRST_INT7_HNSW_VERSION = Version.fromBits(9, 10, 0);
private static final String KNN_VECTOR_FIELD = "knn_field";
private static final int DOC_COUNT = 30;
private static final FieldType KNN_VECTOR_FIELD_TYPE =
KnnFloatVectorField.createFieldType(3, VectorSimilarityFunction.COSINE);
private static final float[] KNN_VECTOR = {0.2f, -0.1f, 0.1f};
- public TestInt8HnswBackwardsCompatibility(Version version, String pattern) {
+ public TestInt7HnswBackwardsCompatibility(Version version, String pattern) {
super(version, pattern);
}
- /** Provides all sorted versions to the test-framework */
@ParametersFactory(argumentFormatting = "Lucene-Version:%1$s; Pattern: %2$s")
public static Iterable<Object[]> testVersionsFactory() throws IllegalAccessException {
return allVersion(INDEX_NAME, SUFFIX);
@@ -76,7 +81,7 @@ public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTe
@Override
protected boolean supportsVersion(Version version) {
- return version.onOrAfter(FIRST_INT8_HNSW_VERSION);
+ return version.onOrAfter(FIRST_INT7_HNSW_VERSION);
}
@Override
@@ -84,7 +89,7 @@ public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTe
// We don't use the default codec
}
- public void testInt8HnswIndexAndSearch() throws Exception {
+ public void testInt7HnswIndexAndSearch() throws Exception {
IndexWriterConfig indexWriterConfig =
newIndexWriterConfig(new MockAnalyzer(random()))
.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
@@ -108,7 +113,6 @@ public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTe
assertKNNSearch(searcher, KNN_VECTOR, 10, 10, "0");
}
}
- // This will confirm the docs are really sorted
TestUtil.checkIndex(directory);
}
@@ -117,7 +121,7 @@ public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTe
IndexWriterConfig conf =
new IndexWriterConfig(new MockAnalyzer(random()))
.setMaxBufferedDocs(10)
- .setCodec(TestUtil.getDefaultCodec())
+ .setCodec(getCodec())
.setMergePolicy(NoMergePolicy.INSTANCE);
try (IndexWriter writer = new IndexWriter(dir, conf)) {
for (int i = 0; i < DOC_COUNT; i++) {
@@ -147,4 +151,29 @@ public class TestInt8HnswBackwardsCompatibility extends BackwardsCompatibilityTe
assertKNNSearch(searcher, KNN_VECTOR, 10, 10, "0");
}
}
+
+ // #13880: make sure the BWC index really contains quantized HNSW not float32
+ public void testIndexIsReallyQuantized() throws Exception {
+ try (DirectoryReader reader = DirectoryReader.open(directory)) {
+ for (LeafReaderContext leafContext : reader.leaves()) {
+ KnnVectorsReader knnVectorsReader = ((CodecReader) leafContext.reader()).getVectorReader();
+ assertTrue(
+ "expected PerFieldKnnVectorsFormat.FieldsReader but got: " + knnVectorsReader,
+ knnVectorsReader instanceof PerFieldKnnVectorsFormat.FieldsReader);
+
+ KnnVectorsReader forField =
+ ((PerFieldKnnVectorsFormat.FieldsReader) knnVectorsReader)
+ .getFieldReader(KNN_VECTOR_FIELD);
+
+ assertTrue(forField instanceof Lucene99HnswVectorsReader);
+
+ QuantizedByteVectorValues quantized =
+ ((Lucene99HnswVectorsReader) forField).getQuantizedVectorValues(KNN_VECTOR_FIELD);
+
+ assertNotNull(
+ "KnnVectorsReader should have quantized interface for field " + KNN_VECTOR_FIELD,
+ quantized);
+ }
+ }
+ }
}
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.10.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.10.0.zip
new file mode 100644
index 00000000000..0425b451fa0
Binary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.10.0.zip differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.0.zip
new file mode 100644
index 00000000000..9dd53d92a99
Binary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.0.zip differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.1.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.1.zip
new file mode 100644
index 00000000000..29aef1b909f
Binary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.11.1.zip differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.12.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.12.0.zip
new file mode 100644
index 00000000000..bfe07de8143
Binary files /dev/null and b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int7_hnsw.9.12.0.zip differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.10.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.10.0.zip
deleted file mode 100644
index 2799f04b65a..00000000000
Binary files a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.10.0.zip and /dev/null differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.0.zip
deleted file mode 100644
index 5fd94783427..00000000000
Binary files a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.0.zip and /dev/null differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.1.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.1.zip
deleted file mode 100644
index c4bb86b5f1b..00000000000
Binary files a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.11.1.zip and /dev/null differ
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.12.0.zip b/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.12.0.zip
deleted file mode 100644
index 82228b37e02..00000000000
Binary files a/lucene/backward-codecs/src/test/org/apache/lucene/backward_index/unsupported.int8_hnsw.9.12.0.zip and /dev/null differ
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
index 24123a4f21e..32eea942e2a 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99ScalarQuantizedVectorsReader.java
@@ -135,7 +135,7 @@ public final class Lucene99ScalarQuantizedVectorsReader extends FlatVectorsReade
}
final long quantizedVectorBytes;
- if (fieldEntry.compress) {
+ if (fieldEntry.bits <= 4 && fieldEntry.compress) {
// two dimensions -> one byte
quantizedVectorBytes = ((dimension + 1) >> 1) + Float.BYTES;
} else {