mirror of
https://gitee.com/dromara/MilvusPlus.git
synced 2025-12-06 17:08:27 +08:00
增加solon支持
This commit is contained in:
parent
01158bd39a
commit
18cbdd9b55
@ -6,7 +6,7 @@
|
|||||||
<artifactId>milvus-plus-core</artifactId>
|
<artifactId>milvus-plus-core</artifactId>
|
||||||
<version>2.4.0-SNAPSHOT</version>
|
<version>2.4.0-SNAPSHOT</version>
|
||||||
<name>milvus-plus-core</name>
|
<name>milvus-plus-core</name>
|
||||||
<description>a tool about milvus-plus</description>
|
<description>a tool about milvus-plus-core</description>
|
||||||
<url>https://github.com/javpower/milvus-plus</url>
|
<url>https://github.com/javpower/milvus-plus</url>
|
||||||
<licenses>
|
<licenses>
|
||||||
<license>
|
<license>
|
||||||
|
|||||||
29
milvus-plus-solon-starter/.gitignore
vendored
Executable file
29
milvus-plus-solon-starter/.gitignore
vendored
Executable file
@ -0,0 +1,29 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
*.log
|
||||||
|
*.flattened-pom.xml
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
nbproject/private/
|
||||||
|
build/
|
||||||
|
nbbuild/
|
||||||
|
dist/
|
||||||
|
nbdist/
|
||||||
|
.nb-gradle/
|
||||||
|
|
||||||
|
### Mac files ###
|
||||||
|
*.DS_Store
|
||||||
70
milvus-plus-solon-starter/pom.xml
Executable file
70
milvus-plus-solon-starter/pom.xml
Executable file
@ -0,0 +1,70 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.noear</groupId>
|
||||||
|
<artifactId>solon-parent</artifactId>
|
||||||
|
<version>2.7.6</version>
|
||||||
|
<relativePath />
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>io.github.javpower</groupId>
|
||||||
|
<artifactId>milvus-plus-solon-starter</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<description>milvus-plus-solon-starter</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.github.javpower</groupId>
|
||||||
|
<artifactId>milvus-plus-core</artifactId>
|
||||||
|
<version>2.4.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.noear</groupId>
|
||||||
|
<artifactId>solon-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.noear</groupId>
|
||||||
|
<artifactId>solon.logging.logback</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.noear</groupId>
|
||||||
|
<artifactId>solon-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.noear</groupId>
|
||||||
|
<artifactId>solon-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>tencent</id>
|
||||||
|
<url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
||||||
11
milvus-plus-solon-starter/src/main/java/io/github/javpower/solon/App.java
Executable file
11
milvus-plus-solon-starter/src/main/java/io/github/javpower/solon/App.java
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
package io.github.javpower.solon;
|
||||||
|
|
||||||
|
import org.noear.solon.Solon;
|
||||||
|
import org.noear.solon.annotation.SolonMain;
|
||||||
|
|
||||||
|
@SolonMain
|
||||||
|
public class App {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solon.start(App.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package io.github.javpower.solon;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import io.github.javpower.milvus.plus.model.vo.MilvusResp;
|
||||||
|
import io.github.javpower.milvus.plus.model.vo.MilvusResult;
|
||||||
|
import io.github.javpower.solon.test.model.Face;
|
||||||
|
import io.github.javpower.solon.test.test.FaceMilvusMapper;
|
||||||
|
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.noear.solon.annotation.Controller;
|
||||||
|
import org.noear.solon.annotation.Get;
|
||||||
|
import org.noear.solon.annotation.Inject;
|
||||||
|
import org.noear.solon.annotation.Mapping;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@Slf4j
|
||||||
|
public class DemoController {
|
||||||
|
@Inject
|
||||||
|
FaceMilvusMapper mapper;
|
||||||
|
@Mapping("/test")
|
||||||
|
@Get
|
||||||
|
public void hello() {
|
||||||
|
Face face=new Face();
|
||||||
|
List<Float> vector = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 128; i++) {
|
||||||
|
vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
|
||||||
|
}
|
||||||
|
face.setPersonId(1l);
|
||||||
|
face.setFaceVector(vector);
|
||||||
|
//新增
|
||||||
|
List<Face> faces=new ArrayList<>();
|
||||||
|
for (int i = 1; i < 10 ;i++){
|
||||||
|
Face face1=new Face();
|
||||||
|
face1.setPersonId(Long.valueOf(i));
|
||||||
|
List<Float> vector1 = new ArrayList<>();
|
||||||
|
for (int j = 0; j < 128; j++) {
|
||||||
|
vector1.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数
|
||||||
|
}
|
||||||
|
face1.setFaceVector(vector1);
|
||||||
|
faces.add(face1);
|
||||||
|
}
|
||||||
|
MilvusResp<InsertResp> insert = mapper.insert(faces.toArray(faces.toArray(new Face[0]))); log.info("insert--{}", JSONObject.toJSONString(insert));
|
||||||
|
//id查询
|
||||||
|
MilvusResp<List<MilvusResult<Face>>> query = mapper.getById(9l);
|
||||||
|
log.info("query--getById---{}", JSONObject.toJSONString(query));
|
||||||
|
//向量查询
|
||||||
|
MilvusResp<List<MilvusResult<Face>>> query1 = mapper.queryWrapper()
|
||||||
|
.vector(Face::getFaceVector, vector)
|
||||||
|
.ne(Face::getPersonId, 1L)
|
||||||
|
.topK(3)
|
||||||
|
.query();
|
||||||
|
log.info("向量查询 query--queryWrapper---{}", JSONObject.toJSONString(query1));
|
||||||
|
//标量查询
|
||||||
|
MilvusResp<List<MilvusResult<Face>>> query2 = mapper.queryWrapper()
|
||||||
|
.eq(Face::getPersonId, 2L)
|
||||||
|
.topK(3)
|
||||||
|
.query();
|
||||||
|
log.info("标量查询 query--queryWrapper---{}", JSONObject.toJSONString(query2));
|
||||||
|
//更新
|
||||||
|
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));
|
||||||
|
//id查询
|
||||||
|
MilvusResp<List<MilvusResult<Face>>> query3 = mapper.getById(1L);log.info("query--getById---{}", JSONObject.toJSONString(query3));
|
||||||
|
//删除
|
||||||
|
MilvusResp<DeleteResp> remove = mapper.removeById(1L);log.info("remove--{}", JSONObject.toJSONString(remove));
|
||||||
|
//查询
|
||||||
|
MilvusResp<List<MilvusResult<Face>>> query4 = mapper.getById(1L);log.info("query--{}", JSONObject.toJSONString(query4));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
package io.github.javpower.solon.entity;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.noear.solon.annotation.Component;
|
||||||
|
import org.noear.solon.annotation.Inject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xgc
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
public class MilvusProperties {
|
||||||
|
@Inject("${milvus.enable}")
|
||||||
|
private boolean enable;
|
||||||
|
@Inject("${milvus.uri}")
|
||||||
|
private String uri;
|
||||||
|
@Inject("${milvus.token}")
|
||||||
|
private String token;
|
||||||
|
@Inject("${milvus.packages}")
|
||||||
|
private List<String> packages;
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package io.github.javpower.solon.mapper;
|
||||||
|
|
||||||
|
import io.github.javpower.milvus.plus.core.mapper.BaseMilvusMapper;
|
||||||
|
import io.github.javpower.solon.service.MilvusInit;
|
||||||
|
import io.milvus.v2.client.MilvusClientV2;
|
||||||
|
|
||||||
|
|
||||||
|
public class MilvusMapper<T> extends BaseMilvusMapper<T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MilvusClientV2 getClient() {
|
||||||
|
return MilvusInit.client;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
package io.github.javpower.solon.service;
|
||||||
|
|
||||||
|
import io.github.javpower.milvus.plus.service.AbstractMilvusClientBuilder;
|
||||||
|
import io.github.javpower.solon.entity.MilvusProperties;
|
||||||
|
import io.milvus.v2.client.MilvusClientV2;
|
||||||
|
import org.noear.solon.annotation.Component;
|
||||||
|
import org.noear.solon.annotation.Init;
|
||||||
|
import org.noear.solon.annotation.Inject;
|
||||||
|
import org.noear.solon.core.bean.LifecycleBean;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MilvusInit extends AbstractMilvusClientBuilder implements LifecycleBean {
|
||||||
|
|
||||||
|
|
||||||
|
public static MilvusClientV2 client;
|
||||||
|
@Inject
|
||||||
|
MilvusProperties milvusProperties;
|
||||||
|
|
||||||
|
// Spring会调用这个方法来初始化client
|
||||||
|
@Init
|
||||||
|
public void initialize() {
|
||||||
|
io.github.javpower.milvus.plus.model.MilvusProperties milvusProperties1=new io.github.javpower.milvus.plus.model.MilvusProperties();
|
||||||
|
BeanUtils.copyProperties(milvusProperties,milvusProperties1);
|
||||||
|
super.setProperties(milvusProperties1);
|
||||||
|
super.initialize();
|
||||||
|
client = getClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() throws Throwable {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() throws Throwable {
|
||||||
|
super.close();
|
||||||
|
LifecycleBean.super.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
package io.github.javpower.solon.test.model;
|
||||||
|
|
||||||
|
import io.github.javpower.milvus.plus.annotation.ExtraParam;
|
||||||
|
import io.github.javpower.milvus.plus.annotation.MilvusCollection;
|
||||||
|
import io.github.javpower.milvus.plus.annotation.MilvusField;
|
||||||
|
import io.github.javpower.milvus.plus.annotation.MilvusIndex;
|
||||||
|
import io.milvus.v2.common.DataType;
|
||||||
|
import io.milvus.v2.common.IndexParam;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@MilvusCollection(name = "face_collection") // 指定Milvus集合的名称
|
||||||
|
public class Face {
|
||||||
|
@MilvusField(
|
||||||
|
name = "person_id", // 字段名称
|
||||||
|
dataType = DataType.Int64, // 数据类型为64位整数
|
||||||
|
isPrimaryKey = true, // 标记为主键
|
||||||
|
autoID = false // 假设这个ID是自动生成的
|
||||||
|
)
|
||||||
|
private Long personId; // 人员的唯一标识符
|
||||||
|
|
||||||
|
@MilvusField(
|
||||||
|
name = "face_vector", // 字段名称
|
||||||
|
dataType = DataType.FloatVector, // 数据类型为浮点型向量
|
||||||
|
dimension = 128 // 向量维度,假设人脸特征向量的维度是128
|
||||||
|
)
|
||||||
|
@MilvusIndex(
|
||||||
|
indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型
|
||||||
|
metricType = IndexParam.MetricType.L2, // 使用L2距离度量类型
|
||||||
|
indexName = "face_index", // 索引名称
|
||||||
|
extraParams = { // 指定额外的索引参数
|
||||||
|
@ExtraParam(key = "nlist", value = "100") // 例如,IVF的nlist参数
|
||||||
|
}
|
||||||
|
)
|
||||||
|
private List<Float> faceVector; // 存储人脸特征的向量
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package io.github.javpower.solon.test.test;
|
||||||
|
|
||||||
|
|
||||||
|
import io.github.javpower.solon.mapper.MilvusMapper;
|
||||||
|
import io.github.javpower.solon.test.model.Face;
|
||||||
|
import org.noear.solon.annotation.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class FaceMilvusMapper extends MilvusMapper<Face> {
|
||||||
|
|
||||||
|
}
|
||||||
13
milvus-plus-solon-starter/src/main/resources/app.yml
Executable file
13
milvus-plus-solon-starter/src/main/resources/app.yml
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
server.port: 8880
|
||||||
|
|
||||||
|
solon.app:
|
||||||
|
name: 'milvus-plus-solon'
|
||||||
|
group: 'milvus'
|
||||||
|
|
||||||
|
|
||||||
|
milvus:
|
||||||
|
uri: https://in03-a5357975ab80da7.api.gcp-us-west1.zillizcloud.com
|
||||||
|
token: xxx
|
||||||
|
enable: true
|
||||||
|
packages:
|
||||||
|
- io.github.javpower.solon.test.model
|
||||||
@ -3,7 +3,7 @@ server:
|
|||||||
|
|
||||||
milvus:
|
milvus:
|
||||||
uri: https://in03-a5357975ab80da7.api.gcp-us-west1.zillizcloud.com
|
uri: https://in03-a5357975ab80da7.api.gcp-us-west1.zillizcloud.com
|
||||||
token: XXXx
|
token: 6fab5641a3156d2666feba14390e4ef4b6d376b5dce91faed303eec91a4bdb82239b70b29eb252b981daa3170516245818d4ee12
|
||||||
enable: true
|
enable: true
|
||||||
packages:
|
packages:
|
||||||
- io.github.javpower.milvus.demo.model
|
- io.github.javpower.milvus.demo.model
|
||||||
Loading…
x
Reference in New Issue
Block a user