Skip to content

Commit 8752941

Browse files
committed
chore: add SortBy to fetch objects
1 parent d215a66 commit 8752941

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/it/java/io/weaviate/integration/SearchITest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.weaviate.client6.v1.api.collections.query.Metadata;
3030
import io.weaviate.client6.v1.api.collections.query.QueryMetadata;
3131
import io.weaviate.client6.v1.api.collections.query.QueryResponseGroup;
32+
import io.weaviate.client6.v1.api.collections.query.SortBy;
3233
import io.weaviate.client6.v1.api.collections.query.Where;
3334
import io.weaviate.containers.Container;
3435
import io.weaviate.containers.Container.ContainerGroup;
@@ -257,6 +258,41 @@ public void testFetchObjectsWithFilters() throws IOException {
257258

258259
}
259260

261+
@Test
262+
public void testFetchObjectsWithSort() throws IOException {
263+
var nsNumbers = ns("Numbers");
264+
265+
// Arrange
266+
client.collections.create(nsNumbers,
267+
c -> c.properties(Property.integer("value")));
268+
269+
var numbers = client.collections.use(nsNumbers);
270+
271+
var one = numbers.data.insert(Map.of("value", 1L));
272+
var two = numbers.data.insert(Map.of("value", 2L));
273+
var three = numbers.data.insert(Map.of("value", 3L));
274+
275+
// Act: sort ascending
276+
var asc = numbers.query.fetchObjects(
277+
q -> q.sort(SortBy.property("value")));
278+
279+
Assertions.assertThat(asc.objects())
280+
.hasSize(3)
281+
.extracting(WeaviateObject::properties)
282+
.extracting(object -> object.get("value"))
283+
.containsExactly(1L, 2L, 3L);
284+
285+
// Act: sort descending
286+
var desc = numbers.query.fetchObjects(
287+
q -> q.sort(SortBy.property("value").desc()));
288+
289+
Assertions.assertThat(desc.objects())
290+
.hasSize(3)
291+
.extracting(WeaviateObject::properties)
292+
.extracting(object -> object.get("value"))
293+
.containsExactly(3L, 2L, 1L);
294+
}
295+
260296
@Test
261297
public void testBm25() throws IOException, InterruptedException, ExecutionException {
262298
var nsWords = ns("Words");

src/main/java/io/weaviate/client6/v1/api/collections/query/FetchObjects.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package io.weaviate.client6.v1.api.collections.query;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
36
import java.util.function.Function;
47

58
import io.weaviate.client6.v1.internal.ObjectBuilder;
69
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoSearchGet;
710

8-
public record FetchObjects(BaseQueryOptions common) implements QueryOperator {
11+
public record FetchObjects(BaseQueryOptions common, List<SortBy> sortBy) implements QueryOperator {
912

1013
public static FetchObjects of(Function<Builder, ObjectBuilder<FetchObjects>> fn) {
1114
return fn.apply(new Builder()).build();
1215
}
1316

1417
public FetchObjects(Builder builder) {
15-
this(builder.baseOptions());
18+
this(builder.baseOptions(), builder.sortBy);
1619
}
1720

1821
public static class Builder extends BaseQueryOptions.Builder<Builder, FetchObjects> {
22+
private final List<SortBy> sortBy = new ArrayList<>();
23+
24+
public Builder sort(SortBy... sortBy) {
25+
return sort(Arrays.asList(sortBy));
26+
}
27+
28+
public Builder sort(List<SortBy> sortBy) {
29+
this.sortBy.addAll(sortBy);
30+
return this;
31+
}
1932

2033
@Override
2134
public final FetchObjects build() {
@@ -26,5 +39,11 @@ public final FetchObjects build() {
2639
@Override
2740
public void appendTo(WeaviateProtoSearchGet.SearchRequest.Builder req) {
2841
common.appendTo(req);
42+
43+
for (final var sort : sortBy) {
44+
req.addSortBy(WeaviateProtoSearchGet.SortBy.newBuilder()
45+
.addAllPath(sort.path())
46+
.setAscending(sort.ascending()));
47+
}
2948
}
3049
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.weaviate.client6.v1.api.collections.query;
2+
3+
import java.util.List;
4+
5+
public record SortBy(List<String> path, boolean ascending) {
6+
public static SortBy property(String property) {
7+
return new SortBy(List.of(property), true);
8+
}
9+
10+
public SortBy asc() {
11+
return new SortBy(path, true);
12+
}
13+
14+
public SortBy desc() {
15+
return new SortBy(path, false);
16+
}
17+
}

0 commit comments

Comments
 (0)