Skip to content

Commit 7b0fb2a

Browse files
authored
Merge pull request #122 from weaviate/feat/sort-and-other-fixes
Simple test for Sort in queries + other related fixes
2 parents a3894a7 + c6b609d commit 7b0fb2a

File tree

7 files changed

+112
-34
lines changed

7 files changed

+112
-34
lines changed

src/Example/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static async Task Main()
8282
{
8383
Name = "Cat",
8484
Description = "Lots of Cats of multiple breeds",
85-
Properties = [.. Property.FromCollection<Cat>()],
85+
Properties = [.. Property.FromClass<Cat>()],
8686
VectorConfig = new VectorConfig("default", new Vectorizer.Text2VecWeaviate()),
8787
};
8888

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Weaviate.Client.Models;
2+
using Weaviate.Client.Tests.Common;
3+
4+
namespace Weaviate.Client.Tests.Integration;
5+
6+
[Collection("TestQueries")]
7+
public class TestQueries : IntegrationTests
8+
{
9+
[Theory()]
10+
[InlineData("testText")]
11+
[InlineData("testInt")]
12+
[InlineData("testNumber")]
13+
[InlineData("testDate")]
14+
public async Task Test_Sorting(string propertyName)
15+
{
16+
// Arrange
17+
var collection = await this.CollectionFactory<TestProperties>(
18+
properties: Property.FromClass<TestProperties>(),
19+
vectorConfig: new VectorConfig("default", new Vectorizer.SelfProvided())
20+
);
21+
22+
var testData = new[]
23+
{
24+
new TestProperties
25+
{
26+
TestText = "Alice",
27+
TestInt = 1,
28+
TestNumber = 1.1,
29+
TestDate = DateTime.Parse("2023-01-01"),
30+
},
31+
new TestProperties
32+
{
33+
TestText = "Bob",
34+
TestInt = 2,
35+
TestNumber = 2.2,
36+
TestDate = DateTime.Parse("2023-01-02"),
37+
},
38+
new TestProperties
39+
{
40+
TestText = "Charlie",
41+
TestInt = 3,
42+
TestNumber = 3.3,
43+
TestDate = DateTime.Parse("2023-01-03"),
44+
},
45+
};
46+
47+
await collection.Data.InsertMany(testData);
48+
49+
// Act
50+
var dataDesc = await collection.Query.FetchObjects(
51+
sort: Sort.ByProperty(propertyName).Descending()
52+
);
53+
var dataAsc = await collection.Query.FetchObjects(
54+
sort: Sort.ByProperty(propertyName).Ascending()
55+
);
56+
57+
var namesDesc = dataDesc.Select(d => d.Properties["testText"]);
58+
var namesAsc = dataAsc.Select(d => d.Properties["testText"]);
59+
60+
// Assert
61+
Assert.Equal(new List<string> { "Charlie", "Bob", "Alice" }, namesDesc);
62+
Assert.Equal(new List<string> { "Alice", "Bob", "Charlie" }, namesAsc);
63+
}
64+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public async Task<CollectionClient<TData>> CollectionFactory<TData>(
100100

101101
description ??= TestContext.Current.TestMethod?.MethodName ?? string.Empty;
102102

103-
properties ??= [.. Property.FromCollection<TData>()];
103+
properties ??= Property.FromClass<TData>();
104104

105105
ArgumentException.ThrowIfNullOrEmpty(name);
106106

@@ -113,7 +113,7 @@ public async Task<CollectionClient<TData>> CollectionFactory<TData>(
113113
{
114114
Name = name,
115115
Description = description,
116-
Properties = properties.Concat(references!.Select(p => (Property)p)).ToList(),
116+
Properties = properties?.Concat(references!.Select(p => (Property)p)).ToList() ?? [],
117117
VectorConfig = vectorConfig,
118118
MultiTenancyConfig = multiTenancyConfig,
119119
InvertedIndexConfig = invertedIndexConfig,

src/Weaviate.Client/Models/Property.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static class DataType
192192
public static string UuidArray => "uuid[]";
193193
public static string GeoCoordinate => "geoCoordinates";
194194
public static string Blob => "blob";
195-
public static string PhoneNumber => "phone";
195+
public static string PhoneNumber => "phoneNumber";
196196
public static string Object => "object";
197197
public static string ObjectArray => "object[]";
198198
}
@@ -272,11 +272,12 @@ public static Reference Reference(
272272
) => new(name, targetCollection, description);
273273

274274
// Extract collection properties from type specified by TData.
275-
public static IEnumerable<Property> FromCollection<TData>()
275+
public static Property[] FromClass<TData>()
276276
{
277277
return typeof(TData)
278278
.GetProperties()
279-
.Select(x => PropertyHelper.ForType(x.PropertyType)(x.Name));
279+
.Select(x => PropertyHelper.ForType(x.PropertyType)(x.Name))
280+
.ToArray();
280281
}
281282

282283
public override int GetHashCode()

src/Weaviate.Client/Models/Sort.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ private Sort() { }
88

99
public static Sort ByCreationTime(bool ascending = true)
1010
{
11-
var s = new Sort().ByProperty("_creationTimeUnix");
11+
var s = ByProperty("_creationTimeUnix");
1212
return ascending ? s.Ascending() : s.Descending();
1313
}
1414

15-
public Sort ByProperty(string name)
15+
public static Sort ByProperty(string name)
1616
{
17-
InternalSort.Path.Add(name);
18-
return this;
17+
return new Sort().ByProperty(name);
1918
}
2019

2120
public Sort Ascending()
@@ -30,3 +29,12 @@ public Sort Descending()
3029
return this;
3130
}
3231
}
32+
33+
public static partial class SortExtensions
34+
{
35+
public static Sort ByProperty(this Sort sort, string name)
36+
{
37+
sort.InternalSort.Path.Add(name);
38+
return sort;
39+
}
40+
}

src/Weaviate.Client/QueryClient.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public async Task<GroupByResult> FetchObjects(
1919
Models.GroupByRequest groupBy,
2020
uint? limit = null,
2121
Filter? filter = null,
22-
IEnumerable<Sort>? sort = null,
22+
OneOrManyOf<Sort>? sort = null,
2323
Rerank? rerank = null,
2424
string? tenant = null,
2525
OneOrManyOf<string>? returnProperties = null,
@@ -30,9 +30,9 @@ public async Task<GroupByResult> FetchObjects(
3030
await _client.GrpcClient.FetchObjects(
3131
_collectionName,
3232
limit: limit,
33-
sort: sort,
3433
rerank: rerank,
3534
filter: filter,
35+
sort: sort,
3636
groupBy: groupBy,
3737
tenant: tenant ?? _collectionClient.Tenant,
3838
consistencyLevel: _collectionClient.ConsistencyLevel,
@@ -45,7 +45,7 @@ await _client.GrpcClient.FetchObjects(
4545
public async Task<WeaviateResult> FetchObjects(
4646
uint? limit = null,
4747
Filter? filter = null,
48-
IEnumerable<Sort>? sort = null,
48+
OneOrManyOf<Sort>? sort = null,
4949
Rerank? rerank = null,
5050
string? tenant = null,
5151
OneOrManyOf<string>? returnProperties = null,
@@ -56,9 +56,9 @@ public async Task<WeaviateResult> FetchObjects(
5656
await _client.GrpcClient.FetchObjects(
5757
_collectionName,
5858
limit: limit,
59-
sort: sort,
6059
rerank: rerank,
6160
filter: filter,
61+
sort: sort,
6262
tenant: tenant ?? _collectionClient.Tenant,
6363
returnProperties: returnProperties,
6464
returnReferences: returnReferences,
@@ -89,6 +89,8 @@ public async Task<WeaviateResult> FetchObjectsByIDs(
8989
uint? limit = null,
9090
string? tenant = null,
9191
Rerank? rerank = null,
92+
Filter? filter = null,
93+
OneOrManyOf<Sort>? sort = null,
9294
OneOrManyOf<string>? returnProperties = null,
9395
IList<QueryReference>? returnReferences = null,
9496
MetadataQuery? returnMetadata = null
@@ -97,7 +99,8 @@ public async Task<WeaviateResult> FetchObjectsByIDs(
9799
await _client.GrpcClient.FetchObjects(
98100
_collectionName,
99101
limit: limit,
100-
filter: Filter.WithIDs(ids),
102+
filter: filter != null ? Filter.WithIDs(ids) & filter : Filter.WithIDs(ids),
103+
sort: sort,
101104
tenant: tenant ?? _collectionClient.Tenant,
102105
rerank: rerank,
103106
returnProperties: returnProperties,
@@ -117,6 +120,7 @@ public async Task<WeaviateResult> NearText(
117120
Move? moveTo = null,
118121
Move? moveAway = null,
119122
string? tenant = null,
123+
Filter? filter = null,
120124
Rerank? rerank = null,
121125
OneOrManyOf<string>? returnProperties = null,
122126
IList<QueryReference>? returnReferences = null,
@@ -131,6 +135,7 @@ await _client.GrpcClient.SearchNearText(
131135
limit: limit,
132136
moveTo: moveTo,
133137
moveAway: moveAway,
138+
filters: filter,
134139
tenant: tenant ?? _collectionClient.Tenant,
135140
rerank: rerank,
136141
consistencyLevel: _collectionClient.ConsistencyLevel,
@@ -150,7 +155,7 @@ public async Task<GroupByResult> NearText(
150155
uint? limit = null,
151156
uint? offset = null,
152157
uint? autoCut = null,
153-
Filter? filters = null,
158+
Filter? filter = null,
154159
Rerank? rerank = null,
155160
string[]? targetVector = null,
156161
OneOrManyOf<string>? returnProperties = null,
@@ -169,7 +174,7 @@ await _client.GrpcClient.SearchNearText(
169174
limit: limit,
170175
offset: offset,
171176
autoCut: autoCut,
172-
filters: filters,
177+
filters: filter,
173178
tenant: _collectionClient.Tenant,
174179
rerank: rerank,
175180
targetVector: targetVector,
@@ -182,6 +187,7 @@ await _client.GrpcClient.SearchNearText(
182187

183188
public async Task<WeaviateResult> NearVector(
184189
Vectors vector,
190+
Filter? filter = null,
185191
float? certainty = null,
186192
float? distance = null,
187193
uint? limit = null,
@@ -200,6 +206,7 @@ await _client.GrpcClient.SearchNearVector(
200206
certainty: certainty,
201207
limit: limit,
202208
targetVector: targetVector,
209+
filters: filter,
203210
tenant: tenant ?? _collectionClient.Tenant,
204211
rerank: rerank,
205212
consistencyLevel: _collectionClient.ConsistencyLevel,
@@ -212,6 +219,7 @@ await _client.GrpcClient.SearchNearVector(
212219
public async Task<GroupByResult> NearVector(
213220
Vectors vector,
214221
GroupByRequest groupBy,
222+
Filter? filter = null,
215223
float? distance = null,
216224
float? certainty = null,
217225
uint? limit = null,
@@ -227,6 +235,7 @@ await _client.GrpcClient.SearchNearVector(
227235
_collectionClient.Name,
228236
vector,
229237
groupBy,
238+
filters: filter,
230239
distance: distance,
231240
certainty: certainty,
232241
limit: limit,
@@ -245,7 +254,6 @@ public async Task<GroupByResult> BM25(
245254
GroupByRequest groupBy,
246255
string[]? searchFields = null,
247256
Filter? filter = null,
248-
IEnumerable<Sort>? sort = null,
249257
uint? autoCut = null,
250258
uint? limit = null,
251259
uint? offset = null,
@@ -258,15 +266,12 @@ public async Task<GroupByResult> BM25(
258266
IList<QueryReference>? returnReferences = null
259267
)
260268
{
261-
var sortBy = sort?.Select(s => s.InternalSort);
262-
var grpcFilter = filter?.InternalFilter;
263269
return (
264270
await _client.GrpcClient.SearchBM25(
265271
_collectionClient.Name,
266272
query: query,
267273
searchFields: searchFields,
268-
filter: grpcFilter,
269-
sort: sortBy,
274+
filter: filter,
270275
autoCut: autoCut,
271276
limit: limit,
272277
offset: offset,
@@ -286,7 +291,6 @@ public async Task<WeaviateResult> BM25(
286291
string query,
287292
string[]? searchFields = null,
288293
Filter? filter = null,
289-
IEnumerable<Sort>? sort = null,
290294
uint? autoCut = null,
291295
uint? limit = null,
292296
uint? offset = null,
@@ -299,15 +303,12 @@ public async Task<WeaviateResult> BM25(
299303
IList<QueryReference>? returnReferences = null
300304
)
301305
{
302-
var sortBy = sort?.Select(s => s.InternalSort);
303-
var grpcFilter = filter?.InternalFilter;
304306
return (
305307
await _client.GrpcClient.SearchBM25(
306308
_collectionClient.Name,
307309
query: query,
308310
searchFields: searchFields,
309-
filter: grpcFilter,
310-
sort: sortBy,
311+
filter: filter,
311312
autoCut: autoCut,
312313
limit: limit,
313314
offset: offset,

0 commit comments

Comments
 (0)