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