🔥🔥🔥 MilvusPlus (short for MP) is an operational tool for Milvus, designed to simplify interactions with the Milvus vector database, providing developers with an intuitive API similar to MyBatis-Plus annotations and method call style, born to improve efficiency.
Custom extension support:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>milvus-plus-core</artifactId>
<version>2.1.3</version>
</dependency>
Spring application support:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>milvus-plus-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
Solon application support:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>milvus-plus-solon-plugin</artifactId>
<version>2.1.3</version>
</dependency>
milvus:
uri: https://in03-a5357975ab80da7.api.gcp-us-west1.zillizcloud.com
token: x'x'x'x
enable: true
packages:
- com.example.entity
milvus
: Defines configurations related to the Milvus service.
uri
: The URI of the Milvus service, through which the application communicates with the Milvus service.token
: A token for verification and authorization, ensuring the security of access to the Milvus service.enable
: A boolean value indicating whether the Milvus module should be enabled.packages
: These packages contain Java classes corresponding to custom annotations, which you can consider as the package where your custom entity classes are located.Using custom annotations to automate the integration of the Milvus database provides the following significant advantages:
key()
: The key name of the parameter.value()
: The value of the parameter.name()
: The name of the collection.name()
: The field name, defaulting to the Java field name.dataType()
: The data type, defaulting to FLOAT_VECTOR
.dimension()
: The vector dimension, defaulting to -1.isPrimaryKey()
: Whether it is the primary key, defaulting to false.autoID()
: Whether to automatically generate an ID, defaulting to false.description()
: The field description, defaulting to empty.elementType()
: The element type, defaulting to None
.maxLength()
: The maximum length, defaulting to -1.maxCapacity()
: The maximum capacity, defaulting to -1.isPartitionKey()
: Whether it is a partition key, defaulting to false.indexType()
: The index type, defaulting to FLAT
.metricType()
: The metric type, defaulting to L2
.indexName()
: The index name, defaulting to empty.extraParams()
: Additional parameters, defined using the ExtraParam
annotation.name()
: An array of partition names.Through these annotations, developers can easily define and manage the structure of the Milvus database, achieving the goal of automatically building the required database structure when the project starts.
MilvusMapper
is a general-purpose interface for operating the Milvus database, providing a series of data manipulation methods, including querying, deleting, updating, and inserting. The following is a functional description of MilvusMapper
and its related classes:
MilvusMapper
is a generic abstract class that inherits from BaseMilvusMapper
, providing basic methods for interacting with the Milvus client.
getClient()
- Returns a MilvusClientV2
instance.BaseMilvusMapper
is an abstract class that defines basic operations for interacting with the Milvus database.
queryWrapper()
- Creates a LambdaQueryWrapper
instance.deleteWrapper()
- Creates a LambdaDeleteWrapper
instance.updateWrapper()
- Creates a LambdaUpdateWrapper
instance.insertWrapper()
- Creates a LambdaInsertWrapper
instance.Get Data by ID: getById(Serializable ... ids)
ids
- A list of serializable IDs.MilvusResp<List<MilvusResult<T>>>
- The response containing the query results.Delete Data: removeById(Serializable ... ids)
ids
- A list of serializable IDs.MilvusResp<DeleteResp>
- The response of the deletion operation.Update Data: updateById(T ... entity)
entity
- A list of entity objects.MilvusResp<UpsertResp>
- The response of the update operation.Insert Data: insert(T ... entity)
entity
- A list of entity objects.MilvusResp<InsertResp>
- The response of the insertion operation.lambda(Wrapper<W, T> wrapper)
- Initializes and returns a builder instance.LambdaQueryWrapper<T>
is a builder class used to construct and execute Milvus search queries. It provides a series of methods to set query parameters and ultimately execute the query.
searchParams(Map<String, Object> searchParams): Sets search parameters.
The following are the parameters supported by searchParams and their descriptions:
Map<String, Object> searchParams = new HashMap<>();
searchParams.put("metric_type", "L2");
searchParams.put("radius", 0.5f);
searchParams.put("range_filter", 0.3f);
MilvusResp
object containing the query results.The LambdaQueryWrapper<T>
class provides a wealth of methods to build complex search queries, supporting various conditions, logical operations, JSON and array operations, and vector searches. By calling these methods in a chain, users can flexibly construct search requests and obtain the desired query results.
LambdaDeleteWrapper
is a builder class used to construct and execute deletion operations.
partition(String partitionName)
eq(String fieldName, Object value)
ne(String fieldName, Object value)
id(Object id)
remove()
- Builds and executes the deletion request.removeById(Serializable ... ids)
LambdaUpdateWrapper
is a builder class used to construct and execute update operations.
Add Partition: partition(String partitionName)
execute insertion operations.
Add Partition: partition(String partitionName)
Add Field Value: put(String fieldName, Object value)
insert()
- Builds and executes the insertion request.insert(T ... t)
MilvusService
is a comprehensive service that provides full management of the Milvus database. It implements multiple interfaces: IAMService
(Identity and Access Management Service), ICMService
(Collection Management Service), and IVecMService
(Vector Management Service).
The IAMService
interface provides functions for creating, deleting, querying users and roles, as well as granting and revoking permissions.
createRole(String roleName)
createUser(String userName, String password)
describeRole(String roleName)
describeUser(String userName)
dropRole(String roleName)
dropUser(String userName)
grantPrivilege(String roleName, String objectType, String privilege, String objectName)
grantRole(String roleName, String userName)
listRoles()
listUsers()
revokePrivilege(String roleName, String objectType, String privilege, String objectName, String databaseName)
revokeRole(String roleName, String userName)
updatePassword(String userName, String password, String newPassword)
The ICMService
interface provides functions for creating, deleting, querying, renaming, and managing indexes of collections.
createCollection(MilvusEntity milvusEntity)
addField(String collectionName, AddFieldReq ... addFieldReq)
getField(String collectionName, String fieldName)
describeCollection(String collectionName)
dropCollection(String collectionName)
hasCollection(String collectionName)
getCollectionStats(String collectionName)
renameCollection(String oldCollectionName, String newCollectionName)
createIndex(String collectionName, List<IndexParam> indexParams)
describeIndex(String collectionName, String fieldName)
dropIndex(String collectionName, String fieldName)
getLoadState(String collectionName, String partitionName)
loadCollection(String collectionName)
releaseCollection(String collectionName)
createPartition(String collectionName, String partitionName)
dropPartition(String collectionName, String partitionName)
hasPartition(String collectionName, String partitionName)
listPartitions(String collectionName)
loadPartitions(String collectionName, List<String> partitionNames)
releasePartitions(String collectionName, List<String> partitionNames)
The IVecMService
interface provides functions for inserting, updating, querying, deleting vectors, and performing similarity searches.
delete(String collectionName, String partitionName, String filter, List<Object> ids)
get(String collectionName, String partitionName, List<Object> ids, List<String> outputFields)
insert(String collectionName, List<JSONObject> data, String partitionName)
query(String collectionName, List<String> partitionNames, List<String> outputFields, List<Object> ids, String filter, ConsistencyLevel consistencyLevel, long offset, long limit)
search(String collectionName, List<String> partitionNames, String annsField, int topK, String filter, List<String> outputFields, List<Object> data, long offset, long limit, int roundDecimal, Map<String, Object> searchParams, long guaranteeTimestamp, long gracefulTime, ConsistencyLevel consistencyLevel, boolean ignoreGrowing)
upsert(String collectionName, String partitionName, List<JSONObject> data)
In addition to the above functionalities, MilvusService
also provides a public method to obtain a MilvusClientV2
instance:
getClient()
Here is an example of using MilvusPlus for vector search:
Example usage:
@Data
@MilvusCollection(name = "face_collection") // Specifies the name of the Milvus collection
public class Face {
@MilvusField(
name = "person_id", // Field Name
dataType = DataType.Int64, // Data type is 64-bit integer
isPrimaryKey = true // Mark as Primary Key
)
private Long personId; // Unique identifier of the person
@MilvusField(
name = "face_vector", // Field Name
dataType = DataType.FloatVector, // The data type is a floating point vector
dimension = 128 // Vector dimension, assuming that the dimension of the face feature vector is 128
)
@MilvusIndex(
indexType = IndexParam.IndexType.IVF_FLAT, // Using the IVF FLAT index type
metricType = IndexParam.MetricType.L2, // Using the L 2 Distance Metric Type
indexName = "face_index", // Index Name
extraParams = { // Specify additional index parameters
@ExtraParam(key = "nlist", value = "100") // For example, the nlist parameter for IVF
}
)
private List<Float> faceVector; // Storing vectors of face features
}
@Component
public class FaceMilvusMapper extends MilvusMapper<Face> {
}
@Component
@Slf4j
public class ApplicationRunnerTest implements ApplicationRunner {
private final FaceMilvusMapper mapper;
public ApplicationRunnerTest(FaceMilvusMapper mapper) {
this.mapper = mapper;
}
@Override
public void run(ApplicationArguments args){
Face face=new Face();
List<Float> vector = new ArrayList<>();
for (int i = 0; i < 128; i++) {
vector.add((float) (Math.random() * 100)); // Using random numbers here as an example only
}
face.setPersonId(1l);
face.setFaceVector(vector);
// add
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)); // Using random numbers here as an example only
}
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 query
MilvusResp<List<MilvusResult<Face>>> query = mapper.getById(9l);
log.info("query--getById---{}", JSONObject.toJSONString(query));
// VECTOR QUERY
MilvusResp<List<MilvusResult<Face>>> query1 = mapper.queryWrapper()
.vector(Face::getFaceVector, vector)
.ne(Face::getPersonId, 1L)
.topK(3)
.query();
log.info("VectorQuery query--queryWrapper---{}", JSONObject.toJSONString(query1));
// SCALAR QUERY
MilvusResp<List<MilvusResult<Face>>> query2 = mapper.queryWrapper()
.eq(Face::getPersonId, 2L)
.limit(3)
.query();
log.info("ScalarQuery query--queryWrapper---{}", JSONObject.toJSONString(query2));
// update
vector.clear();
for (int i = 0; i < 128; i++) {
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));
// id Query
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));
// query
MilvusResp<List<MilvusResult<Face>>> query4 = mapper.getById(1L);log.info("query--{}", JSONObject.toJSONString(query4));
}
}
Contributions are welcome!
MilvusPlus is open source and available under the License.
For questions or support, reach out to javpower@163.com.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。