Skip to content

Commit 711ac1d

Browse files
committed
Fix PgVectorStore doDelete function as batch
Signed-off-by: CChuYong <[email protected]>
1 parent 58ed5ea commit 711ac1d

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,21 @@ private Object convertIdToPgType(String id) {
319319

320320
@Override
321321
public void doDelete(List<String> idList) {
322-
int updateCount = 0;
323-
for (String id : idList) {
324-
int count = this.jdbcTemplate.update("DELETE FROM " + getFullyQualifiedTableName() + " WHERE id = ?",
325-
UUID.fromString(id));
326-
updateCount = updateCount + count;
327-
}
322+
String sql = "DELETE FROM " + getFullyQualifiedTableName() + " WHERE id = ?";
323+
324+
this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
325+
326+
@Override
327+
public void setValues(PreparedStatement ps, int i) throws SQLException {
328+
var id = idList.get(i);
329+
StatementCreatorUtils.setParameterValue(ps, 1, SqlTypeValue.TYPE_UNKNOWN, convertIdToPgType(id));
330+
}
331+
332+
@Override
333+
public int getBatchSize() {
334+
return idList.size();
335+
}
336+
});
328337
}
329338

330339
@Override

vector-stores/spring-ai-pgvector-store/src/test/java/org/springframework/ai/vectorstore/pgvector/PgVectorStoreIT.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* @author Christian Tzolov
7676
* @author Thomas Vitale
7777
* @author Jihoon Kim
78+
* @author CChuYong
7879
*/
7980
@Testcontainers
8081
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
@@ -232,6 +233,48 @@ public void testToPgTypeWithNonUuidIdType() {
232233
});
233234
}
234235

236+
@Test
237+
public void testBulkOperationWithUuidIdType() {
238+
this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.pgvector.distanceType=" + "COSINE_DISTANCE")
239+
.run(context -> {
240+
241+
VectorStore vectorStore = context.getBean(VectorStore.class);
242+
243+
List<Document> documents = List.of(
244+
new Document(new RandomIdGenerator().generateId(), "TEXT", new HashMap<>()),
245+
new Document(new RandomIdGenerator().generateId(), "TEXT", new HashMap<>()),
246+
new Document(new RandomIdGenerator().generateId(), "TEXT", new HashMap<>()));
247+
vectorStore.add(documents);
248+
249+
List<String> idList = documents.stream().map(Document::getId).toList();
250+
vectorStore.delete(idList);
251+
252+
dropTable(context);
253+
});
254+
}
255+
256+
@Test
257+
public void testBulkOperationWithNonUuidIdType() {
258+
this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.pgvector.distanceType=" + "COSINE_DISTANCE")
259+
.withPropertyValues("test.spring.ai.vectorstore.pgvector.initializeSchema=" + false)
260+
.withPropertyValues("test.spring.ai.vectorstore.pgvector.idType=" + "TEXT")
261+
.run(context -> {
262+
263+
VectorStore vectorStore = context.getBean(VectorStore.class);
264+
initSchema(context);
265+
266+
List<Document> documents = List.of(new Document("NON_UUID_1", "TEXT", new HashMap<>()),
267+
new Document("NON_UUID_2", "TEXT", new HashMap<>()),
268+
new Document("NON_UUID_3", "TEXT", new HashMap<>()));
269+
vectorStore.add(documents);
270+
271+
List<String> idList = documents.stream().map(Document::getId).toList();
272+
vectorStore.delete(idList);
273+
274+
dropTable(context);
275+
});
276+
}
277+
235278
@ParameterizedTest(name = "Filter expression {0} should return {1} records ")
236279
@MethodSource("provideFilters")
237280
public void searchWithInFilter(String expression, Integer expectedRecords) {

0 commit comments

Comments
 (0)