修改readme一些解释,增加dbName等字段配置

This commit is contained in:
ming 2024-05-29 17:10:56 +08:00
parent 390504e903
commit c3b95546a4
6 changed files with 52 additions and 32 deletions

View File

@ -66,14 +66,14 @@ milvus:
token: x'x'x'x token: x'x'x'x
enable: true enable: true
packages: packages:
- io.github.javpower.milvus.demo.model - com.example.entity
``` ```
- `milvus`定义了与Milvus服务相关的配置。 - `milvus`定义了与Milvus服务相关的配置。
- `uri`Milvus服务的URI应用程序通过这个URI与Milvus服务进行通信。 - `uri`Milvus服务的URI应用程序通过这个URI与Milvus服务进行通信。
- `token`用于验证和授权的令牌Token确保访问Milvus服务的安全性。 - `token`用于验证和授权的令牌Token确保访问Milvus服务的安全性。
- `enable`一个布尔值用于指示Milvus模块是否应该被启用。 - `enable`一个布尔值用于指示Milvus模块是否应该被启用。
- `packages`这些包包含了自定义注解对应的Java类。 - `packages`这些包包含了自定义注解对应的Java类,你可以认为这是你自定义的实体类所在的包
## 应用场景 ## 应用场景

View File

@ -29,7 +29,7 @@ Core
<dependency> <dependency>
<groupId>io.github.javpower</groupId> <groupId>io.github.javpower</groupId>
<artifactId>milvus-plus-core</artifactId> <artifactId>milvus-plus-core</artifactId>
<version>2.4.0-SNAPSHOT</version> <version>2.4.0</version>
</dependency> </dependency>
``` ```
@ -40,7 +40,7 @@ Spring
<dependency> <dependency>
<groupId>io.github.javpower</groupId> <groupId>io.github.javpower</groupId>
<artifactId>milvus-plus-boot-starter</artifactId> <artifactId>milvus-plus-boot-starter</artifactId>
<version>2.4.0-SNAPSHOT</version> <version>2.4.0</version>
</dependency> </dependency>
``` ```
@ -50,7 +50,7 @@ Solon
<dependency> <dependency>
<groupId>io.github.javpower</groupId> <groupId>io.github.javpower</groupId>
<artifactId>milvus-plus-solon-plugin</artifactId> <artifactId>milvus-plus-solon-plugin</artifactId>
<version>2.4.0-SNAPSHOT</version> <version>2.4.0</version>
</dependency> </dependency>
``` ```
@ -79,29 +79,29 @@ Example usage:
```java ```java
@Data @Data
@MilvusCollection(name = "face_collection") // 指定Milvus集合的名称 @MilvusCollection(name = "face_collection") // Specifies the name of the Milvus collection
public class Face { public class Face {
@MilvusField( @MilvusField(
name = "person_id", // 字段名称 name = "person_id", // Field Name
dataType = DataType.Int64, // 数据类型为64位整数 dataType = DataType.Int64, // Data type is 64-bit integer
isPrimaryKey = true, // 标记为主键 isPrimaryKey = true // Mark as Primary Key
) )
private Long personId; // 人员的唯一标识符 private Long personId; // Unique identifier of the person
@MilvusField( @MilvusField(
name = "face_vector", // 字段名称 name = "face_vector", // Field Name
dataType = DataType.FloatVector, // 数据类型为浮点型向量 dataType = DataType.FloatVector, // The data type is a floating point vector
dimension = 128, // 向量维度,假设人脸特征向量的维度是128 dimension = 128 // Vector dimension, assuming that the dimension of the face feature vector is 128
) )
@MilvusIndex( @MilvusIndex(
indexType = IndexParam.IndexType.IVF_FLAT, // 使用IVF_FLAT索引类型 indexType = IndexParam.IndexType.IVF_FLAT, // Using the IVF FLAT index type
metricType = IndexParam.MetricType.L2, // 使用L2距离度量类型 metricType = IndexParam.MetricType.L2, // Using the L 2 Distance Metric Type
indexName = "face_index", // 索引名称 indexName = "face_index", // Index Name
extraParams = { // 指定额外的索引参数 extraParams = { // Specify additional index parameters
@ExtraParam(key = "nlist", value = "100") // 例如IVF的nlist参数 @ExtraParam(key = "nlist", value = "100") // For example, the nlist parameter for IVF
} }
) )
private List<Float> faceVector; // 存储人脸特征的向量 private List<Float> faceVector; // Storing vectors of face features
} }
``` ```
``` ```
@ -124,50 +124,58 @@ public class ApplicationRunnerTest implements ApplicationRunner {
Face face=new Face(); Face face=new Face();
List<Float> vector = new ArrayList<>(); List<Float> vector = new ArrayList<>();
for (int i = 0; i < 128; i++) { for (int i = 0; i < 128; i++) {
vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数 vector.add((float) (Math.random() * 100)); // Using random numbers here as an example only
} }
face.setPersonId(1l); face.setPersonId(1l);
face.setFaceVector(vector); face.setFaceVector(vector);
//新增
// add
List<Face> faces=new ArrayList<>(); List<Face> faces=new ArrayList<>();
for (int i = 1; i < 10 ;i++){ for (int i = 1; i < 10 ;i++){
Face face1=new Face(); Face face1=new Face();
face1.setPersonId(Long.valueOf(i)); face1.setPersonId(Long.valueOf(i));
List<Float> vector1 = new ArrayList<>(); List<Float> vector1 = new ArrayList<>();
for (int j = 0; j < 128; j++) { for (int j = 0; j < 128; j++) {
vector1.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数 vector1.add((float) (Math.random() * 100)); // Using random numbers here as an example only
} }
face1.setFaceVector(vector1); face1.setFaceVector(vector1);
faces.add(face1); faces.add(face1);
} }
MilvusResp<InsertResp> insert = mapper.insert(faces.toArray(faces.toArray(new Face[0]))); log.info("insert--{}", JSONObject.toJSONString(insert)); MilvusResp<InsertResp> insert = mapper.insert(faces.toArray(faces.toArray(new Face[0]))); log.info("insert--{}", JSONObject.toJSONString(insert));
//id查询
// id query
MilvusResp<List<MilvusResult<Face>>> query = mapper.getById(9l); MilvusResp<List<MilvusResult<Face>>> query = mapper.getById(9l);
log.info("query--getById---{}", JSONObject.toJSONString(query)); log.info("query--getById---{}", JSONObject.toJSONString(query));
//向量查询
// VECTOR QUERY
MilvusResp<List<MilvusResult<Face>>> query1 = mapper.queryWrapper() MilvusResp<List<MilvusResult<Face>>> query1 = mapper.queryWrapper()
.vector(Face::getFaceVector, vector) .vector(Face::getFaceVector, vector)
.ne(Face::getPersonId, 1L) .ne(Face::getPersonId, 1L)
.topK(3) .topK(3)
.query(); .query();
log.info("向量查询 query--queryWrapper---{}", JSONObject.toJSONString(query1)); log.info("VectorQuery query--queryWrapper---{}", JSONObject.toJSONString(query1));
//标量查询
// SCALAR QUERY
MilvusResp<List<MilvusResult<Face>>> query2 = mapper.queryWrapper() MilvusResp<List<MilvusResult<Face>>> query2 = mapper.queryWrapper()
.eq(Face::getPersonId, 2L) .eq(Face::getPersonId, 2L)
.limit(3) .limit(3)
.query(); .query();
log.info("标量查询 query--queryWrapper---{}", JSONObject.toJSONString(query2)); log.info("ScalarQuery query--queryWrapper---{}", JSONObject.toJSONString(query2));
//更新
// update
vector.clear(); vector.clear();
for (int i = 0; i < 128; i++) { for (int i = 0; i < 128; i++) {
vector.add((float) (Math.random() * 100)); // 这里仅作为示例使用随机数 vector.add((float) (Math.random() * 100)); // Using random numbers here as an example only
} }
MilvusResp<UpsertResp> update = mapper.updateById(face);log.info("update--{}", JSONObject.toJSONString(update)); MilvusResp<UpsertResp> update = mapper.updateById(face);log.info("update--{}", JSONObject.toJSONString(update));
//id查询
// id Query
MilvusResp<List<MilvusResult<Face>>> query3 = mapper.getById(1L);log.info("query--getById---{}", JSONObject.toJSONString(query3)); MilvusResp<List<MilvusResult<Face>>> query3 = mapper.getById(1L);log.info("query--getById---{}", JSONObject.toJSONString(query3));
//删除
// del
MilvusResp<DeleteResp> remove = mapper.removeById(1L);log.info("remove--{}", JSONObject.toJSONString(remove)); MilvusResp<DeleteResp> remove = mapper.removeById(1L);log.info("remove--{}", JSONObject.toJSONString(remove));
//查询
// query
MilvusResp<List<MilvusResult<Face>>> query4 = mapper.getById(1L);log.info("query--{}", JSONObject.toJSONString(query4)); MilvusResp<List<MilvusResult<Face>>> query4 = mapper.getById(1L);log.info("query--{}", JSONObject.toJSONString(query4));
} }

View File

@ -15,6 +15,9 @@ import java.util.List;
public class MilvusProperties { public class MilvusProperties {
private boolean enable; private boolean enable;
private String uri; private String uri;
private String dbName;
private String username;
private String password;
private String token; private String token;
private List<String> packages; private List<String> packages;
} }

View File

@ -11,6 +11,9 @@ import java.util.List;
public class MilvusProperties { public class MilvusProperties {
private boolean enable; private boolean enable;
private String uri; private String uri;
private String dbName;
private String username;
private String password;
private String token; private String token;
private List<String> packages; private List<String> packages;
} }

View File

@ -38,6 +38,9 @@ public abstract class AbstractMilvusClientBuilder implements MilvusClientBuilder
ConnectConfig connectConfig = ConnectConfig.builder() ConnectConfig connectConfig = ConnectConfig.builder()
.uri(properties.getUri()) .uri(properties.getUri())
.token(properties.getToken()) .token(properties.getToken())
.dbName(properties.getDbName())
.username(properties.getUsername())
.password(properties.getPassword())
.build(); .build();
client = new MilvusClientV2(connectConfig); client = new MilvusClientV2(connectConfig);
// 初始化逻辑 // 初始化逻辑

View File

@ -15,6 +15,9 @@ import java.util.List;
public class MilvusProperties { public class MilvusProperties {
private boolean enable; private boolean enable;
private String uri; private String uri;
private String dbName;
private String username;
private String password;
private String token; private String token;
private List<String> packages; private List<String> packages;
} }