Skip to content

Commit e030f5a

Browse files
committed
Search points.
1 parent 0ff728b commit e030f5a

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

Diff for: README.md

+32-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,38 @@ var client = new QdrantHttpClient
1111
apiKey: ""
1212
);
1313

14-
// create a new collection
15-
await client.CreateCollection("my_collection", new VectorParams(size: 100, distance: Distance.COSINE));
14+
var collectionName = "my_collection";
1615

17-
// list all the collections
16+
// Create a new collection
17+
await client.CreateCollection(collectionName, new VectorParams(size: 4, distance: Distance.DOT));
18+
19+
// List all the collections
1820
var collections = await client.GetCollections();
21+
22+
// Insert vectors
23+
await client.Upsert(collectionName, points: new List<PointStruct>
24+
{
25+
new PointStruct(id:1, vector: new float[]{ 0.05f, 0.61f, 0.76f, 0.74f }),
26+
new PointStruct(id:2, vector: new float[]{ 0.19f, 0.81f, 0.75f, 0.11f }),
27+
new PointStruct(id:3, vector: new float[]{ 0.36f, 0.55f, 0.47f, 0.94f }),
28+
new PointStruct(id:4, vector: new float[]{ 0.18f, 0.01f, 0.85f, 0.80f }),
29+
new PointStruct(id:5, vector: new float[]{ 0.24f, 0.18f, 0.22f, 0.44f }),
30+
new PointStruct(id:6, vector: new float[]{ 0.35f, 0.08f, 0.11f, 0.44f })
31+
});
32+
33+
// Get collection info
34+
var collectionInfo = await client.GetCollection(collectionName);
35+
36+
Console.WriteLine($"Upserted {collectionInfo.Result.VectorsCount} points");
37+
38+
// Vector search
39+
var result = await client.Search
40+
(
41+
collectionName,
42+
new float[] { 0.2f, 0.1f, 0.9f, 0.7f },
43+
limit: 3
44+
);
45+
46+
// Delete collection
47+
await client.DeleteCollection(collectionName);
1948
```

Diff for: src/QdrantCSharp/Models/QdrantHttpResponse.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QdrantCSharp.Models;
22

3-
public class QdrantHttpResponse<T> where T : new()
3+
public class QdrantHttpResponse<T>
44
{
55
public float Time { get; set; }
66
public string Status { get; set; } = "ok";
7-
public T Result { get; set; } = new();
7+
public T Result { get; set; }
88
}

Diff for: src/QdrantCSharp/Models/ScoredPoint.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace QdrantCSharp.Models;
2+
3+
public class ScoredPoint
4+
{
5+
public int Id { get; set; }
6+
public int Version { get; set; }
7+
public float Score { get; set; }
8+
public float[] Vector { get; set; }
9+
}

Diff for: src/QdrantCSharp/Models/SearchBody.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace QdrantCSharp.Models;
4+
5+
public class SearchBody
6+
{
7+
public float[] Vector { get; set; }
8+
public int Limit { get; set; }
9+
public int Offset { get; set; }
10+
[JsonPropertyName("score_threshold")]
11+
public float ScoreThreshold { get; set; }
12+
}

Diff for: src/QdrantCSharp/QdrantHttpClient.cs

+8
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ public async Task<QdrantHttpResponse<UpdateResult>> Upsert(string collectionName
6262
{
6363
Points = points
6464
});
65+
66+
public async Task<QdrantHttpResponse<ScoredPoint[]>> Search(string collectionName, float[] queryVector, int limit = 10, int offset = 0)
67+
=> await _pointsApi.Search(collectionName, new SearchBody
68+
{
69+
Limit = limit,
70+
Offset = offset,
71+
Vector = queryVector
72+
});
6573
}

Diff for: src/QdrantCSharp/RestAPIs/IQdrantPointApi.cs

+3
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ public interface IQdrantPointApi
77
{
88
[Put("/collections/{collection_name}/points")]
99
Task<QdrantHttpResponse<UpdateResult>> Upsert([AliasAs("collection_name")] string collectionName, [Body] PointsUpsertBody body);
10+
11+
[Post("/collections/{collection_name}/points/search")]
12+
Task<QdrantHttpResponse<ScoredPoint[]>> Search([AliasAs("collection_name")] string collectionName, [Body] SearchBody body);
1013
}

Diff for: test/ConsoleApp/Program.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// See https://aka.ms/new-console-template for more information
22

33
using QdrantCSharp;
4+
using QdrantCSharp.Enums;
45
using QdrantCSharp.Models;
56

67
var client = new QdrantHttpClient
78
(
89
url: "https://808bac29-e17d-4f4e-bcc5-2bb3f873b1a1.us-east-1-0.aws.cloud.qdrant.io:6333",
910
apiKey: ""
1011
);
11-
// await client.CreateCollection("my_collection", new VectorParams(size: 4, distance: Distance.COSINE));
12+
13+
var collectionName = "my_collection";
14+
await client.CreateCollection(collectionName, new VectorParams(size: 4, distance: Distance.DOT));
1215
var collections = await client.GetCollections();
13-
await client.Upsert("my_collection", points: new List<PointStruct>
16+
await client.Upsert(collectionName, points: new List<PointStruct>
1417
{
1518
new PointStruct(id:1, vector: new float[]{ 0.05f, 0.61f, 0.76f, 0.74f }),
1619
new PointStruct(id:2, vector: new float[]{ 0.19f, 0.81f, 0.75f, 0.11f }),
@@ -19,9 +22,15 @@
1922
new PointStruct(id:5, vector: new float[]{ 0.24f, 0.18f, 0.22f, 0.44f }),
2023
new PointStruct(id:6, vector: new float[]{ 0.35f, 0.08f, 0.11f, 0.44f })
2124
});
22-
var collectionInfo = await client.GetCollection("my_collection");
25+
var collectionInfo = await client.GetCollection(collectionName);
2326
Console.WriteLine($"Upserted {collectionInfo.Result.VectorsCount} points");
24-
// await client.DeleteCollection("my_collection");
27+
var result = await client.Search
28+
(
29+
collectionName,
30+
new float[] { 0.2f, 0.1f, 0.9f, 0.7f },
31+
limit: 3
32+
);
33+
await client.DeleteCollection(collectionName);
2534
collections = await client.GetCollections();
2635

2736
Console.ReadLine();

0 commit comments

Comments
 (0)