Skip to content

Commit 140fe02

Browse files
committed
fix:Add check for empty search results to prevent array out of bounds
1 parent f9b9852 commit 140fe02

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

geaflow/geaflow-plugins/geaflow-store/geaflow-store-vector/src/main/java/org/apache/geaflow/store/lucene/GraphVectorIndex.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public K searchVectorIndex(boolean isVertex, String fieldName, float[] vector, i
145145
// Execute search
146146
TopDocs topDocs = searcher.search(knnQuery, topK);
147147

148+
if (topDocs.scoreDocs.length == 0) {
149+
return null;
150+
}
151+
148152
Document firstDoc = searcher.doc(topDocs.scoreDocs[0].doc);
149153

150154
K result;

geaflow/geaflow-plugins/geaflow-store/geaflow-store-vector/src/test/java/org/apache/geaflow/store/lucene/GraphVectorIndexTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,11 @@ public void testMultipleVectorsSameKeyDifferentFields() {
155155
assertEquals(result1, key);
156156
assertEquals(result2, key);
157157
}
158+
@Test
159+
public void testSearchNonExistentFieldReturnsNull() {
160+
stringIndex.addVectorIndex(true, "vertex_1", "embedding", new float[]{0.1f, 0.2f, 0.3f, 0.4f});
161+
// searching for a non-existent field should return null
162+
String result = stringIndex.searchVectorIndex(true, "nonexistent_field", new float[]{0.1f, 0.2f, 0.3f, 0.4f}, 1);
163+
assertEquals(result, null);
164+
}
158165
}

0 commit comments

Comments
 (0)