Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MAX_DIMENSIONS configurable via a system property. #12306

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lucene/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ dependencies {
moduleTestImplementation project(':lucene:codecs')
moduleTestImplementation project(':lucene:test-framework')
}

test {
// test higher dimensions than the default limit
systemProperty 'org.apache.lucene.hnsw.maxDimensions', '2048'
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package org.apache.lucene.index;

import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Logger;
import org.apache.lucene.document.KnnFloatVectorField;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.util.SuppressForbidden;

/**
* This class provides access to per-document floating point vector values indexed as {@link
Expand All @@ -27,9 +31,40 @@
* @lucene.experimental
*/
public abstract class FloatVectorValues extends DocIdSetIterator {
private static final Logger LOG = Logger.getLogger(FloatVectorValues.class.getName());

/** The maximum length of a vector */
public static final int MAX_DIMENSIONS = 1024;
/**
* The maximum length of a vector. Can be overridden via a system property
* "org.apache.lucene.hnsw.maxDimensions".
*
* @deprecated Expected to move to a codec specific limit.
*/
@Deprecated public static final int MAX_DIMENSIONS = doPrivileged(FloatVectorValues::initMaxDim);

// Extracted to a method to be able to apply the SuppressForbidden annotation
@SuppressWarnings("removal")
@SuppressForbidden(reason = "security manager")
private static <T> T doPrivileged(PrivilegedAction<T> action) {
return AccessController.doPrivileged(action);
}

private static int initMaxDim() {
final var PROP_NAME = "org.apache.lucene.hnsw.maxDimensions";
final int DEFAULT_MAX = 1024;
try {
int d = Integer.getInteger(PROP_NAME, DEFAULT_MAX);
if (d > 2048) {
LOG.warning("Lucene HNSW only supports up to 2048 dimensions.");
}
return d;
} catch (
@SuppressWarnings("unused")
Exception ignored) { // e.g. SecurityException
LOG.warning(
"Cannot read sysprop " + PROP_NAME + ", so the dimension limit will be unchanged.");
return DEFAULT_MAX;
}
}

/** Sole constructor */
protected FloatVectorValues() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.KnnVectorsFormat;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase;
import org.apache.lucene.tests.util.TestUtil;

Expand Down Expand Up @@ -48,4 +49,8 @@ public void testLimits() {
expectThrows(IllegalArgumentException.class, () -> new Lucene95HnswVectorsFormat(512 + 1, 20));
expectThrows(IllegalArgumentException.class, () -> new Lucene95HnswVectorsFormat(20, 3201));
}

public void testTestedWithHighDimensionLimit() {
assertEquals(2048, FloatVectorValues.MAX_DIMENSIONS);
}
}