Java library for the Qdrant vector search engine.
Java client library with handy utility methods and overloads for interfacing with Qdrant.
Important
Requires Java 8 or above.
To install the library, add the following lines to your build config file.
<dependency>
<groupId>io.qdrant</groupId>
<artifactId>client</artifactId>
<version>1.7.0</version>
</dependency>
libraryDependencies += "io.qdrant" % "client" % "1.7.0"
implementation 'io.qdrant:client:1.7.0'
A client can be instantiated with
QdrantClient client =
new QdrantClient(QdrantGrpcClient.newBuilder("localhost").build());
which creates a client that will connect to Qdrant on https://localhost:6334.
Internally, the high level client uses a low level gRPC client to interact with Qdrant. Additional constructor overloads provide more control over how the gRPC client is configured. The following example configures a client to use TLS, validating the certificate using the root CA to verify the server's identity instead of the system's default, and also configures API key authentication:
ManagedChannel channel = Grpc.newChannelBuilder(
"localhost:6334",
TlsChannelCredentials.newBuilder()
.trustManager(new File("ssl/ca.crt"))
.build())
.build();
QdrantClient client = new QdrantClient(
QdrantGrpcClient.newBuilder(channel)
.withApiKey("<apikey>")
.build());
The client implements AutoCloseable
,
though a client will typically be created once and used for the lifetime of the
application. When a client is constructed by passing a ManagedChannel
, the
client does not shut down the channel on close by default. The client can be
configured to shut down the channel on close with
ManagedChannel channel = Grpc.newChannelBuilder(
"localhost:6334",
TlsChannelCredentials.create())
.build();
QdrantClient client = new QdrantClient(
QdrantGrpcClient.newBuilder(channel, true)
.withApiKey("<apikey>")
.build());
All client methods return ListenableFuture<T>
.
Once a client has been created, create a new collection
client.createCollectionAsync("my_collection",
VectorParams.newBuilder()
.setDistance(Distance.Cosine)
.setSize(4)
.build())
.get();
Insert vectors into a collection
// import static convenience methods
import static io.qdrant.client.PointIdFactory.id;
import static io.qdrant.client.ValueFactory.value;
import static io.qdrant.client.VectorsFactory.vector;
Random random = new Random();
List<PointStruct> points = IntStream.range(1, 101)
.mapToObj(i -> PointStruct.newBuilder()
.setId(id(i))
.setVectors(vector(IntStream.range(1, 101)
.mapToObj(v -> random.nextFloat())
.collect(Collectors.toList())))
.putAllPayload(ImmutableMap.of(
"color", value("red"),
"rand_number", value(i % 10))
)
.build()
)
.collect(Collectors.toList());
UpdateResult updateResult = client.upsertAsync("my_collection", points).get();
Search for similar vectors
List<Float> queryVector = IntStream.range(1, 101)
.mapToObj(v -> random.nextFloat())
.collect(Collectors.toList());
List<ScoredPoint> points = client.searchAsync(SearchPoints.newBuilder()
.setCollectionName("my_collection")
.addAllVector(queryVector)
.setLimit(5)
.build()
).get();
Search for similar vectors with filtering condition
// import static convenience methods
import static io.qdrant.client.ConditionFactory.range;
List<ScoredPoint> points = client.searchAsync(SearchPoints.newBuilder()
.setCollectionName("my_collection")
.addAllVector(queryVector)
.setFilter(Filter.newBuilder()
.addMust(range("rand_number", Range.newBuilder().setGte(3).build()))
.build())
.setLimit(5)
.build()
).get();
Apache 2.0 © 2023