|
| 1 | +# CollectionSchema |
| 2 | + |
| 3 | +A **CollectionSchema** instance represents the schema of a collection. A schema sketches the structure of a collection. |
| 4 | + |
| 5 | +```python |
| 6 | +class pymilvus.CollectionSchema |
| 7 | +``` |
| 8 | + |
| 9 | +## Constructor |
| 10 | + |
| 11 | +Constructs the schema of a collection by defining fields, data types, and other parameters. |
| 12 | + |
| 13 | +```python |
| 14 | +CollectionSchema( |
| 15 | + fields: list, |
| 16 | + description: str |
| 17 | +) |
| 18 | +``` |
| 19 | + |
| 20 | +**PARAMETERS:** |
| 21 | + |
| 22 | +- **fields** (*list*) - |
| 23 | + |
| 24 | + **[REQUIRED]** |
| 25 | + |
| 26 | + A list of **FieldSchema** objects that define the fields in the collection schema. |
| 27 | + |
| 28 | + <div class="admonition note"> |
| 29 | + |
| 30 | + <p><b>what is a field schema?</b></p> |
| 31 | + |
| 32 | + <p>A field schema represents and contains metadata for a single field, while <strong>CollectionSchema</strong> ties together a list of FieldSchema objects to define the full schema.</p> |
| 33 | + |
| 34 | + </div> |
| 35 | + |
| 36 | +- **description** (*string*) - |
| 37 | + |
| 38 | + The description of the schema. |
| 39 | + |
| 40 | + If a description is not provided, it will be set to an empty string. |
| 41 | + |
| 42 | +- **kwargs** - |
| 43 | + |
| 44 | + - **auto_id** (*bool*) |
| 45 | + |
| 46 | + Whether allows the primary field to automatically increment. |
| 47 | + |
| 48 | + Setting this to **True** makes the primary field automatically increment. In this case, the primary field should not be included in the data to insert to avoid errors. |
| 49 | + |
| 50 | + - **enable_dynamic_field** (*bool*) |
| 51 | + |
| 52 | + Whether allows Milvus saves the values of undefined fields in a dynamic field if the data being inserted into the target collection includes fields that are not defined in the collection's schema. |
| 53 | + |
| 54 | + When you set this to **True**, Milvus and will create a field called **$meta** to store any undefined fields and their values from the data that is inserted. |
| 55 | + |
| 56 | + <div class="admonition note"> |
| 57 | + |
| 58 | + <p><b>what is a dynamic field?</b></p> |
| 59 | + |
| 60 | + <p>If the data being inserted into the target collection includes fields that are not defined in the collection's schema, those fields will be saved in a dynamic field as key-value pairs.</p> |
| 61 | + |
| 62 | + </div> |
| 63 | + |
| 64 | + - **primary_field** (*str*) |
| 65 | + |
| 66 | + The name of the primary field. |
| 67 | + |
| 68 | + The value should be the name of a field listed in **fields**. |
| 69 | + |
| 70 | + As an alternative, you can set **is_primary** when creating a **FieldSchema** object. |
| 71 | + |
| 72 | + - **partition_key_field** (*str*) |
| 73 | + |
| 74 | + The name of the field that serves as the partition key. |
| 75 | + |
| 76 | + The value should be the name of a field listed in **fields**. |
| 77 | + |
| 78 | + Setting this makes Milvus manage all partitions in the current collection. |
| 79 | + |
| 80 | + As an alternative, you can set **is_partition_key** when creating a **FieldSchema** object. |
| 81 | + |
| 82 | + <div class="admonition note"> |
| 83 | + |
| 84 | + <p><b>what is a partition key?</b></p> |
| 85 | + |
| 86 | + <p>Once a field is designated as the partition key, Milvus automatically creates a partition for each unique value in this field and saves entities in these partitions accordingly.</p> |
| 87 | + <p>This is particularly useful when implementing data separation based on a specific key, such as partition-oriented multi-tenancy.</p> |
| 88 | + <p>As an alternative, you can set <strong>partition<em>key</em>field</strong> when creating a <strong>CollectionSchema</strong> object.</p> |
| 89 | + |
| 90 | + </div> |
| 91 | + |
| 92 | +**RETURN TYPE:** |
| 93 | + |
| 94 | +*CollectionSchema* |
| 95 | + |
| 96 | +**RETURNS:** |
| 97 | + |
| 98 | +A **CollectionSchema** object. |
| 99 | + |
| 100 | +**EXCEPTIONS:** |
| 101 | + |
| 102 | +- **FieldsTypeException**: |
| 103 | + |
| 104 | + This exception will be raised when the **fields** parameter is not a list. |
| 105 | + |
| 106 | +- **FieldTypeException**: |
| 107 | + |
| 108 | + This exception will be raised when a field in the **fields** list is not a **FieldSchema** object. |
| 109 | + |
| 110 | +- **PrimaryKeyException:** |
| 111 | + |
| 112 | + This exception will be raised if |
| 113 | + |
| 114 | + - The **primary_field** parameter has been set but the value is not a string. |
| 115 | + |
| 116 | + - The **primary_field** parameter has been set but the value is not the name of any listed fields. |
| 117 | + |
| 118 | +- **PartitionKeyException:** |
| 119 | + |
| 120 | + This exception will be raised if |
| 121 | + |
| 122 | + - The **partition_key_field** parameter has been set but the value is not a string. |
| 123 | + |
| 124 | + - The **partition_key_field** parameter has been set but the value is not the name of any listed fields. |
| 125 | + |
| 126 | +- **AutoIDException:** |
| 127 | + |
| 128 | + - This exception will be raised if the **auto_id** parameter has been set but the value is not a boolean. |
| 129 | + |
| 130 | +## Examples |
| 131 | + |
| 132 | +```python |
| 133 | +from pymilvus import CollectionSchema, FieldSchema, DataType |
| 134 | + |
| 135 | +# Define fields in a schema |
| 136 | +primary_key = FieldSchema( |
| 137 | + name="id", |
| 138 | + dtype=DataType.INT64, |
| 139 | + is_primary=True, |
| 140 | +) |
| 141 | + |
| 142 | +vector = FieldSchema( |
| 143 | + name="vector", |
| 144 | + dtype=DataType.FLOAT_VECTOR, |
| 145 | + dim=768 |
| 146 | +) |
| 147 | + |
| 148 | +# Construct a schema with the predefined fields |
| 149 | +schema = CollectionSchema( |
| 150 | + fields=[primary_key, vector], |
| 151 | + description="example_schema" |
| 152 | +) |
| 153 | +``` |
| 154 | + |
| 155 | +## Methods |
| 156 | + |
| 157 | +The following are the methods of the `CollectionSchema` class: |
| 158 | + |
0 commit comments