Skip to content

Commit 40de0e5

Browse files
authored
Qdrant: fix skipping upserts (#1208)
### Description In the Qdrant connector, during Upsert(), it checks whether the vector already exists in the db; if so, it calls `continue` and skips upserting. The expected behavior of Upsert() is Insert+Update, but currently only the insert is working updates are explicitly skipped. Resolves #1035 Also includes some minor cleanup on the internal request builder classes: - DeleteRange - UpsertRange ...allow you to create the Builder, add all operations, and Build() in a single statement, so you never have to hold an instance of the Builder as a var just to operate a loop.
1 parent bdc0fad commit 40de0e5

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/DeleteVectorsRequest.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public DeleteVectorsRequest DeleteVector(string qdrantPointId)
3030
return this;
3131
}
3232

33+
public DeleteVectorsRequest DeleteRange(IEnumerable<string> qdrantPointIds)
34+
{
35+
Verify.NotNull(qdrantPointIds, "The point ID collection is NULL");
36+
this.Ids.AddRange(qdrantPointIds);
37+
return this;
38+
}
39+
3340
public HttpRequestMessage Build()
3441
{
3542
this.Validate();

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/Http/ApiSchema/UpsertVectorRequest.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public UpsertVectorRequest UpsertVector(QdrantVectorRecord vectorRecord)
2121
return this;
2222
}
2323

24+
public UpsertVectorRequest UpsertRange(IEnumerable<QdrantVectorRecord> vectorRecords)
25+
{
26+
foreach (var vectorRecord in vectorRecords)
27+
{
28+
this.UpsertVector(vectorRecord);
29+
}
30+
31+
return this;
32+
}
33+
2434
public HttpRequestMessage Build()
2535
{
2636
return HttpRequest.CreatePutRequest(

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorDbClient.cs‎

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,9 @@ public async Task DeleteVectorsByIdAsync(string collectionName, IEnumerable<stri
163163
Verify.NotNullOrEmpty(collectionName, "Collection name is empty");
164164
Verify.NotNull(pointIds, "Qdrant point IDs are NULL");
165165

166-
DeleteVectorsRequest requestBuilder = DeleteVectorsRequest.DeleteFrom(collectionName);
167-
168-
foreach (var pointId in pointIds)
169-
{
170-
requestBuilder.DeleteVector(pointId);
171-
}
172-
173-
using var request = requestBuilder.Build();
174-
166+
using var request = DeleteVectorsRequest.DeleteFrom(collectionName)
167+
.DeleteRange(pointIds)
168+
.Build();
175169
(HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);
176170

177171
try
@@ -239,21 +233,9 @@ public async Task UpsertVectorsAsync(string collectionName, IEnumerable<QdrantVe
239233
Verify.NotNull(vectorData, "The vector data entries are NULL");
240234
Verify.NotNullOrEmpty(collectionName, "Collection name is empty");
241235

242-
var requestBuilder = UpsertVectorRequest.Create(collectionName);
243-
244-
foreach (var record in vectorData)
245-
{
246-
QdrantVectorRecord? existingRecord = await this.GetVectorsByIdAsync(collectionName, new[] { record.PointId }, false, cancellationToken).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
247-
248-
if (existingRecord != null)
249-
{
250-
continue;
251-
}
252-
253-
requestBuilder.UpsertVector(record);
254-
}
255-
256-
using var request = requestBuilder.Build();
236+
using var request = UpsertVectorRequest.Create(collectionName)
237+
.UpsertRange(vectorData)
238+
.Build();
257239
(HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);
258240

259241
try

0 commit comments

Comments
 (0)