Skip to content

Commit

Permalink
add support for facets
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Oct 4, 2024
1 parent 403e07f commit e7262ff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/io/qdrant/client/QdrantClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,37 @@ public ListenableFuture<List<PointGroup>> queryGroupsAsync(
future, response -> response.getResult().getGroupsList(), MoreExecutors.directExecutor());
}

/**
* Perform facet counts. For each value in the field, count the number of points that have this
* value and match the conditions.
*
* @param request the facet counts request
* @return a new instance of {@link ListenableFuture}
*/
public ListenableFuture<List<Points.FacetHit>> facetAsync(Points.FacetCounts request) {
return facetAsync(request, null);
}

/**
* Perform facet counts. For each value in the field, count the number of points that have this
* value and match the conditions.
*
* @param request the facet counts request
* @param timeout the timeout for the call.
* @return a new instance of {@link ListenableFuture}
*/
public ListenableFuture<List<Points.FacetHit>> facetAsync(
Points.FacetCounts request, @Nullable Duration timeout) {
Preconditions.checkArgument(
!request.getCollectionName().isEmpty(), "Collection name must not be empty");

logger.debug("Facet on '{}'", request.getCollectionName());
ListenableFuture<Points.FacetResponse> future = getPoints(timeout).facet(request);
addLogFailureCallback(future, "Facet");
return Futures.transform(
future, Points.FacetResponse::getHitsList, MoreExecutors.directExecutor());
}

// region distance matrix

/**
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/io/qdrant/client/PointsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,44 @@ public void searchMatrixPairs() throws ExecutionException, InterruptedException
assertEquals(2, pairs.getPairsCount());
}

@Test
public void facets() throws ExecutionException, InterruptedException {
createAndSeedCollection(testName);

// create payload index for "foo" field
UpdateResult result =
client
.createPayloadIndexAsync(
testName, "foo", PayloadSchemaType.Keyword, null, null, null, null)
.get();

assertEquals(UpdateStatus.Completed, result.getStatus());

List<Points.FacetHit> facets =
client
.facetAsync(
Points.FacetCounts.newBuilder()
.setCollectionName(testName)
.setKey("foo")
.setLimit(2)
.build())
.get();

// Number of facets matches the limit
assertEquals(2, facets.size());
// validate hits
assertEquals(
1,
facets.stream()
.filter(f -> f.getValue().getStringValue().equals("hello") && f.getCount() == 1)
.count());
assertEquals(
1,
facets.stream()
.filter(f -> f.getValue().getStringValue().equals("goodbye") && f.getCount() == 1)
.count());
}

private void createAndSeedCollection(String collectionName)
throws ExecutionException, InterruptedException {
CreateCollection request =
Expand Down

0 comments on commit e7262ff

Please sign in to comment.