Skip to content

Commit 98cbd92

Browse files
committed
Refactor NearVector handling and improve syntax consistency
- Introduced NearVectorInput to encapsulate vector handling. - Updated various methods across the codebase to utilize NearVectorInput instead of Vectors. - Enhanced array handling syntax for improved readability. - Adjusted assertions in tests to align with new vector handling. - Ensured consistent use of implicit conversions for vector types. - Refactored gRPC client methods to accommodate new vector structures. - Improved target vector handling in aggregate and search functionalities.
1 parent 26b3cfd commit 98cbd92

16 files changed

+504
-235
lines changed

src/Weaviate.Client.Tests/Integration/TestBatch.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,22 @@ public async Task Test_Batch_ReferenceAddMany()
8787

8888
// First batch: each "From" object references the "To" object with the same index
8989
var batchReturn1 = await collection.Data.ReferenceAddMany(
90-
Enumerable
91-
.Range(0, numObjects)
92-
.Select(i => new DataReference(uuidsFrom[i], "ref", [uuidsTo[i]]))
93-
.ToArray(),
90+
[
91+
.. Enumerable
92+
.Range(0, numObjects)
93+
.Select(i => new DataReference(uuidsFrom[i], "ref", [uuidsTo[i]])),
94+
],
9495
TestContext.Current.CancellationToken
9596
);
9697
Assert.False(batchReturn1.HasErrors);
9798

9899
// Second batch: each "From" object references the first 3 "To" objects
99100
var batchReturn2 = await collection.Data.ReferenceAddMany(
100-
Enumerable
101-
.Range(0, numObjects)
102-
.Select(i => new DataReference(uuidsFrom[i], "ref", [.. uuidsTo.Take(3)]))
103-
.ToArray(),
101+
[
102+
.. Enumerable
103+
.Range(0, numObjects)
104+
.Select(i => new DataReference(uuidsFrom[i], "ref", [.. uuidsTo.Take(3)])),
105+
],
104106
TestContext.Current.CancellationToken
105107
);
106108
Assert.False(batchReturn2.HasErrors);
@@ -131,7 +133,7 @@ public async Task Test_Batch_ReferenceAddMany()
131133
.Select(r => (long)r.Properties["number"]!)
132134
.OrderBy(x => x)
133135
.ToList();
134-
Assert.Equal(new List<long> { 0, 1, 2 }, refs);
136+
Assert.Equal([0, 1, 2], refs);
135137
}
136138
}
137139
}

src/Weaviate.Client.Tests/Integration/TestIterator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ await collection.Data.InsertMany(
112112

113113
// Sort by data property for consistent comparison
114114
var allData = objects.Select(obj => (long)obj.Properties["data"]!).OrderBy(x => x).ToList();
115-
Assert.Equal(Enumerable.Range(0, 10).Select(Convert.ToInt64).ToList(), allData);
115+
Assert.Equal([.. Enumerable.Range(0, 10).Select(Convert.ToInt64)], allData);
116116

117117
// Test expectations based on parameters
118118
if (includeVector && returnSpecificProperties != true && returnFullMetadata == true)
@@ -253,7 +253,7 @@ public async Task Test_Iterator_Basic(uint count)
253253
Assert.Equal(firstOrder, ret);
254254
}
255255

256-
Assert.Equal(expected, ret.OrderBy(x => x).ToList());
256+
Assert.Equal(expected, [.. ret.OrderBy(x => x)]);
257257
}
258258
}
259259

src/Weaviate.Client.Tests/Integration/TestNamedVectorMultiTarget.cs

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace Weaviate.Client.Tests.Integration;
77
using Weaviate.Client.Models;
88
using Xunit;
99

10+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
11+
"Performance",
12+
"CA1861:Avoid constant arrays as arguments",
13+
Justification = "Irrelevant"
14+
)]
1015
public class TestNamedVectorMultiTarget : IntegrationTests
1116
{
1217
[Fact]
@@ -51,94 +56,37 @@ public async Task Test_NamedVector_MultiTargetVectorPerTarget()
5156
Assert.Equal(expected, ids);
5257
}
5358

54-
public static TheoryData<Vectors, string[]> MultiInputCombinations =>
59+
public static TheoryData<NearVectorInput, string[]> MultiInputCombinations =>
5560
new(
5661
(
57-
new Vectors
58-
{
59-
{ "first", new[] { 0f, 1f } },
60-
{
61-
"second",
62-
new float[,]
63-
{
64-
{ 1f, 0f, 0f },
65-
{ 0f, 0f, 1f },
66-
}
67-
},
68-
},
69-
new[] { "first", "second" }
70-
),
71-
(
72-
new Vectors
62+
new NearVectorInput
7363
{
7464
{ "first", new[] { 0f, 1f } },
75-
{
76-
"second",
77-
new float[,]
78-
{
79-
{ 1f, 0f, 0f },
80-
{ 0f, 0f, 1f },
81-
}
82-
},
65+
{ "second", new float[] { 1f, 0f, 0f }, new float[] { 0f, 0f, 1f } },
8366
},
8467
new[] { "first", "second" }
8568
),
8669
(
87-
new Vectors
70+
new NearVectorInput
8871
{
89-
{
90-
"first",
91-
new float[,]
92-
{
93-
{ 0f, 1f },
94-
{ 0f, 1f },
95-
}
96-
},
72+
{ "first", new[] { 0f, 1f }, new[] { 0f, 1f } },
9773
{ "second", new[] { 1f, 0f, 0f } },
9874
},
9975
new[] { "first", "second" }
10076
),
10177
(
102-
new Vectors
78+
new NearVectorInput
10379
{
104-
{
105-
"first",
106-
new float[,]
107-
{
108-
{ 0f, 1f },
109-
{ 0f, 1f },
110-
}
111-
},
112-
{
113-
"second",
114-
new float[,]
115-
{
116-
{ 1f, 0f, 0f },
117-
{ 0f, 0f, 1f },
118-
}
119-
},
80+
{ "first", new float[] { 0f, 1f }, new float[] { 0f, 1f } },
81+
{ "second", new float[] { 1f, 0f, 0f }, new float[] { 0f, 0f, 1f } },
12082
},
12183
new[] { "first", "second" }
12284
),
12385
(
124-
new Vectors
86+
new NearVectorInput
12587
{
126-
{
127-
"first",
128-
new float[,]
129-
{
130-
{ 0f, 1f },
131-
{ 0f, 1f },
132-
}
133-
},
134-
{
135-
"second",
136-
new float[,]
137-
{
138-
{ 1f, 0f, 0f },
139-
{ 0f, 0f, 1f },
140-
}
141-
},
88+
{ "first", new float[] { 0f, 1f }, new float[] { 0f, 1f } },
89+
{ "second", new float[] { 1f, 0f, 0f }, new float[] { 0f, 0f, 1f } },
14290
},
14391
new[] { "second", "first" }
14492
)
@@ -147,7 +95,7 @@ public async Task Test_NamedVector_MultiTargetVectorPerTarget()
14795
[Theory]
14896
[MemberData(nameof(MultiInputCombinations))]
14997
public async Task Test_SameTargetVector_MultipleInputCombinations(
150-
Vectors nearVector,
98+
NearVectorInput nearVector,
15199
string[] targetVector
152100
)
153101
{
@@ -438,17 +386,10 @@ await collection.Query.FetchObjects(
438386
var uuid2 = results[1].UUID!.Value;
439387

440388
var objs = await collection.Query.NearVector(
441-
new Vectors
389+
new NearVectorInput
442390
{
443391
{ "first", new[] { 0f, 1f } },
444-
{
445-
"second",
446-
new[,]
447-
{
448-
{ 1f, 0f, 0f },
449-
{ 0f, 0f, 1f },
450-
}
451-
},
392+
{ "second", new[] { 1f, 0f, 0f }, new[] { 0f, 0f, 1f } },
452393
},
453394
targetVector: targetVector,
454395
returnMetadata: MetadataOptions.All,

src/Weaviate.Client.Tests/Integration/TestQueries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public async Task Test_Generate_By_Ids(Guid[] ids, int expectedLen, HashSet<Guid
252252
Assert.NotNull(res);
253253
Assert.NotNull(res.Generative);
254254
Assert.Equal(expectedLen, res.Objects.Count());
255-
Assert.Equal(expected, res.Objects.Select(o => o.UUID!.Value).ToHashSet());
255+
Assert.Equal(expected, [.. res.Objects.Select(o => o.UUID!.Value)]);
256256
foreach (var obj in res.Objects)
257257
{
258258
Assert.NotNull(obj.Generative);

src/Weaviate.Client.Tests/Integration/TestSearchHybrid.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Weaviate.Client.Tests.Integration;
44

5+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
6+
"Performance",
7+
"CA1861:Avoid constant arrays as arguments",
8+
Justification = "Irrelevant"
9+
)]
510
public partial class SearchTests : IntegrationTests
611
{
712
[Theory]
@@ -50,7 +55,7 @@ await collection.Query.Hybrid(
5055
)
5156
).Objects;
5257

53-
Assert.Equal(2, objs.Count());
58+
Assert.Equal(2, objs.Count);
5459
}
5560

5661
[Fact]
@@ -158,7 +163,7 @@ await collection.Query.Hybrid(
158163
)
159164
).Objects;
160165

161-
Assert.Equal(limit, (uint)objs.Count());
166+
Assert.Equal(limit, (uint)objs.Count);
162167
}
163168

164169
[Theory]
@@ -190,7 +195,7 @@ await collection.Query.Hybrid(
190195
)
191196
).Objects;
192197

193-
Assert.Equal(expected, objs.Count());
198+
Assert.Equal(expected, objs.Count);
194199
}
195200

196201
[Fact]
@@ -222,7 +227,7 @@ await collection.Query.BM25(
222227
cancellationToken: TestContext.Current.CancellationToken
223228
)
224229
).Objects;
225-
Assert.Equal(hybridRes.Count(), bm25Res.Count());
230+
Assert.Equal(hybridRes.Count, bm25Res.Count);
226231
Assert.True(hybridRes.Zip(bm25Res).All(pair => pair.First.UUID == pair.Second.UUID));
227232

228233
hybridRes = (
@@ -238,7 +243,7 @@ await collection.Query.NearText(
238243
cancellationToken: TestContext.Current.CancellationToken
239244
)
240245
).Objects;
241-
Assert.Equal(hybridRes.Count(), textRes.Count());
246+
Assert.Equal(hybridRes.Count, textRes.Count);
242247
Assert.True(hybridRes.Zip(textRes).All(pair => pair.First.UUID == pair.Second.UUID));
243248
}
244249

@@ -281,7 +286,7 @@ await collection.Query.Hybrid(
281286
).Objects;
282287

283288
Assert.Equal(uuidBanana, hybridObjs.First().UUID);
284-
Assert.Equal(3, hybridObjs.Count());
289+
Assert.Equal(3, hybridObjs.Count);
285290

286291
var nearVec = (
287292
await collection.Query.NearVector(
@@ -353,7 +358,7 @@ await collection.Query.Hybrid(
353358
).Objects;
354359

355360
Assert.Equal(uuidBanana, hybridObjs.First().UUID);
356-
Assert.Equal(3, hybridObjs.Count());
361+
Assert.Equal(3, hybridObjs.Count);
357362

358363
var nearVec = (
359364
await collection.Query.NearVector(
@@ -416,7 +421,7 @@ await collection.Query.Hybrid(
416421
).Objects;
417422

418423
Assert.Equal(uuidBananaPudding, hybridObjs.First().UUID);
419-
Assert.Equal(3, hybridObjs.Count());
424+
Assert.Equal(3, hybridObjs.Count);
420425

421426
var hybridObjs2 = (
422427
await collection.Query.Hybrid(
@@ -473,7 +478,7 @@ await collection.Query.Hybrid(
473478
).Objects;
474479

475480
Assert.Equal(uuidBananaPudding, hybridObjs.First().UUID);
476-
Assert.Equal(3, hybridObjs.Count());
481+
Assert.Equal(3, hybridObjs.Count);
477482

478483
var hybridObjs2 = (
479484
await collection.Query.Hybrid(
@@ -544,14 +549,17 @@ await collection.Query.Hybrid(
544549
Assert.Equal(uuid1, objs[0].UUID);
545550
Assert.Equal(uuid2, objs[1].UUID);
546551

547-
objs = (
548-
await collection.Query.Hybrid(
549-
query: null,
550-
vectors: new HybridNearVector(vector, Certainty: null, Distance: 0.1f),
551-
targetVector: new[] { "first", "second" },
552-
cancellationToken: TestContext.Current.CancellationToken
553-
)
554-
).Objects.ToList();
552+
objs =
553+
[
554+
.. (
555+
await collection.Query.Hybrid(
556+
query: null,
557+
vectors: new HybridNearVector(vector, Certainty: null, Distance: 0.1f),
558+
targetVector: new[] { "first", "second" },
559+
cancellationToken: TestContext.Current.CancellationToken
560+
)
561+
).Objects,
562+
];
555563

556564
Assert.Single(objs);
557565
Assert.Equal(uuid1, objs[0].UUID);
@@ -706,15 +714,18 @@ await collection.Query.Hybrid(
706714
Assert.Equal(3, objs.Count);
707715
Assert.Equal(uuid1, objs[0].UUID);
708716

709-
objs = (
710-
await collection.Query.Hybrid(
711-
"name",
712-
vectors: Vectors.Create(1f, 0f, 0f),
713-
maxVectorDistance: 0.1f,
714-
alpha: 0.7f,
715-
cancellationToken: TestContext.Current.CancellationToken
716-
)
717-
).ToList();
717+
objs =
718+
[
719+
.. (
720+
await collection.Query.Hybrid(
721+
"name",
722+
vectors: Vectors.Create(1f, 0f, 0f),
723+
maxVectorDistance: 0.1f,
724+
alpha: 0.7f,
725+
cancellationToken: TestContext.Current.CancellationToken
726+
)
727+
),
728+
];
718729
Assert.Single(objs);
719730
Assert.Equal(uuid1, objs[0].UUID);
720731
}

0 commit comments

Comments
 (0)