mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-07 01:18:23 +08:00
添加可用测试数据例子
This commit is contained in:
parent
8f59cd7d59
commit
4d0f0911e9
@ -1,56 +1,56 @@
|
|||||||
package io.github.javpower.milvus.demo;
|
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.model.Face;
|
||||||
import io.github.javpower.milvus.demo.test.FaceMilvusMapper;
|
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.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.DeleteResp;
|
||||||
import io.milvus.v2.service.vector.response.InsertResp;
|
import io.milvus.v2.service.vector.response.InsertResp;
|
||||||
import io.milvus.v2.service.vector.response.UpsertResp;
|
import io.milvus.v2.service.vector.response.UpsertResp;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@Slf4j
|
||||||
public class ApplicationRunnerTest implements ApplicationRunner {
|
public class ApplicationRunnerTest implements ApplicationRunner {
|
||||||
@Autowired
|
@Autowired
|
||||||
private FaceMilvusMapper mapper;
|
private FaceMilvusMapper mapper;
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
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);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -11,21 +11,20 @@ import lombok.Data;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@MilvusCollection(name = "face_collection") // 指定Milvus集合的名称
|
@MilvusCollection(name = "face_collection_3") // 指定Milvus集合的名称
|
||||||
public class Face {
|
public class Face {
|
||||||
@MilvusField(
|
@MilvusField(
|
||||||
name = "person_id", // 字段名称
|
name = "person_id", // 字段名称
|
||||||
dataType = DataType.Int64, // 数据类型为64位整数
|
dataType = DataType.Int64, // 数据类型为64位整数
|
||||||
isPrimaryKey = true, // 标记为主键
|
isPrimaryKey = true, // 标记为主键
|
||||||
autoID = true // 假设这个ID是自动生成的
|
autoID = false // 假设这个ID是自动生成的
|
||||||
)
|
)
|
||||||
private Long personId; // 人员的唯一标识符
|
private Long personId; // 人员的唯一标识符
|
||||||
|
|
||||||
@MilvusField(
|
@MilvusField(
|
||||||
name = "face_vector", // 字段名称
|
name = "face_vector", // 字段名称
|
||||||
dataType = DataType.FloatVector, // 数据类型为浮点型向量
|
dataType = DataType.FloatVector, // 数据类型为浮点型向量
|
||||||
dimension = 128, // 向量维度,假设人脸特征向量的维度是128
|
dimension = 128 // 向量维度,假设人脸特征向量的维度是128
|
||||||
isPartitionKey = false // 假设这个字段不是分区键
|
|
||||||
)
|
)
|
||||||
@MilvusIndex(
|
@MilvusIndex(
|
||||||
indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型
|
indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型
|
||||||
|
|||||||
@ -2,6 +2,8 @@ server:
|
|||||||
port: 8131
|
port: 8131
|
||||||
|
|
||||||
milvus:
|
milvus:
|
||||||
uri: localhost:8999
|
uri: http://xxxxx:19530
|
||||||
token: sss
|
token: root:xxxx
|
||||||
enable: false
|
enable: true
|
||||||
|
packages:
|
||||||
|
- io.github.javpower.milvus.demo.model
|
||||||
Loading…
x
Reference in New Issue
Block a user