Skip to content

Commit 403e07f

Browse files
committed
distance matrix API
1 parent e70ea39 commit 403e07f

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ build/
1111
.idea/libraries/
1212
.idea/uiDesigner.xml
1313
.idea/codeStyles/codeStyleConfig.xml
14+
.idea/codeStyles/Project.xml
1415
*.iws
1516
*.iml
1617
*.ipr

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.qdrant.client.grpc.Collections.VectorsConfig;
4141
import io.qdrant.client.grpc.CollectionsGrpc;
4242
import io.qdrant.client.grpc.JsonWithInt.Value;
43+
import io.qdrant.client.grpc.Points;
4344
import io.qdrant.client.grpc.Points.BatchResult;
4445
import io.qdrant.client.grpc.Points.ClearPayloadPoints;
4546
import io.qdrant.client.grpc.Points.CountPoints;
@@ -2804,6 +2805,72 @@ public ListenableFuture<List<PointGroup>> queryGroupsAsync(
28042805
future, response -> response.getResult().getGroupsList(), MoreExecutors.directExecutor());
28052806
}
28062807

2808+
// region distance matrix
2809+
2810+
/**
2811+
* Compute distance matrix for sampled points with a pair based output format.
2812+
*
2813+
* @param request the search matrix pairs request
2814+
* @return a new instance of {@link ListenableFuture}
2815+
*/
2816+
public ListenableFuture<Points.SearchMatrixPairs> searchMatrixPairsAsync(
2817+
Points.SearchMatrixPoints request) {
2818+
return searchMatrixPairsAsync(request, null);
2819+
}
2820+
2821+
/**
2822+
* Compute distance matrix for sampled points with a pair based output format.
2823+
*
2824+
* @param request the search matrix pairs request
2825+
* @param timeout the timeout for the call.
2826+
* @return a new instance of {@link ListenableFuture}
2827+
*/
2828+
public ListenableFuture<Points.SearchMatrixPairs> searchMatrixPairsAsync(
2829+
Points.SearchMatrixPoints request, @Nullable Duration timeout) {
2830+
Preconditions.checkArgument(
2831+
!request.getCollectionName().isEmpty(), "Collection name must not be empty");
2832+
2833+
logger.debug("Search matrix pairs on '{}'", request.getCollectionName());
2834+
ListenableFuture<Points.SearchMatrixPairsResponse> future =
2835+
getPoints(timeout).searchMatrixPairs(request);
2836+
addLogFailureCallback(future, "Search matrix pairs");
2837+
return Futures.transform(
2838+
future, Points.SearchMatrixPairsResponse::getResult, MoreExecutors.directExecutor());
2839+
}
2840+
2841+
/**
2842+
* Compute distance matrix for sampled points with an offset based output format
2843+
*
2844+
* @param request the search matrix pairs request
2845+
* @return a new instance of {@link ListenableFuture}
2846+
*/
2847+
public ListenableFuture<Points.SearchMatrixOffsets> searchMatrixOffsetsAsync(
2848+
Points.SearchMatrixPoints request) {
2849+
return searchMatrixOffsetsAsync(request, null);
2850+
}
2851+
2852+
/**
2853+
* Compute distance matrix for sampled points with an offset based output format
2854+
*
2855+
* @param request the search matrix pairs request
2856+
* @param timeout the timeout for the call.
2857+
* @return a new instance of {@link ListenableFuture}
2858+
*/
2859+
public ListenableFuture<Points.SearchMatrixOffsets> searchMatrixOffsetsAsync(
2860+
Points.SearchMatrixPoints request, @Nullable Duration timeout) {
2861+
Preconditions.checkArgument(
2862+
!request.getCollectionName().isEmpty(), "Collection name must not be empty");
2863+
2864+
logger.debug("Search matrix offsets on '{}'", request.getCollectionName());
2865+
ListenableFuture<Points.SearchMatrixOffsetsResponse> future =
2866+
getPoints(timeout).searchMatrixOffsets(request);
2867+
addLogFailureCallback(future, "Search matrix offsets");
2868+
return Futures.transform(
2869+
future, Points.SearchMatrixOffsetsResponse::getResult, MoreExecutors.directExecutor());
2870+
}
2871+
2872+
// endregion
2873+
28072874
// region Snapshot Management
28082875

28092876
/**

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,42 @@ public void queryGroups() throws ExecutionException, InterruptedException {
813813
assertEquals(1, groups.stream().filter(g -> g.getHitsCount() == 1).count());
814814
}
815815

816+
@Test
817+
public void searchMatrixOffsets() throws ExecutionException, InterruptedException {
818+
createAndSeedCollection(testName);
819+
820+
Points.SearchMatrixOffsets offsets =
821+
client
822+
.searchMatrixOffsetsAsync(
823+
Points.SearchMatrixPoints.newBuilder()
824+
.setCollectionName(testName)
825+
.setSample(3)
826+
.setLimit(2)
827+
.build())
828+
.get();
829+
830+
// Number of ids matches the limit
831+
assertEquals(2, offsets.getIdsCount());
832+
}
833+
834+
@Test
835+
public void searchMatrixPairs() throws ExecutionException, InterruptedException {
836+
createAndSeedCollection(testName);
837+
838+
Points.SearchMatrixPairs pairs =
839+
client
840+
.searchMatrixPairsAsync(
841+
Points.SearchMatrixPoints.newBuilder()
842+
.setCollectionName(testName)
843+
.setSample(3)
844+
.setLimit(2)
845+
.build())
846+
.get();
847+
848+
// Number of ids matches the limit
849+
assertEquals(2, pairs.getPairsCount());
850+
}
851+
816852
private void createAndSeedCollection(String collectionName)
817853
throws ExecutionException, InterruptedException {
818854
CreateCollection request =

0 commit comments

Comments
 (0)