Java连接Qdrant数据库

1.下载Qdrant

在虚拟机上使用Docker下载

链接:docker.io/langgenius/qdrant:v1.7.3 - 镜像下载 | docker.io (aityp.com)

docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/langgenius/qdrant:v1.7.3
docker tag  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/langgenius/qdrant:v1.7.3  docker.io/langgenius/qdrant:v1.7.3

2.运行Qdrant

docker run -p 6333:6333 -p 6334:6334  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/langgenius/qdrant:v1.7.3

3.添加Maven依赖

<dependency>
    <groupId>io.qdrant</groupId>
    <artifactId>client</artifactId>
    <version>1.10.0</version>
</dependency>

4.Java操作Qdrant

官方文档:Local Quickstart - Qdrant

    // 创建客户端链接
    QdrantClient client = new QdrantClient(
            QdrantGrpcClient.newBuilder("192.168.40.130", 6334, false).build());
    // 创建collection
    client.createCollectionAsync("my_collection",
                    Collections.VectorParams.newBuilder()
                            .setDistance(Collections.Distance.Cosine)
                            .setSize(4)
                            .build()).get();
    // 插入向量
    HashMap<String, JsonWithInt.Value> map1 = new HashMap<>();
    map1.put("color", value("red"));
    map1.put("rand_number", value(32));
    HashMap<String, JsonWithInt.Value> map2 = new HashMap<>();
    map2.put("color", value("blue"));
    map2.put("rand_number", value(53));
    map2.put("extra_field", value(true));
    List<Points.PointStruct> points = new ArrayList<>();
    points.add(Points.PointStruct.newBuilder()
            .setId(id(1))
            .setVectors(vectors(0.32f, 0.52f, 0.21f, 0.52f))
            .putAllPayload(map1)
            .build());
    points.add(Points.PointStruct.newBuilder()
            .setId(id(2))
            .setVectors(vectors(0.42f, 0.52f, 0.67f, 0.632f))
            .putAllPayload(map2)
            .build());
    Points.UpdateResult updateResult = client.upsertAsync("my_collection", points).get();
    // 搜索相似结果
    List<Points.ScoredPoint> points1 = client.searchAsync(
            Points.SearchPoints.newBuilder()
                    .setCollectionName("my_collection")
                    .addAllVector(Arrays.asList(0.6235f, 0.123f, 0.532f, 0.123f))
                    .setLimit(5)
                    .build()).get();
    System.out.println(points1);
    // 搜索相似结果带过滤条件
    List<Points.ScoredPoint> points2 = client.searchAsync(
            Points.SearchPoints.newBuilder()
                    .setCollectionName("my_collection")
                    .addAllVector(Arrays.asList(0.6235f, 0.123f, 0.532f, 0.123f))
                    .setFilter(Points.Filter.newBuilder()
                            .addMust(range("rand_number", Points.Range.newBuilder().setGte(3).build()))
                            .build())
                    .setLimit(5)
                    .build()).get();
    System.out.println(points2);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值