郭卫东 发表于 2025-3-25 12:11:58

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 tagswr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/langgenius/qdrant:v1.7.3docker.io/langgenius/qdrant:v1.7.3 2.运行Qdrant
docker run -p 6333:6333 -p 6334:6334swr.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);

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Java连接Qdrant数据库