< Docs
  • Java


A MilvusClient interface. This method inserts entities into a specified collection.

R<MutationResult> insert(InsertParam requestParam);


Use the InsertParam.Builder to construct an InsertParam object.

import io.milvus.param.InsertParam;
InsertParam.Builder builder = InsertParam.newBuilder();

Methods of InsertParam.Builder:

withCollectionName(String collectionName)Sets the target collection name. The collection name cannot be empty or null.collectionName: The name of the collection to insert data into.
withPartitionName(String partitionName)Sets the target partition name (optional).partitionName: The name of the partition to insert data into.
withFields(List<InsertParam.Field> fields)Sets the data to be inserted. The field list cannot be empty. Note that no input is required for the primary key field if auto-ID is enabled.fields: a list of Field objects, each representing a field.
build()Constructs an InsertParam object.N/A

The can throw the following exceptions:

  • ParamException: error if the parameter is invalid.


A tool class to hold a data field.

Methods of InsertParam.Field:

Field(String name, List<?> values)This class only provides a Constructorto create a Field The name of the data field.
  • If data type is Bool, values is List of Boolean;
  • If data type is Int64, values is List of Long;
  • If data type is Float, values is List of Float;
  • If data type is Double, values is List of Double;
  • If data type is VARCHAR, values is List of String;
  • If data type is FloatVector, values is List of List Float;
  • If data type is BinaryVector, values is List of ByteBuffer.


This method catches all the exceptions and returns an R<MutationResult> object.

  • If the API fails on the server side, it returns the error code and message from the server.

  • If the API fails by RPC exception, it returns R.Status.Unknow and the error message of the exception.

  • If the API succeeds, it returns a valid MutationResult held by the R template. You can use MutationResultWrapper to get the returned information.


A tool class to encapsulate the MutationResult.

import io.milvus.response.MutationResultWrapper;
MutationResultWrapper wrapper = new MutationResultWrapper(mutationResult);

Methods of MutationResultWrapper:

getInsertCount()Gets the row count of the inserted entities.long
getLongIDs()Gets the long ID array returned by the insert() interface if the primary key field is int64 type. Throws ParamException if the primary key type is not int64.List
getStringIDs()Gets the string ID array returned by the insert() interface if the primary key field is VARCHAR type. Throws ParamException if the primary key type is not VARCHAR type.List
getDeleteCount()Gets the row count of the deleted entities. Currently, this value is always equal to the input row count.long
getOperationTs()Gets the timestamp of the operation marked by the server. You can use this timestamp as the guarantee timestamp of query/search APIs. Note that the timestamp is not an absolute timestamp, but rather a hybrid value combined by UTC time and internal flags.long


import io.milvus.param.*;
import io.milvus.grpc.MutationResult;

List<InsertParam.Field> fields = new ArrayList<>();
int rowCount = 10000;
List<Long> ids = new ArrayList<>();
for (long i = 0L; i < rowCount; ++i) {
List<List<Float>> vectors = generateFloatVectors(rowCount);

List<InsertParam.Field> fieldsInsert = new ArrayList<>();
fieldsInsert.add(new InsertParam.Field("id", ids));
fieldsInsert.add(new InsertParam.Field("vec", vectors));

InsertParam param = InsertParam.newBuilder()
R<MutationResult> response = client.insert(param);
if (response.getStatus() != R.Status.Success.getCode()) {