diff --git a/src/it/java/io/weaviate/integration/AggregationITest.java b/src/it/java/io/weaviate/integration/AggregationITest.java index 071607d66..586cac1ea 100644 --- a/src/it/java/io/weaviate/integration/AggregationITest.java +++ b/src/it/java/io/weaviate/integration/AggregationITest.java @@ -165,4 +165,13 @@ public void testNearVector_groupBy_category() { .as(desc.apply("median")).returns((double) expectedPrice, IntegerAggregation.Values::median); }); } + + @Test + public void testCollestionSizeShortcut() { + var things = client.collections.use(COLLECTION); + var countAggregate = things.aggregate + .overAll(x -> x.includeTotalCount(true)).totalCount(); + var size = things.size(); + Assertions.assertThat(size).isEqualTo(countAggregate); + } } diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandle.java b/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandle.java index a90701d52..fb262cbc2 100644 --- a/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandle.java +++ b/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandle.java @@ -1,5 +1,6 @@ package io.weaviate.client6.v1.api.collections; +import java.util.Collection; import java.util.function.Function; import io.weaviate.client6.v1.api.collections.aggregate.WeaviateAggregateClient; @@ -36,4 +37,24 @@ public Paginator paginate() { public Paginator paginate(Function, ObjectBuilder>> fn) { return Paginator.of(this.query, fn); } + + /** + * Get the object count in this collection. + * + *

+ * While made to resemeble {@link Collection#size}, counting Weaviate collection + * objects involves making a network call, making this a blocking operation. + * This method also does not define behaviour for cases where the size of the + * collection exceeds {@link Long#MAX_VALUE} as this is unlikely to happen. + * + *

+ * This is a shortcut for: + * + *

{@code
+   * handle.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount()
+   * }
+ */ + public long size() { + return this.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount(); + } } diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandleAsync.java b/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandleAsync.java index 5c080afc7..9a646d518 100644 --- a/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandleAsync.java +++ b/src/main/java/io/weaviate/client6/v1/api/collections/CollectionHandleAsync.java @@ -1,7 +1,10 @@ package io.weaviate.client6.v1.api.collections; +import java.util.Collection; +import java.util.concurrent.CompletableFuture; import java.util.function.Function; +import io.weaviate.client6.v1.api.collections.aggregate.AggregateResponse; import io.weaviate.client6.v1.api.collections.aggregate.WeaviateAggregateClientAsync; import io.weaviate.client6.v1.api.collections.config.WeaviateConfigClientAsync; import io.weaviate.client6.v1.api.collections.data.WeaviateDataClientAsync; @@ -37,4 +40,28 @@ public AsyncPaginator paginate( Function, ObjectBuilder>> fn) { return AsyncPaginator.of(this.query, fn); } + + /** + * Get the object count in this collection. + * + *

+ * While made to resemeble {@link Collection#size}, counting Weaviate collection + * objects involves making a network call; still, this operation is + * non-blocking, as resolving the underlying {@code CompletableFuture} is + * deferred to the caller. + * + * This method also does not define behaviour for cases where the size of the + * collection exceeds {@link Long#MAX_VALUE} as this is unlikely to happen. + * + *

+ * This is a shortcut for: + * + *

{@code
+   * handle.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount()
+   * }
+ */ + public CompletableFuture size() { + return this.aggregate.overAll(all -> all.includeTotalCount(true)) + .thenApply(AggregateResponse::totalCount); + } }