Skip to main content

Create a StructArray Field

Public Preview

Create a StructArray field when one entity needs to contain an ordered list of structured elements. A StructArray field is an Array field whose element type is Struct. Each Struct element follows the same schema and can contain scalar subfields, vector subfields, and, scalar array subfields.

This page shows how to define a Struct schema, add it as a StructArray field, choose subfields for later search and filtering, and understand the schema rules that apply before you insert or index data.

Before you begin​

On Zilliz Cloud, nullable StructArray fields and dynamic StructArray field addition are supported on Dedicated and On-Demand Clusters. A StructArray field added dynamically must be nullable. Nullable vector arrays, element-level null values, scalar ARRAY subfields, and adding or dropping StructArray subfields are supported on Dedicated Clusters compatible with Milvus v3.0.x. These capabilities are not yet available on On-Demand Clusters.

The creation, nullable creation, element-null creation, and field-addition examples are alternative workflows. Do not run them sequentially against the same collection. Reuse the client initialization for your selected language in the later examples.

This page uses a collection named tech_articles. Each entity represents one technical article, and the chunks field stores chunk-level data as Struct elements.

FieldTypePurpose
doc_idINT64Primary key for the article.
titleVARCHARArticle title.
categoryVARCHARArticle-level category.
title_vectorFLOAT_VECTORArticle-level vector field, used later in hybrid search examples.
chunksARRAYStructArray field that stores chunk-level text, metadata, and embeddings.

The chunks StructArray field contains the following subfields.

SubfieldTypePurpose
textVARCHARChunk text.
sectionVARCHARSection name, such as index, search, or filter.
pageINT64Page number or logical position of the chunk.
quality_scoreFLOATChunk-level score used in scalar filtering and range examples.
has_codeBOOLWhether the chunk contains code.
emb_list_vectorFLOAT_VECTORVector subfield for EmbeddingList search with MAX_SIM* metrics.
embFLOAT_VECTORVector subfield for element-level search with regular vector metrics.
📘Notes

A vector field or vector subfield accepts only one index. If you need both EmbeddingList search and element-level search, define two separate vector subfields. In this example, chunks[emb_list_vector] is for EmbeddingList search, and chunks[emb] is for element-level search.

Supported subfield data types​

A StructArray field stores one array value for each Struct subfield. When you define a Struct schema, choose subfield types from the supported scalar, scalar array, and vector families.

Struct subfield physical typeSupportNotes
Array<Bool>SupportedDefine the subfield as DataType.BOOL.
Array<Int8/Int16/Int32/Int64>SupportedDefine the subfield as DataType.INT8, DataType.INT16, DataType.INT32, or DataType.INT64.
Array<Float/Double>SupportedDefine the subfield as DataType.FLOAT or DataType.DOUBLE.
Array<VarChar>SupportedDefine the subfield as DataType.VARCHAR and set max_length.
ArrayOfVector<FloatVector>SupportedDefine the subfield as DataType.FLOAT_VECTOR and set dim.
ArrayOfVector<Float16Vector>SupportedDefine the subfield as DataType.FLOAT16_VECTOR and set dim.
ArrayOfVector<BFloat16Vector>SupportedDefine the subfield as DataType.BFLOAT16_VECTOR and set dim.
ArrayOfVector<Int8Vector>SupportedDefine the subfield as DataType.INT8_VECTOR and set dim.
ArrayOfVector<BinaryVector>SupportedDefine the subfield as DataType.BINARY_VECTOR and set dim.
ArrayOfVector<SparseFloatVector>Not supportedSparse vector subfields are not supported in StructArray fields.
Array<String>Not supportedUse VARCHAR, not String.
Array<JSON>Not supportedJSON subfields are not supported in StructArray fields.
Array<Geometry>Not supportedGeometry subfields and GIS functions are not supported in StructArray fields.
Array<Text>Not supportedText subfields are not supported in StructArray fields.
Array<Timestamptz>Not supportedTimestamptz subfields and time-specific expressions are not supported in StructArray fields.
Array<Array>SupportedDefine the subfield as DataType.ARRAY with a scalar element_type and a max_capacity for the inner array. Each Struct element then holds one inner array.
Array of Array of Array, Array of vectors, Struct, or ArrayOfStructNot supportedArray nesting inside a Struct is limited to one level, the inner array cannot hold vectors, and a Struct cannot contain nested Struct or Array-of-Struct subfields.

For version-specific support, nullable behavior, and other limits, see StructArray Limits.

Create a collection with a StructArray field​

To create a StructArray field, first define the Struct schema used by each element. Then add an Array field and set its element type to Struct.

  1. Create the collection schema.

  2. Add collection-level fields, such as the primary key and article-level fields.

  3. Create a Struct schema for elements stored inside the StructArray field.

  4. Add scalar and vector subfields to the Struct schema.

  5. Add an Array field with element_type=DataType.STRUCT.

  6. Set struct_schema to the Struct schema.

  7. Set max_capacity to limit how many Struct elements each entity can store in the field.

python
from pymilvus import MilvusClient, DataType

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN",
)

schema = client.create_schema(
auto_id=False,
enable_dynamic_field=False,
)

# Collection-level fields.
schema.add_field(
field_name="doc_id",
datatype=DataType.INT64,
is_primary=True,
)
schema.add_field(
field_name="title",
datatype=DataType.VARCHAR,
max_length=512,
)
schema.add_field(
field_name="category",
datatype=DataType.VARCHAR,
max_length=128,
)
schema.add_field(
field_name="title_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Struct schema used by each element in the StructArray field.
chunk_schema = client.create_struct_field_schema()
chunk_schema.add_field(
field_name="text",
datatype=DataType.VARCHAR,
max_length=65535,
)
chunk_schema.add_field(
field_name="section",
datatype=DataType.VARCHAR,
max_length=128,
)
chunk_schema.add_field(
field_name="page",
datatype=DataType.INT64,
)
chunk_schema.add_field(
field_name="quality_score",
datatype=DataType.FLOAT,
)
chunk_schema.add_field(
field_name="has_code",
datatype=DataType.BOOL,
)

# Vector subfield for EmbeddingList search.
chunk_schema.add_field(
field_name="emb_list_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Vector subfield for element-level search.
chunk_schema.add_field(
field_name="emb",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Add the StructArray field.
schema.add_field(
field_name="chunks",
datatype=DataType.ARRAY,
element_type=DataType.STRUCT,
struct_schema=chunk_schema,
max_capacity=1000,
)

client.create_collection(
collection_name="tech_articles",
schema=schema,
)

Add a scalar array subfield​

A Struct subfield can be a scalar ARRAY. Each Struct element then holds one inner array, such as a list of tags for each chunk. Define the subfield with DataType.ARRAY, a scalar element_type, and a max_capacity for the inner array.

python
chunk_schema.add_field(
field_name="tags",
datatype=DataType.ARRAY,
element_type=DataType.VARCHAR,
max_capacity=16,
max_length=64,
)

Array subfields follow these rules:

  • The inner array holds scalar values only. It cannot hold vectors, Struct elements, or another array.

  • The max_capacity of the subfield limits the inner array. The max_capacity of the StructArray field still limits the number of Struct elements.

  • Scalar indexes are not supported on array subfields.

  • In filters, use array_contains, array_contains_any, array_contains_all, and array_length on $[tags] inside element_filter or MATCH_*, or on a positional path such as chunks[0][tags]. For details, see StructArray Operators.

Allow null values in Struct elements​

A scalar, vector, or array subfield can allow a null value for that subfield in individual Struct elements (an element-nullable subfield). Set nullable=True on a scalar or vector subfield. On an array subfield, set nullable=True to allow a null inner array and element_nullable=True to allow null values inside the inner array. For example, a chunk can have no detected language while its other subfields hold values.

The following example creates tech_articles with the complete schema that the element-level-null examples in Insert Data into StructArray Fields assume: the subfields from the first example, plus an element-nullable lang subfield and an element-nullable tags array subfield.

python
from pymilvus import MilvusClient, DataType

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN",
)

schema = client.create_schema(
auto_id=False,
enable_dynamic_field=False,
)

# Collection-level fields.
schema.add_field(
field_name="doc_id",
datatype=DataType.INT64,
is_primary=True,
)
schema.add_field(
field_name="title",
datatype=DataType.VARCHAR,
max_length=512,
)
schema.add_field(
field_name="category",
datatype=DataType.VARCHAR,
max_length=128,
)
schema.add_field(
field_name="title_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Struct schema used by each element in the StructArray field.
chunk_schema = client.create_struct_field_schema()
chunk_schema.add_field(
field_name="text",
datatype=DataType.VARCHAR,
max_length=65535,
)
chunk_schema.add_field(
field_name="section",
datatype=DataType.VARCHAR,
max_length=128,
)
chunk_schema.add_field(
field_name="page",
datatype=DataType.INT64,
)
chunk_schema.add_field(
field_name="quality_score",
datatype=DataType.FLOAT,
)
chunk_schema.add_field(
field_name="has_code",
datatype=DataType.BOOL,
)

# Vector subfield for EmbeddingList search.
chunk_schema.add_field(
field_name="emb_list_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Vector subfield for element-level search.
chunk_schema.add_field(
field_name="emb",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# An element-nullable scalar subfield.
chunk_schema.add_field(
field_name="lang",
datatype=DataType.VARCHAR,
max_length=16,
nullable=True,
)

# An element-nullable array subfield: nullable=True allows a null inner
# array and element_nullable=True allows null values inside the inner array.
chunk_schema.add_field(
field_name="tags",
datatype=DataType.ARRAY,
element_type=DataType.VARCHAR,
max_capacity=16,
max_length=64,
nullable=True,
element_nullable=True,
)

# Add the StructArray field. nullable=True applies to the whole field.
schema.add_field(
field_name="chunks",
datatype=DataType.ARRAY,
element_type=DataType.STRUCT,
struct_schema=chunk_schema,
max_capacity=1000,
nullable=True,
)

client.create_collection(
collection_name="tech_articles",
schema=schema,
)

Element-level null follows these rules:

  • Row-level nullable applies to the whole StructArray field and is set on the parent field only. Element-level null is set on the subfield itself: nullable=True on a scalar or vector subfield, element_nullable=True on an array subfield.

  • A Struct element itself cannot be null. Set the subfield values of the element to null instead.

  • When you insert data, you can omit an element-nullable subfield from a Struct element or set it to None. Both store a null value. A subfield that is not element-nullable must be present in every Struct element.

  • On an array subfield, nullable=True allows a null inner array for a Struct element and element_nullable=True allows null values inside the inner array. An empty inner array is not null.

  • An element-nullable vector subfield (nullable=True) supports element-level search only. MAX_SIM* metrics and EmbeddingList search are rejected on it, and null vectors do not participate in search.

  • Scalar indexes are not supported on element-nullable subfields.

  • In filters, a null subfield value makes the element predicate unknown rather than false. Use $[lang] is null or $[lang] is not null inside element_filter or MATCH_* to test it. For details, see StructArray Operators.

Understand StructArray field paths​

After you create a StructArray field, refer to its subfields with the structArray[subfield] path syntax. Use this syntax when you create indexes, search vector subfields, output subfields, or build scalar filters.

PathMeaningCommon usage
chunks[text]The text subfield inside each Struct element.Output field or scalar filtering.
chunks[section]The section label for each chunk.Scalar filtering.
chunks[quality_score]The chunk-level quality score.Scalar filtering or scalar index.
chunks[emb_list_vector]The vector subfield used as an embedding list.EmbeddingList search with MAX_SIM*.
chunks[emb]The vector subfield used by each Struct element independently.Element-level vector search.

Make a StructArray field nullable​

A nullable StructArray field allows an entity to store NULL for the entire field, subject to the availability stated above. The following is a complete alternative to the previous collection-creation example. Run it only when tech_articles does not exist; it sets nullable on the StructArray parent before creating the collection.

python
from pymilvus import MilvusClient, DataType

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN",
)

schema = client.create_schema(
auto_id=False,
enable_dynamic_field=False,
)

# Collection-level fields.
schema.add_field(
field_name="doc_id",
datatype=DataType.INT64,
is_primary=True,
)
schema.add_field(
field_name="title",
datatype=DataType.VARCHAR,
max_length=512,
)
schema.add_field(
field_name="category",
datatype=DataType.VARCHAR,
max_length=128,
)
schema.add_field(
field_name="title_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Struct schema used by each element in the StructArray field.
chunk_schema = client.create_struct_field_schema()
chunk_schema.add_field(
field_name="text",
datatype=DataType.VARCHAR,
max_length=65535,
)
chunk_schema.add_field(
field_name="section",
datatype=DataType.VARCHAR,
max_length=128,
)
chunk_schema.add_field(
field_name="page",
datatype=DataType.INT64,
)
chunk_schema.add_field(
field_name="quality_score",
datatype=DataType.FLOAT,
)
chunk_schema.add_field(
field_name="has_code",
datatype=DataType.BOOL,
)

# Vector subfield for EmbeddingList search.
chunk_schema.add_field(
field_name="emb_list_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Vector subfield for element-level search.
chunk_schema.add_field(
field_name="emb",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

# Add the StructArray field.
schema.add_field(
field_name="chunks",
datatype=DataType.ARRAY,
element_type=DataType.STRUCT,
struct_schema=chunk_schema,
nullable=True,
max_capacity=1000,
)

client.create_collection(
collection_name="tech_articles",
schema=schema,
)
📘Notes

NULL applies to the whole StructArray field. Do not configure individual Struct subfields as row-level nullable independently. When supplying a valid StructArray value, do not mix null subfield arrays with valid subfield arrays. A Struct element itself cannot be null. Element-nullable subfields can hold null values in individual Struct elements; see the next section. For details, see StructArray Limits.

Add a StructArray field to an existing collection​

Subject to the availability stated above, you can add a StructArray field to an existing collection. The added StructArray field must be nullable, because entities that already exist in the collection do not have values for the new field.

For this alternative workflow, start with tech_articles containing the four collection-level fields from the first example but no chunks field. Reuse the initialized client for your selected language. Define the complete Struct schema, then add the field with its parent marked nullable. This schema operation is independent of the collection’s dynamic field setting (enable_dynamic_field); it does not enable arbitrary subfields inside a Struct.

python
chunk_schema = client.create_struct_field_schema()
chunk_schema.add_field(
field_name="text",
datatype=DataType.VARCHAR,
max_length=65535,
)
chunk_schema.add_field(
field_name="section",
datatype=DataType.VARCHAR,
max_length=128,
)
chunk_schema.add_field(
field_name="page",
datatype=DataType.INT64,
)
chunk_schema.add_field(
field_name="quality_score",
datatype=DataType.FLOAT,
)
chunk_schema.add_field(
field_name="has_code",
datatype=DataType.BOOL,
)
chunk_schema.add_field(
field_name="emb_list_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)
chunk_schema.add_field(
field_name="emb",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

client.add_collection_struct_field(
collection_name="tech_articles",
field_name="chunks",
struct_schema=chunk_schema,
max_capacity=1000,
nullable=True,
)

After the StructArray field is added, existing entities return NULL for the new field until you populate it.

Add a subfield to an existing StructArray field​

You can add a subfield to a StructArray field that already holds data. Call add_collection_field() with the subfield path as the field name and set nullable=True on the new subfield (required for every added subfield, and what allows element-level null). On an array subfield, additionally set element_nullable=True to allow null values inside the inner array. The new subfield inherits the row-level nullable setting and the max_capacity of the StructArray field.

python
client.add_collection_field(
collection_name="tech_articles",
field_name="chunks[lang]",
data_type=DataType.VARCHAR,
max_length=16,
nullable=True,
)

Adding a subfield follows these rules:

  • The added subfield must be created with nullable=True, and an added array subfield additionally takes element_nullable=True to allow null values inside the inner array. It cannot have a default value. Existing entities return null for the new subfield in each of their Struct elements, and entities whose StructArray field is null stay null.

  • The subfield can be a scalar, a vector, or a scalar array. Zilliz Cloud appends it after the existing subfields.

  • You cannot bind an index while adding the subfield. Create the index with a separate create_index() call. An added vector subfield supports element-level metrics only, and the collection must have an index on it before you load the collection with the new subfield included.

  • Clients that omit the new subfield when inserting keep working, because Zilliz Cloud stores null for omitted element-nullable subfields.

Dropping a field removes its data; adding it again does not restore the old values. Preserve the data you need, follow the prerequisites in Alter Collection Schema, then repopulate and reindex the replacement field. This example retains the collection-level title_vector field so that dropping chunks does not remove every vector field from the collection.

python
updated_chunk_schema = client.create_struct_field_schema()
updated_chunk_schema.add_field(
field_name="text",
datatype=DataType.VARCHAR,
max_length=65535,
)
updated_chunk_schema.add_field(
field_name="section",
datatype=DataType.VARCHAR,
max_length=128,
)
updated_chunk_schema.add_field(
field_name="page",
datatype=DataType.INT64,
)
updated_chunk_schema.add_field(
field_name="quality_score",
datatype=DataType.FLOAT,
)
updated_chunk_schema.add_field(
field_name="has_code",
datatype=DataType.BOOL,
)
updated_chunk_schema.add_field(
field_name="emb_list_vector",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)
updated_chunk_schema.add_field(
field_name="emb",
datatype=DataType.FLOAT_VECTOR,
dim=4,
)

updated_chunk_schema.add_field(
field_name="source",
datatype=DataType.VARCHAR,
max_length=256,
)

client.drop_collection_field(
collection_name="tech_articles",
field_name="chunks",
)

client.add_collection_struct_field(
collection_name="tech_articles",
field_name="chunks",
struct_schema=updated_chunk_schema,
max_capacity=1000,
nullable=True,
)

Drop a subfield from a StructArray field​

You can drop a subfield from an existing StructArray field by its path. Zilliz Cloud removes the subfield, its data, and its index.

python
client.drop_collection_field(
collection_name="tech_articles",
field_name="chunks[lang]",
)

You cannot drop the last subfield of a StructArray field, the last vector field of the collection, or a subfield used by a Function. To remove the whole StructArray field, drop the field by its name, such as chunks. After a subfield is dropped, insert requests that still carry the subfield are rejected.

Dropping a subfield while queries are still running against it can fail transiently: query plans that were built on the older schema keep executing for a short time and fail with an internal error. Finish queries against a subfield before dropping it, or retry the drop after the running queries finish.

Schema rules​

RuleExplanation
Struct is used as an Array element type.Create a StructArray field as an Array field with element_type=STRUCT. Do not create Struct as a top-level collection field.
All elements share one schema.Every Struct element in the same StructArray field follows the Struct schema defined for that field.
max_capacity is required.It limits how many Struct elements each entity can store in the StructArray field.
Only supported subfield types are allowed.Use scalar, scalar array, and vector subfield types supported by StructArray. Do not define JSON, Geometry, Text, Timestamptz, SparseFloatVector, or nested Struct subfields.
Vector subfields need indexes before search.Create indexes on paths such as chunks[emb_list_vector] or chunks[emb] before running vector search.
One vector subfield has one index.If you need both EmbeddingList search and element-level search, create two separate vector subfields.
Subfields can change only.Add an element-nullable subfield or drop a subfield by its path. Existing subfields cannot be changed to element_nullable afterward.
All subfields share one max_capacity.The StructArray field's max_capacity applies to every subfield. An added subfield inherits it.
Functions are not supported inside Struct.Do not define functions for fields or subfields inside a StructArray field.
Scalar subfields should match filter needs.Add fields such as section, quality_score, or has_code only when you need to filter, group, or output them later.

Common mistakes​

  • Creating DataType.STRUCT as a top-level collection field instead of using it as the element type of an Array field.

  • Forgetting to set max_capacity on the StructArray field.

  • Defining unsupported subfield types, such as JSON, Geometry, Text, Timestamptz, SparseFloatVector, nested Struct, Array-of-Struct, or an array subfield that holds vectors or another array.

  • Using String as a subfield type. Use VARCHAR and set max_length.

  • Using one vector subfield for both EmbeddingList search and element-level search.

  • Adding only vector subfields and forgetting scalar subfields needed for filtering, such as section, quality_score, or has_code.

  • Treating vector subfields as $[...] scalar predicate inputs. Use vector subfields for vector search, and scalar subfields for scalar predicates.

  • Assuming subfields can be added to an existing StructArray field without element-level null, or adding a subfield that does not allow element-level null.

  • Setting row-level null on a subfield. Row-level null is set on the StructArray parent only. For null values inside Struct elements, set nullable=True on a scalar or vector subfield, and element_nullable=True on an array subfield.

  • Using chunks.emb or chunks.emb_list_vector instead of the required path syntax chunks[emb] or chunks[emb_list_vector].

  • Treating nullable StructArray behavior as available in every target version.

Next steps​

  1. To insert nested data into the StructArray field, read Insert Data into StructArray Fields.

  2. To create vector and scalar indexes, read Index StructArray Fields.

  3. To search StructArray vector subfields, read Basic Vector Search with StructArray.

  4. To review supported data types, nullable behavior, and version-specific limitations, read StructArray Limits.