Create a Collection
Milvus Docs 需要你的帮助
本文档暂时没有中文版本,欢迎你成为社区贡献者,协助中文技术文档的翻译。你可以通过页面右边的 编辑 按钮直接贡献你的翻译。更多详情,参考 贡献指南。如需帮助,你可以 提交 GitHub Issue。
This topic describes how to create a collection in Milvus.
A collection consists of one or more partitions. While creating a new collection, Milvus creates a default partition _default
. See Glossary - Collection for more information.
The following example builds a two-shard collection named book
, with a primary key field named book_id
, an INT64
scalar field named word_count
, and a two-dimensional floating point vector field named book_intro
. Real applications will likely use much higher dimensional vectors than the example.
Prepare Schema
- Connecting to Milvus server before any operation.
- The collection to create must contain a primary key field and a vector field. INT64 is the only supported data type for the primary key field in current release of Milvus.
First, prepare necessary parameters, including field schema, collection schema, and collection name.
from pymilvus import CollectionSchema, FieldSchema, DataType
book_id = FieldSchema(
name="book_id",
dtype=DataType.INT64,
is_primary=True,
)
word_count = FieldSchema(
name="word_count",
dtype=DataType.INT64,
)
book_intro = FieldSchema(
name="book_intro",
dtype=DataType.FLOAT_VECTOR,
dim=2
)
schema = CollectionSchema(
fields=[book_id, word_count, book_intro],
description="Test book search"
)
collection_name = "book"
const params = {
collection_name: "book",
fields: [
{
name: "book_intro",
description: "",
data_type: 101, // DataType.FloatVector
type_params: {
dim: "2",
},
},
{
name: "book_id",
data_type: 5, //DataType.Int64
is_primary_key: true,
description: "",
},
{
name: "word_count",
data_type: 5, //DataType.Int64
description: "",
},
],
};
create collection -c book -f book_id:INT64 -f word_count:INT64 -f book_intro:FLOAT_VECTOR:2 -p book_id
Parameter | Description | Option |
---|---|---|
FieldSchema |
Schema of the fields within the collection to create. Refer to Field Schema for more information. | N/A |
name |
Name of the field to create. | N/A |
dtype |
Data type of the field to create. | For primary key field:
|
is_primary (Mandatory for primary key field) |
Switch to control if the field is primary key field. | True or False |
auto_id |
Switch to enable or disable Automatic ID (primary key) allocation. | True or False |
dim (Mandatory for vector field) |
Dimension of the vector. | [1, 32,768] |
description (Optional) |
Description of the field. | N/A |
CollectionSchema |
Schema of the collection to create. Refer to Collection Schema for more information. | |
fields |
Fields of the collection to create. | |
description (Optional) |
Description of the collection to create. | N/A |
collection_name |
Name of the collection to create. |
Parameter | Description | Option |
---|---|---|
collection_name |
Name of the collection to create. | N/A |
description |
Description of the collection to create. | N/A |
fields |
Schema of the filed and the collection to create. | Refer to Field Schema and Collection Schema for more information. |
data_type |
Data type of the filed to create. | Refer to data type reference number for more information. |
is_primary (Mandatory for primary key field) |
Switch to control if the field is primary key field. | True or False |
auto_id |
Switch to enable or disable Automatic ID (primary key) allocation. | True or False |
dim (Mandatory for vector field) |
Dimension of the vector. | [1, 32,768] |
description (Optional) |
Description of the field. | N/A |
Option | Description |
---|---|
-c | The name of the collection. |
-f (Multiple) | The field schema in the ``` |
-p | The name of the primary key field. |
-a (Optional) | Flag to generate IDs automatically. |
-d (Optional) | The description of the collection. |
Create a collection with the schema
Then, create a collection with the schema you specified above.
from pymilvus import Collection
collection = Collection(name=collection_name, schema=schema, using='default', shards_num=2)
await milvusClient.collectionManager.createCollection(params);
# Follow the previous step.
Parameter | Description |
---|---|
using (optional) |
By specifying the server alias here, you can choose in which Milvus server you create a collection. |
shards_num (optional) |
Number of the shards for the collection to create. |
What's next
- Learn more basic operations of Milvus:
- Explore API references for Milvus SDKs: