Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/it/java/io/weaviate/integration/AggregationITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,4 +37,24 @@ public Paginator<T> paginate() {
public Paginator<T> paginate(Function<Paginator.Builder<T>, ObjectBuilder<Paginator<T>>> fn) {
return Paginator.of(this.query, fn);
}

/**
* Get the object count in this collection.
*
* <p>
* 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.
*
* <p>
* This is a shortcut for:
*
* <pre>{@code
* handle.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount()
* }</pre>
*/
public long size() {
return this.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,4 +40,28 @@ public AsyncPaginator<PropertiesT> paginate(
Function<AsyncPaginator.Builder<PropertiesT>, ObjectBuilder<AsyncPaginator<PropertiesT>>> fn) {
return AsyncPaginator.of(this.query, fn);
}

/**
* Get the object count in this collection.
*
* <p>
* 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.
*
* <p>
* This is a shortcut for:
*
* <pre>{@code
* handle.aggregate.overAll(all -> all.includeTotalCount(true)).totalCount()
* }</pre>
*/
public CompletableFuture<Long> size() {
return this.aggregate.overAll(all -> all.includeTotalCount(true))
.thenApply(AggregateResponse::totalCount);
}
}