添加可用测试数据例子

This commit is contained in:
xgc 2024-05-12 02:40:39 +08:00
parent 8f59cd7d59
commit 4d0f0911e9
3 changed files with 37 additions and 36 deletions

View File

@ -1,56 +1,56 @@
package io.github.javpower.milvus.demo;
import com.google.common.collect.Lists;
import com.alibaba.fastjson.JSONObject;
import io.github.javpower.milvus.demo.model.Face;
import io.github.javpower.milvus.demo.test.FaceMilvusMapper;
import io.github.javpower.milvus.plus.model.vo.MilvusResp;
import io.github.javpower.milvus.plus.model.vo.MilvusResultVo;
import io.milvus.v2.service.vector.response.DeleteResp;
import io.milvus.v2.service.vector.response.InsertResp;
import io.milvus.v2.service.vector.response.UpsertResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class ApplicationRunnerTest implements ApplicationRunner {
@Autowired
private FaceMilvusMapper mapper;
@Override
public void run(ApplicationArguments args) throws Exception {
List<Float> vector = Lists.newArrayList(0.1f,0.2f,0.3f);
Face face=new Face();
List<Float> vector = new ArrayList<>();
// 假设我们有一些浮点数值来填充向量实际应用中这里应该是人脸特征提取算法得到的数值
for (int i = 0; i < 128; i++) {
vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
}
face.setFaceVector(vector);
face.setPersonId(1l);
//新增
MilvusResp<InsertResp> insert = mapper.insert(face);
log.info("insert--{}", JSONObject.toJSONString(insert));
//查询
MilvusResp<List<Face>> query = mapper.getById(1l);
log.info("query--{}", JSONObject.toJSONString(query));
//更新
vector.clear();
for (int i = 0; i < 128; i++) {
vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
}
MilvusResp<UpsertResp> update = mapper.updateById(face);
log.info("update--{}", JSONObject.toJSONString(update));
//删除
MilvusResp<DeleteResp> remove = mapper.removeById(1l);
log.info("remove--{}", JSONObject.toJSONString(remove));
//查询
MilvusResp<MilvusResultVo<Face>> query = mapper.searchWrapper()
.eq(Face::getPersonId, 1l)
.vector(Face::getFaceVector,vector)
.limit(100l)
.query();
MilvusResp<List<Face>> query2 = mapper.getById(1l);
log.info("query--{}", JSONObject.toJSONString(query2));
//删除
MilvusResp<DeleteResp> remove= mapper.deleteWrapper()
.eq(Face::getPersonId, 1l)
.id(111)
.remove();
MilvusResp<DeleteResp> remove2 = mapper.removeById(1l);
//更新
Face face=new Face();
face.setFaceVector(vector);
MilvusResp<UpsertResp> update = mapper.updateWrapper()
.eq(Face::getPersonId, 1l)
.update(face);
face.setPersonId(1l);
MilvusResp<UpsertResp> update2 = mapper.updateById(face);
//新增
MilvusResp<InsertResp> insert = mapper.insertWrapper()
.put(Face::getFaceVector, vector)
.insert();
MilvusResp<InsertResp> insert2 = mapper.insert(face);
}
}

View File

@ -11,21 +11,20 @@ import lombok.Data;
import java.util.List;
@Data
@MilvusCollection(name = "face_collection") // 指定Milvus集合的名称
@MilvusCollection(name = "face_collection_3") // 指定Milvus集合的名称
public class Face {
@MilvusField(
name = "person_id", // 字段名称
dataType = DataType.Int64, // 数据类型为64位整数
isPrimaryKey = true, // 标记为主键
autoID = true // 假设这个ID是自动生成的
autoID = false // 假设这个ID是自动生成的
)
private Long personId; // 人员的唯一标识符
@MilvusField(
name = "face_vector", // 字段名称
dataType = DataType.FloatVector, // 数据类型为浮点型向量
dimension = 128, // 向量维度假设人脸特征向量的维度是128
isPartitionKey = false // 假设这个字段不是分区键
dimension = 128 // 向量维度假设人脸特征向量的维度是128
)
@MilvusIndex(
indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型

View File

@ -2,6 +2,8 @@ server:
port: 8131
milvus:
uri: localhost:8999
token: sss
enable: false
uri: http://xxxxx:19530
token: root:xxxx
enable: true
packages:
- io.github.javpower.milvus.demo.model