Skip to content

Commit e7262ff

Browse files
committed
add support for facets
1 parent 403e07f commit e7262ff

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/main/java/io/qdrant/client/QdrantClient.java

+31
Original file line numberDiff line numberDiff line change
@@ -2805,6 +2805,37 @@ public ListenableFuture<List<PointGroup>> queryGroupsAsync(
28052805
future, response -> response.getResult().getGroupsList(), MoreExecutors.directExecutor());
28062806
}
28072807

2808+
/**
2809+
* Perform facet counts. For each value in the field, count the number of points that have this
2810+
* value and match the conditions.
2811+
*
2812+
* @param request the facet counts request
2813+
* @return a new instance of {@link ListenableFuture}
2814+
*/
2815+
public ListenableFuture<List<Points.FacetHit>> facetAsync(Points.FacetCounts request) {
2816+
return facetAsync(request, null);
2817+
}
2818+
2819+
/**
2820+
* Perform facet counts. For each value in the field, count the number of points that have this
2821+
* value and match the conditions.
2822+
*
2823+
* @param request the facet counts request
2824+
* @param timeout the timeout for the call.
2825+
* @return a new instance of {@link ListenableFuture}
2826+
*/
2827+
public ListenableFuture<List<Points.FacetHit>> facetAsync(
2828+
Points.FacetCounts request, @Nullable Duration timeout) {
2829+
Preconditions.checkArgument(
2830+
!request.getCollectionName().isEmpty(), "Collection name must not be empty");
2831+
2832+
logger.debug("Facet on '{}'", request.getCollectionName());
2833+
ListenableFuture<Points.FacetResponse> future = getPoints(timeout).facet(request);
2834+
addLogFailureCallback(future, "Facet");
2835+
return Futures.transform(
2836+
future, Points.FacetResponse::getHitsList, MoreExecutors.directExecutor());
2837+
}
2838+
28082839
// region distance matrix
28092840

28102841
/**

src/test/java/io/qdrant/client/PointsTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,44 @@ public void searchMatrixPairs() throws ExecutionException, InterruptedException
849849
assertEquals(2, pairs.getPairsCount());
850850
}
851851

852+
@Test
853+
public void facets() throws ExecutionException, InterruptedException {
854+
createAndSeedCollection(testName);
855+
856+
// create payload index for "foo" field
857+
UpdateResult result =
858+
client
859+
.createPayloadIndexAsync(
860+
testName, "foo", PayloadSchemaType.Keyword, null, null, null, null)
861+
.get();
862+
863+
assertEquals(UpdateStatus.Completed, result.getStatus());
864+
865+
List<Points.FacetHit> facets =
866+
client
867+
.facetAsync(
868+
Points.FacetCounts.newBuilder()
869+
.setCollectionName(testName)
870+
.setKey("foo")
871+
.setLimit(2)
872+
.build())
873+
.get();
874+
875+
// Number of facets matches the limit
876+
assertEquals(2, facets.size());
877+
// validate hits
878+
assertEquals(
879+
1,
880+
facets.stream()
881+
.filter(f -> f.getValue().getStringValue().equals("hello") && f.getCount() == 1)
882+
.count());
883+
assertEquals(
884+
1,
885+
facets.stream()
886+
.filter(f -> f.getValue().getStringValue().equals("goodbye") && f.getCount() == 1)
887+
.count());
888+
}
889+
852890
private void createAndSeedCollection(String collectionName)
853891
throws ExecutionException, InterruptedException {
854892
CreateCollection request =

0 commit comments

Comments
 (0)