Exclude degenerate edge case (maxConn=1) from TestKnnGraph.testSearch

This commit is contained in:
Michael Sokolov 2021-01-28 10:16:05 -05:00
parent a176308aa6
commit d84925a201
1 changed files with 14 additions and 15 deletions

View File

@ -59,7 +59,7 @@ public class TestKnnGraph extends LuceneTestCase {
randSeed = random().nextLong();
if (random().nextBoolean()) {
maxConn = HnswGraphBuilder.DEFAULT_MAX_CONN;
HnswGraphBuilder.DEFAULT_MAX_CONN = random().nextInt(256) + 1;
HnswGraphBuilder.DEFAULT_MAX_CONN = random().nextInt(256) + 2;
}
int strategy = random().nextInt(SearchStrategy.values().length - 1) + 1;
searchStrategy = SearchStrategy.values()[strategy];
@ -215,7 +215,7 @@ public class TestKnnGraph extends LuceneTestCase {
searchStrategy = SearchStrategy.EUCLIDEAN_HNSW;
try (Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig())) {
// Add a document for every cartesian point in an NxN square so we can
// Add a document for every cartesian point in an NxN square so we can
// easily know which are the nearest neighbors to every point. Insert by iterating
// using a prime number that is not a divisor of N*N so that we will hit each point once,
// and chosen so that points will be inserted in a deterministic
@ -225,7 +225,8 @@ public class TestKnnGraph extends LuceneTestCase {
int index = 0;
for (int i = 0; i < values.length; i++) {
// System.out.printf("%d: (%d, %d)\n", i, index % n, index / n);
values[i] = new float[] {index % n, index / n};
int x = index % n, y = index / n;
values[i] = new float[] {x, y};
index = (index + stepSize) % (n * n);
add(iw, i, values[i]);
if (i == 13) {
@ -249,10 +250,10 @@ public class TestKnnGraph extends LuceneTestCase {
// 9 24 14 4 19
// 12 2 17 7 22
// For this small graph the "search" is exhaustive, so this mostly tests the APIs, the
// orientation of the
// various priority queues, the scoring function, but not so much the approximate KNN search
// algo
/* For this small graph the "search" is exhaustive, so this mostly tests the APIs, the
* orientation of the various priority queues, the scoring function, but not so much the
* approximate KNN search algorithm
*/
assertGraphSearch(new int[] {0, 15, 3, 18, 5}, new float[] {0f, 0.1f}, dr);
// Tiebreaking by docid must be done after VectorValues.search.
// assertGraphSearch(new int[]{11, 1, 8, 14, 21}, new float[]{2, 2}, dr);
@ -266,8 +267,7 @@ public class TestKnnGraph extends LuceneTestCase {
TopDocs results = doKnnSearch(reader, vector, 5);
for (ScoreDoc doc : results.scoreDocs) {
// map docId to insertion id
int id = Integer.parseInt(reader.document(doc.doc).get("id"));
doc.doc = id;
doc.doc = Integer.parseInt(reader.document(doc.doc).get("id"));
}
assertResults(expected, results);
}
@ -313,7 +313,7 @@ public class TestKnnGraph extends LuceneTestCase {
continue;
}
KnnGraphValues graphValues = vectorReader.getGraphValues(KNN_GRAPH_FIELD);
assertTrue((vectorValues == null) == (graphValues == null));
assertEquals((vectorValues == null), (graphValues == null));
if (vectorValues == null) {
continue;
}
@ -388,11 +388,10 @@ public class TestKnnGraph extends LuceneTestCase {
}
private void assertMaxConn(int[][] graph, int maxConn) {
for (int i = 0; i < graph.length; i++) {
if (graph[i] != null) {
assert (graph[i].length <= maxConn);
for (int j = 0; j < graph[i].length; j++) {
int k = graph[i][j];
for (int[] ints : graph) {
if (ints != null) {
assert (ints.length <= maxConn);
for (int k : ints) {
assertNotNull(graph[k]);
}
}