# 插入、删除向量

## 在集合中插入向量

1. 随机生成 10000 个 Entity：
``````>>> import random
# Generate 10000 entities.
>>> list_of_int = [random.randint(0, 255) for _ in range(10000)]
>>> vectors = [[random.random() for _ in range(128)] for _ in range(10000)]
``````
``````  private static List<List<Float>> randomFloatVectors() {
SplittableRandom splitCollectionRandom = new SplittableRandom();
List<List<Float>> vectors = new ArrayList<>(10000);
for (int i = 0; i < 10000; ++i) {
splitCollectionRandom = splitCollectionRandom.split();
DoubleStream doubleStream = splitCollectionRandom.doubles(128);
List<Float> vector =
doubleStream.boxed().map(Double::floatValue).collect(Collectors.toList());
}
return vectors;
}
``````
1. 插入向量列表。
``````# Insert embeddings.
>>> hybrid_entities = [
{"name": "duration", "values": list_of_int, "type": DataType.INT32},
{"name": "release_year", "values": list_of_int, "type": DataType.INT64},
{"name": "embedding", "values": vectors, "type":DataType.FLOAT_VECTOR}
]
>>> client.insert('demo_films', hybrid_entities)
``````
``````    // Insert three films with their IDs, duration, release year, and fake embeddings into the collection "demo_films".
List<Long> ids = LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<Integer> durations =  /* A list of 1,000 Integers. */
List<Long> releaseYears =  LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<List<Float>> embeddings = randomFloatVectors();

InsertParam insertParam = InsertParam
.create(collectionName)
``````

``````>>> entity_ids = [id for id in range(10000)]
>>> client.insert('demo_films', hybrid_entities, ids=entity_ids)
``````
``````    //Insert three films with their IDs, duration, release year, and fake embeddings into the collection "demo_films".
List<Long> ids = LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<Integer> durations =  /* A list of 1,000 Integers. */
List<Long> releaseYears =  LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<List<Float>> embeddings = randomFloatVectors();

InsertParam insertParam = InsertParam
.create(collectionName)
.setEntityIds(ids)
``````

## 在分区中插入向量

``````>>> client.insert('demo_films', hybrid_entities, partition_tag="American")
``````
``````  //Insert three films with their IDs, duration, release year, and fake embeddings into the partition "American".
List<Long> ids = LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<Integer> durations =  /* A list of 1,000 Integers. */
List<Long> releaseYears =  LongStream.range(0, 10000).boxed().collect(Collectors.toList());
List<List<Float>> embeddings = randomFloatVectors();

InsertParam insertParam = InsertParam
.create(collectionName)
.setEntityIds(ids)
.setPartitionTag(partitionTag);
``````

## 通过 ID 删除向量

``````>>> ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
``````

``````>>> client.delete_entity_by_id('demo_films', ids)
``````
``````client.deleteEntityByID(collectionName, ids.subList(0, 10));
``````

## 常见问题

Milvus 中自定义 ID 有没有长度限制？ ID 类型是非负的 64 位整型。
Milvus 可以插入重复 ID 的向量吗？ 可以，这样在 Milvus 中会存在相同 ID 的多条向量。
Milvus 是否支持 “边插入边查询” ？ 支持。
Milvus 中单次插入数据有上限吗？ 单次插入数据不能超过 256 MB。